diff --git a/README.md b/README.md index 94c97cb..7870823 100644 --- a/README.md +++ b/README.md @@ -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" * }, * .... * ] diff --git a/src/ImgFinder/Payload.php b/src/ImgFinder/Payload.php index dc403fd..1a306ad 100644 --- a/src/ImgFinder/Payload.php +++ b/src/ImgFinder/Payload.php @@ -10,6 +10,7 @@ class Payload public const URL_AUTHOR = 'url_author'; public const MEDIA = 'media'; public const THUMBNAIL = 'thumbnail'; + public const REPOSITORY = 'repository'; private ?string $author; @@ -17,15 +18,24 @@ class Payload 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; } @@ -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, ]; } diff --git a/src/ImgFinder/Repository/PexelsRepository.php b/src/ImgFinder/Repository/PexelsRepository.php index 9f3d13d..bc6574b 100644 --- a/src/ImgFinder/Repository/PexelsRepository.php +++ b/src/ImgFinder/Repository/PexelsRepository.php @@ -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, diff --git a/src/ImgFinder/Repository/UnsplashRepository.php b/src/ImgFinder/Repository/UnsplashRepository.php index 94124da..8d89dd7 100644 --- a/src/ImgFinder/Repository/UnsplashRepository.php +++ b/src/ImgFinder/Repository/UnsplashRepository.php @@ -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, diff --git a/src/ImgFinder/Service/RepositoryService.php b/src/ImgFinder/Service/RepositoryService.php index b6d3742..d32e8c9 100644 --- a/src/ImgFinder/Service/RepositoryService.php +++ b/src/ImgFinder/Service/RepositoryService.php @@ -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); } diff --git a/tests/PayloadTest.php b/tests/PayloadTest.php index ea8ab0a..2c5e256 100644 --- a/tests/PayloadTest.php +++ b/tests/PayloadTest.php @@ -15,6 +15,7 @@ class PayloadTest extends TestCase public function it_should_make_valid_instance() { $payload = Payload::build( + 'test', 'https://image', 'https://thumbnail', 'foo', @@ -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',