Skip to content

Commit

Permalink
Fix error when search repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
ginfarma committed Feb 17, 2023
1 parent 5d15ded commit 682d527
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,14 @@ echo json_encode($imagesUrls);
* "author": "Rodolfo Quirós",
* "url_author": "https://www.pexels.com/@rquiros",
* "media": "https://images.pexels.com/photos/2219118/pexels-photo-2219118.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=627&w=1200",
* "thumbnail": "https://images.pexels.com/photos/2219118/pexels-photo-2219118.jpeg?auto=compress&cs=tinysrgb&h=350&w=320"
* "thumbnail": "https://images.pexels.com/photos/2219118/pexels-photo-2219118.jpeg?auto=compress&cs=tinysrgb&h=350&w=320",
* "repository": "pexels"
* },{
* "author": "Igor Starkov",
* "url_author": "https://unsplash.com/@igorstarkoff",
* "media": "https://images.unsplash.com/photo-1595706480968-ca87913ee9c7?ixid=MXwxODk0OTN8MHwxfHNlYXJjaHwxMHx8YmVhdXR5JTIwZmFjZXxlbnwwfDB8fA&ixlib=rb-1.2.1",
* "thumbnail": "https://images.unsplash.com/photo-1595706480968-ca87913ee9c7?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MXwxODk0OTN8MHwxfHNlYXJjaHwxMHx8YmVhdXR5JTIwZmFjZXxlbnwwfDB8fA&ixlib=rb-1.2.1&q=80&w=320"
* "thumbnail": "https://images.unsplash.com/photo-1595706480968-ca87913ee9c7?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MXwxODk0OTN8MHwxfHNlYXJjaHwxMHx8YmVhdXR5JTIwZmFjZXxlbnwwfDB8fA&ixlib=rb-1.2.1&q=80&w=320",
* "repository": "unsplash"
* },
* ....
* ]
Expand Down
21 changes: 16 additions & 5 deletions src/ImgFinder/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,32 @@ class Payload
public const URL_AUTHOR = 'url_author';
public const MEDIA = 'media';
public const THUMBNAIL = 'thumbnail';
public const REPOSITORY = 'repository';


private ?string $author;
private ?string $urlAuthor;
private string $media;
private ?string $thumbnail;

private string $repository;

public static function build(string $media, ?string $thumbnail, ?string $author = null, ?string $urlAuthor = null): self

public static function build(
string $repository,
string $media,
?string $thumbnail,
?string $author = null,
?string $urlAuthor = null
): self
{
$instance = new static();

$instance->author = $author;
$instance->urlAuthor = $urlAuthor;
$instance->media = $media;
$instance->thumbnail = $thumbnail;
$instance->repository = $repository;
$instance->author = $author;
$instance->urlAuthor = $urlAuthor;
$instance->media = $media;
$instance->thumbnail = $thumbnail;

return $instance;
}
Expand All @@ -38,6 +48,7 @@ public function render(): iterable
self::URL_AUTHOR => $this->urlAuthor,
self::MEDIA => $this->media,
self::THUMBNAIL => $this->thumbnail,
self::REPOSITORY => $this->repository,
];
}

Expand Down
1 change: 1 addition & 0 deletions src/ImgFinder/Repository/PexelsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ private function createResponse(iterable $data, RequestInterface $request): Resp
foreach ($data[self::PHOTOS] as $photo) {
$thumbnail = $this->thumbnail($photo[self::SRC][self::MEDIUM], $request);
$payload = Payload::build(
$this->name(),
$photo[self::SRC][$orientation],
$thumbnail,
$photo[self::PHOTOGRAPHER] ?: null,
Expand Down
1 change: 1 addition & 0 deletions src/ImgFinder/Repository/UnsplashRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private function createResponse(iterable $data, RequestInterface $request): Resp
foreach ($data[self::RESULTS] as $photo) {
$thumbnail = $this->thumbnail($photo[self::URLS][self::THUMB], $request);
$payload = Payload::build(
$this->name(),
$photo[self::URLS][self::RAW],
$thumbnail,
$photo[self::USER][self::USER_NAME] ?: null,
Expand Down
2 changes: 1 addition & 1 deletion src/ImgFinder/Service/RepositoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function findImages(RequestInterface $request): ResponseInterface
$response = Response::fromUrls([]);

foreach ($this->repositories as $imgRepo) {
if (array_key_exists($imgRepo->name(), $request->repositories())) {
if (in_array($imgRepo->name(), $request->repositories())) {
$newResp = $imgRepo->findImages($request);
$response = $response->merge($newResp);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/PayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PayloadTest extends TestCase
public function it_should_make_valid_instance()
{
$payload = Payload::build(
'test',
'https://image',
'https://thumbnail',
'foo',
Expand All @@ -31,6 +32,7 @@ public function it_should_make_valid_instance()
public function it_should_render_to_array()
{
$payload = Payload::build(
'test',
'https://image',
'https://thumbnail',
'foo',
Expand Down

0 comments on commit 682d527

Please sign in to comment.