From 5d15dedf3ca8e47094021172b48795c0187378f2 Mon Sep 17 00:00:00 2001 From: ginsen Date: Thu, 16 Feb 2023 15:01:47 +0100 Subject: [PATCH] Require array repositories --- README.md | 44 +++++------ src/ImgFinder/Cache/CacheTranslator.php | 2 +- src/ImgFinder/Payload.php | 21 +++--- src/ImgFinder/Repository/PexelsRepository.php | 6 +- .../Repository/UnsplashRepository.php | 6 +- src/ImgFinder/Request.php | 75 +++++++++---------- src/ImgFinder/RequestInterface.php | 38 +++++----- src/ImgFinder/Service/RepositoryService.php | 24 +----- tests/Cache/CacheImgRepositoryTest.php | 6 +- tests/Cache/CacheTranslatorTest.php | 10 +-- tests/ImgFinderTest.php | 8 +- tests/PayloadTest.php | 18 ++--- tests/RequestTest.php | 14 ++-- tests/Service/RepositoryServiceTest.php | 12 +-- tests/Service/TranslatorServiceTest.php | 2 +- 15 files changed, 126 insertions(+), 160 deletions(-) diff --git a/README.md b/README.md index 29ced26..94c97cb 100644 --- a/README.md +++ b/README.md @@ -136,39 +136,39 @@ $finder = new ImgFinder\ImgFinder($config); **ImgFinder** is now available to query image repositories, just it only necessary to create a request. -### Request in all repositories +### Request in Pexels & Unsplash repositories ```php -// Search in all repositories -$request = ImgFinder\Request::set('nature'); +// Search in pexels ad unsplash repositories +$request = ImgFinder\Request::set('nature', ['pexels', 'unsplash']); // same as: /** - * @param string $words The search term - * @param string|null $repository The used repository, if it not defined, search in all repositories - * @param int $page Page number - * @param int $perPage Items per page - * @param string $orientation Orientation: 'landscape' or 'portrait', default: 'landscape' - * @param int $widthSmall Width of small photos, default 320 pixels + * @param string $words The search term + * @param array $repositories The used repositories + * @param int $page Page number + * @param int $perPage Items per page + * @param string $orientation Orientation: 'landscape' or 'portrait', default: 'landscape' + * @param int $widthSmall Width of small photos, default 320 pixels */ -$request = ImgFinder\Request::set('nature', null, 1, 10, 'landscape', 320); +$request = ImgFinder\Request::set('nature', ['pexels', 'unsplash'], 1, 10, 'landscape', 320); ``` Both requests are the same, **"nature"** is the search term, **1** is the default page, **10** is the number of response -images for each repository and page (null, 'pexels' or 'unsplash'), **"landscape"** is the orientation -('landscape' or 'portrait'), and finally **320** is the width for thumbnails. +images for each repository, **"landscape"** is the orientation ('landscape' or 'portrait'), and finally **320** is the +width for thumbnails. ### Request to search only in one repository ```php // Search in pexels repository -$request = ImgFinder\Request::set('nature', 'pexels'); +$request = ImgFinder\Request::set('nature', ['pexels']); // same as: -$request = ImgFinder\Request::set('nature', 'pexels', 1, 10, 'landscape', 320); +$request = ImgFinder\Request::set('nature', ['pexels'], 1, 10, 'landscape', 320); ``` ### Search -Finally you only need to perform the search. +Finally, you only need to perform the search. ```php $response = $finder->search($request); @@ -186,7 +186,7 @@ $file = '/your/path/img-finder.yml'; $config = Config::fromYaml($file); $finder = new ImgFinder($config); -$request = Request::set('nature'); +$request = Request::set('nature', ['pexels', 'unsplash']); $response = $finder->search($request); $imagesUrls = $response->toArray(); @@ -197,17 +197,13 @@ echo json_encode($imagesUrls); * { * "author": "Rodolfo Quirós", * "url_author": "https://www.pexels.com/@rquiros", - * "photos": { - * "thumbnail": "https://images.pexels.com/photos/2219118/pexels-photo-2219118.jpeg?auto=compress&cs=tinysrgb&h=350&w=320", - * "image": "https://images.pexels.com/photos/2219118/pexels-photo-2219118.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=627&w=1200" - * } + * "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" * },{ * "author": "Igor Starkov", * "url_author": "https://unsplash.com/@igorstarkoff", - * "photos": { - * "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", - * "image": "https://images.unsplash.com/photo-1595706480968-ca87913ee9c7?ixid=MXwxODk0OTN8MHwxfHNlYXJjaHwxMHx8YmVhdXR5JTIwZmFjZXxlbnwwfDB8fA&ixlib=rb-1.2.1" - * } + * "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" * }, * .... * ] diff --git a/src/ImgFinder/Cache/CacheTranslator.php b/src/ImgFinder/Cache/CacheTranslator.php index 3f28bf5..e912ec1 100644 --- a/src/ImgFinder/Cache/CacheTranslator.php +++ b/src/ImgFinder/Cache/CacheTranslator.php @@ -35,7 +35,7 @@ public function findWord(RequestInterface $request): RequestInterface if ($item->isHit()) { $requestCache = unserialize($item->get()); - return $requestCache->setRepository($request->repository()); + return $requestCache->setRepositories($request->repositories()); } $newRequest = $this->translator->findWord($request); diff --git a/src/ImgFinder/Payload.php b/src/ImgFinder/Payload.php index fd55671..dc403fd 100644 --- a/src/ImgFinder/Payload.php +++ b/src/ImgFinder/Payload.php @@ -8,24 +8,23 @@ class Payload { public const AUTHOR = 'author'; public const URL_AUTHOR = 'url_author'; - public const PHOTOS = 'photos'; + public const MEDIA = 'media'; public const THUMBNAIL = 'thumbnail'; - public const IMAGE = 'image'; - private string $author; - private string $urlAuthor; - private string $urlImage; - private string $thumbnail; + private ?string $author; + private ?string $urlAuthor; + private string $media; + private ?string $thumbnail; - public static function build(string $author, string $urlAuthor, string $urlImage, string $thumbnail): self + public static function build(string $media, ?string $thumbnail, ?string $author = null, ?string $urlAuthor = null): self { $instance = new static(); $instance->author = $author; $instance->urlAuthor = $urlAuthor; - $instance->urlImage = $urlImage; + $instance->media = $media; $instance->thumbnail = $thumbnail; return $instance; @@ -37,10 +36,8 @@ public function render(): iterable return [ self::AUTHOR => $this->author, self::URL_AUTHOR => $this->urlAuthor, - self::PHOTOS => [ - self::THUMBNAIL => $this->thumbnail, - self::IMAGE => $this->urlImage, - ], + self::MEDIA => $this->media, + self::THUMBNAIL => $this->thumbnail, ]; } diff --git a/src/ImgFinder/Repository/PexelsRepository.php b/src/ImgFinder/Repository/PexelsRepository.php index 2608908..9f3d13d 100644 --- a/src/ImgFinder/Repository/PexelsRepository.php +++ b/src/ImgFinder/Repository/PexelsRepository.php @@ -90,10 +90,10 @@ 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( - $photo[self::PHOTOGRAPHER] ?: '', - $photo[self::PHOTOGRAPHER_URL] ?: '', $photo[self::SRC][$orientation], - $thumbnail + $thumbnail, + $photo[self::PHOTOGRAPHER] ?: null, + $photo[self::PHOTOGRAPHER_URL] ?: null ); $response[] = $payload->render(); } diff --git a/src/ImgFinder/Repository/UnsplashRepository.php b/src/ImgFinder/Repository/UnsplashRepository.php index 16e884d..94124da 100644 --- a/src/ImgFinder/Repository/UnsplashRepository.php +++ b/src/ImgFinder/Repository/UnsplashRepository.php @@ -92,10 +92,10 @@ 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( - $photo[self::USER][self::USER_NAME] ?: '', - $photo[self::USER][self::LINKS][self::HTML] ?: '', $photo[self::URLS][self::RAW], - $thumbnail + $thumbnail, + $photo[self::USER][self::USER_NAME] ?: null, + $photo[self::USER][self::LINKS][self::HTML] ?: null ); $response[] = $payload->render(); } diff --git a/src/ImgFinder/Request.php b/src/ImgFinder/Request.php index 4b4f1f3..5ab13ce 100644 --- a/src/ImgFinder/Request.php +++ b/src/ImgFinder/Request.php @@ -13,26 +13,26 @@ class Request implements RequestInterface private int $perPage; private string $orientation; private int $widthSmall; - private ?string $repository; + private array $repositories; private Slugify $slugify; public static function set( string $words, + array $repositories, int $page = 1, int $perPage = 10, string $orientation = 'landscape', - int $widthSmall = 320, - string $repository = null + int $widthSmall = 320 ): RequestInterface { $instance = new static(); - $instance->words = $words; - $instance->page = $page; - $instance->perPage = $perPage; - $instance->orientation = $orientation; - $instance->widthSmall = $widthSmall; - $instance->repository = $repository; + $instance->words = $words; + $instance->page = $page; + $instance->perPage = $perPage; + $instance->orientation = $orientation; + $instance->widthSmall = $widthSmall; + $instance->repositories = $repositories; return $instance; } @@ -42,11 +42,24 @@ public function setWords(string $words): RequestInterface { return self::set( $words, + $this->repositories(), $this->page(), $this->perPage(), $this->orientation(), - $this->widthSmall(), - $this->repository() + $this->widthSmall() + ); + } + + + public function setRepositories(array $repositories): RequestInterface + { + return self::set( + $this->words(), + $repositories, + $this->page(), + $this->perPage(), + $this->orientation(), + $this->widthSmall() ); } @@ -55,11 +68,11 @@ public function setPage(int $page): RequestInterface { return self::set( $this->words(), + $this->repositories(), $page, $this->perPage(), $this->orientation(), - $this->widthSmall(), - $this->repository() + $this->widthSmall() ); } @@ -68,11 +81,11 @@ public function setPerPage(int $perPage): RequestInterface { return self::set( $this->words(), + $this->repositories(), $this->page(), $perPage, $this->orientation(), - $this->widthSmall(), - $this->repository() + $this->widthSmall() ); } @@ -81,11 +94,11 @@ public function setOrientation(string $orientation): RequestInterface { return self::set( $this->words(), + $this->repositories(), $this->page(), $this->perPage(), $orientation, - $this->widthSmall(), - $this->repository() + $this->widthSmall() ); } @@ -94,24 +107,11 @@ public function setWidthSmall(int $width): RequestInterface { return self::set( $this->words(), + $this->repositories(), $this->page(), $this->perPage(), $this->orientation(), $width, - $this->repository() - ); - } - - - public function setRepository(?string $repository): RequestInterface - { - return self::set( - $this->words(), - $this->page(), - $this->perPage(), - $this->orientation(), - $this->widthSmall(), - $repository ); } @@ -134,15 +134,9 @@ public function slugWords(): string } - public function hasRepository(): bool - { - return null !== $this->repository; - } - - - public function repository(): ?string + public function repositories(): array { - return $this->repository; + return $this->repositories; } @@ -191,6 +185,7 @@ public function cacheKey(): string private function __construct() { - $this->slugify = new Slugify(); + $this->repositories = []; + $this->slugify = new Slugify(); } } diff --git a/src/ImgFinder/RequestInterface.php b/src/ImgFinder/RequestInterface.php index b87ba30..e9d78af 100644 --- a/src/ImgFinder/RequestInterface.php +++ b/src/ImgFinder/RequestInterface.php @@ -7,51 +7,49 @@ interface RequestInterface { /** - * @param string $words The search term - * @param int $page Page number - * @param int $perPage Items per page - * @param string $orientation Orientation: 'landscape' or 'portrait', default: 'landscape' - * @param int $widthSmall Width of small photos, default 320 pixels - * @param string|null $repository The used repository, if it not defined, search in all repositories + * @param string $words The search term + * @param string[] $repositories The repositories to use + * @param int $page Page number + * @param int $perPage Items per page + * @param string $orientation Orientation: 'landscape' or 'portrait', default: 'landscape' + * @param int $widthSmall Width of small photos, default 320 pixels * @return static */ public static function set( string $words, + array $repositories, int $page = 1, int $perPage = 10, string $orientation = 'landscape', - int $widthSmall = 320, - string $repository = null + int $widthSmall = 320 ): self; public function setWords(string $words): self; - public function setPage(int $page): self; - - public function setPerPage(int $perPage): self; - - public function setOrientation(string $orientation): self; - - public function setWidthSmall(int $width): self; - - public function setRepository(string $repository): self; - public function words(): string; public function urlWords(): string; public function slugWords(): string; - public function hasRepository(): bool; + public function setRepositories(array $repositories): self; - public function repository(): ?string; + public function repositories(): array; + + public function setPage(int $page): self; public function page(): int; + public function setPerPage(int $perPage): self; + public function perPage(): int; + public function setOrientation(string $orientation): self; + public function orientation(): string; + public function setWidthSmall(int $width): self; + public function widthSmall(): int; public function isEqual(self $request): bool; diff --git a/src/ImgFinder/Service/RepositoryService.php b/src/ImgFinder/Service/RepositoryService.php index 2e85197..b6d3742 100644 --- a/src/ImgFinder/Service/RepositoryService.php +++ b/src/ImgFinder/Service/RepositoryService.php @@ -40,30 +40,14 @@ public function names(): iterable public function findImages(RequestInterface $request): ResponseInterface - { - if ($request->hasRepository()) { - return $this->findInOneRepository($request); - } - - return $this->findInAllRepositories($request); - } - - - protected function findInOneRepository(RequestInterface $request): ResponseInterface - { - $imgRepo = $this->repositories[$request->repository()]; - - return $imgRepo->findImages($request); - } - - - protected function findInAllRepositories(RequestInterface $request): ResponseInterface { $response = Response::fromUrls([]); foreach ($this->repositories as $imgRepo) { - $newResp = $imgRepo->findImages($request); - $response = $response->merge($newResp); + if (array_key_exists($imgRepo->name(), $request->repositories())) { + $newResp = $imgRepo->findImages($request); + $response = $response->merge($newResp); + } } return $response; diff --git a/tests/Cache/CacheImgRepositoryTest.php b/tests/Cache/CacheImgRepositoryTest.php index bef374c..9dada9e 100644 --- a/tests/Cache/CacheImgRepositoryTest.php +++ b/tests/Cache/CacheImgRepositoryTest.php @@ -26,7 +26,7 @@ public function it_should_find_images_without_cache_and_response_empty() $imgRepo = $this->getImgRepository(); $imgRepoCache = new CacheImgRepository($cache, $imgRepo); - $request = Request::set('hello world'); + $request = Request::set('hello world', ['testRepo']); $response = $imgRepoCache->findImages($request); self::assertInstanceOf(ResponseInterface::class, $response); @@ -45,7 +45,7 @@ public function it_should_find_images_without_cache_and_response_urls() $imgRepo = $this->getImgRepository($urls); $imgRepoCache = new CacheImgRepository($cache, $imgRepo); - $request = Request::set('hello world'); + $request = Request::set('hello world', ['testRepo']); $response = $imgRepoCache->findImages($request); self::assertInstanceOf(ResponseInterface::class, $response); @@ -65,7 +65,7 @@ public function it_should_find_images_from_cache() $imgRepo = $this->getImgRepository($urls); $imgRepoCache = new CacheImgRepository($cache, $imgRepo); - $request = Request::set('hello world'); + $request = Request::set('hello world', ['testRepo']); self::assertInstanceOf(ResponseInterface::class, $imgRepoCache->findImages($request)); } diff --git a/tests/Cache/CacheTranslatorTest.php b/tests/Cache/CacheTranslatorTest.php index 9d8edb6..8f508a7 100644 --- a/tests/Cache/CacheTranslatorTest.php +++ b/tests/Cache/CacheTranslatorTest.php @@ -20,7 +20,7 @@ class CacheTranslatorTest extends TestCase */ public function it_should_translate_without_cache_and_translator_no_find_word() { - $request = Request::set('hello world'); + $request = Request::set('hello world', ['testRepo']); $item = $this->getItemCache(); $cache = $this->getCachePool($item); $translator = $this->getTranslator($request); @@ -37,8 +37,8 @@ public function it_should_translate_without_cache_and_translator_no_find_word() */ public function it_should_translate_without_cache_and_translator_find_word() { - $request = Request::set('hello world'); - $transRequest = Request::set('hola mundo'); + $request = Request::set('hello world', ['testRepo']); + $transRequest = Request::set('hola mundo', ['testRepo']); $item = $this->getItemCache(); $cache = $this->getCachePool($item); $translator = $this->getTranslator($transRequest); @@ -55,8 +55,8 @@ public function it_should_translate_without_cache_and_translator_find_word() */ public function it_should_find_request_cache() { - $request = Request::set('hello world'); - $transRequest = Request::set('hola mundo'); + $request = Request::set('hello world', ['testRepo']); + $transRequest = Request::set('hola mundo', ['testRepo']); $translator = $this->getTranslator($transRequest); $item = $this->getItemCache($transRequest); $cache = $this->getCachePool($item); diff --git a/tests/ImgFinderTest.php b/tests/ImgFinderTest.php index cc6bb70..43b8580 100644 --- a/tests/ImgFinderTest.php +++ b/tests/ImgFinderTest.php @@ -36,7 +36,7 @@ public function it_should_search_images() $config = $this->getConfig(); $finder = new ImgFinder($config); - $request = Request::set('cuidados bebé'); + $request = Request::set('cuidados bebé', ['testRepo']); $response = $finder->search($request); self::assertInstanceOf(ResponseInterface::class, $response); @@ -72,7 +72,11 @@ public function getConfig(): Config public function translatorService(): TranslatorService { $translator = m::mock(TranslatorService::class); - $translator->shouldReceive('translate')->andReturn(Request::set('translate request')); + $translator + ->shouldReceive('translate') + ->andReturn( + Request::set('translate request', ['testRepo']) + ); return $translator; } diff --git a/tests/PayloadTest.php b/tests/PayloadTest.php index de4eada..ea8ab0a 100644 --- a/tests/PayloadTest.php +++ b/tests/PayloadTest.php @@ -15,10 +15,10 @@ class PayloadTest extends TestCase public function it_should_make_valid_instance() { $payload = Payload::build( + 'https://image', + 'https://thumbnail', 'foo', - 'http://foo.io', - 'http://image', - 'http://thumbnail' + 'https://foo.io' ); self::assertInstanceOf(Payload::class, $payload); @@ -31,18 +31,18 @@ public function it_should_make_valid_instance() public function it_should_render_to_array() { $payload = Payload::build( + 'https://image', + 'https://thumbnail', 'foo', - 'http://foo', - 'http://image', - 'http://thumbnail' + 'https://foo' ); $data = $payload->render(); self::assertIsArray($data); self::assertSame('foo', $data[Payload::AUTHOR]); - self::assertSame('http://foo', $data[Payload::URL_AUTHOR]); - self::assertSame('http://image', $data[Payload::PHOTOS][Payload::IMAGE]); - self::assertSame('http://thumbnail', $data[Payload::PHOTOS][Payload::THUMBNAIL]); + self::assertSame('https://foo', $data[Payload::URL_AUTHOR]); + self::assertSame('https://image', $data[Payload::MEDIA]); + self::assertSame('https://thumbnail', $data[Payload::THUMBNAIL]); } } diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 69e23f0..ffdb52b 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -15,7 +15,7 @@ class RequestTest extends TestCase */ public function it_should_make_valid_instance() { - $request = Request::set('tests'); + $request = Request::set('tests', ['testRepo']); self::assertInstanceOf(RequestInterface::class, $request); } @@ -26,7 +26,7 @@ public function it_should_make_valid_instance() */ public function it_should_return_default_values() { - $request = Request::set('tests'); + $request = Request::set('tests', ['testRepo']); self::assertSame('tests', $request->words()); self::assertSame(1, $request->page()); @@ -41,7 +41,7 @@ public function it_should_return_default_values() */ public function it_should_allow_set_values() { - $request1 = Request::set('tests'); + $request1 = Request::set('tests', ['testRepo']); $request2 = $request1->setWords('other phrase'); $request3 = $request2->setPage(2); $request4 = $request3->setPerPage(11); @@ -70,8 +70,8 @@ public function it_should_allow_set_values() */ public function it_should_compare_equals_instances() { - $request1 = Request::set('tests'); - $request2 = Request::set('tests'); + $request1 = Request::set('tests', ['testRepo']); + $request2 = Request::set('tests', ['testRepo']); self::assertFalse($request1->isEqual($request2)); self::assertTrue($request1->isEqual($request1)); @@ -84,7 +84,7 @@ public function it_should_compare_equals_instances() */ public function it_should_return_words_in_several_formats() { - $request = Request::set('protección crema solar!'); + $request = Request::set('protección crema solar!', ['testRepo']); self::assertSame('protección crema solar!', $request->words()); self::assertSame('protecci%C3%B3n+crema+solar%21', $request->urlWords()); @@ -97,7 +97,7 @@ public function it_should_return_words_in_several_formats() */ public function it_should_return_cache_key_in_slug_format() { - $request = Request::set('tests'); + $request = Request::set('tests', ['testRepo']); self::assertSame('landscape-10-320-tests-1', $request->cacheKey()); } diff --git a/tests/Service/RepositoryServiceTest.php b/tests/Service/RepositoryServiceTest.php index d536402..7ff7b2f 100644 --- a/tests/Service/RepositoryServiceTest.php +++ b/tests/Service/RepositoryServiceTest.php @@ -41,7 +41,7 @@ public function it_should_init_instance_with_cache() public function it_should_return_response_when_request_find_images() { $service = $this->makeService(); - $request = Request::set('test'); + $request = Request::set('test', ['spy-repository']); $response = $service->findImages($request); self::assertInstanceOf(ResponseInterface::class, $response); @@ -54,15 +54,7 @@ public function it_should_return_response_when_request_find_images() public function it_should_return_response_when_request_find_images_from_one_repository() { $service = $this->makeService(); - $request = Request::set( - 'test', - 1, - 10, - 'landscape', - 320, - 'spy-repository' - ); - + $request = Request::set('test', ['spy-repository']); $response = $service->findImages($request); self::assertInstanceOf(ResponseInterface::class, $response); diff --git a/tests/Service/TranslatorServiceTest.php b/tests/Service/TranslatorServiceTest.php index 2e556c3..d103e32 100644 --- a/tests/Service/TranslatorServiceTest.php +++ b/tests/Service/TranslatorServiceTest.php @@ -40,7 +40,7 @@ public function it_should_init_instance_with_cache() public function it_should_return_request_when_find_translator() { $service = $this->makeService(); - $request = Request::set('test'); + $request = Request::set('test', ['testRepo']); $newRequest = $service->translate($request); self::assertTrue($request->isEqual($newRequest));