Skip to content

Commit

Permalink
Require array repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
ginfarma committed Feb 16, 2023
1 parent ad47777 commit 5d15ded
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 160 deletions.
44 changes: 20 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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"
* },
* ....
* ]
Expand Down
2 changes: 1 addition & 1 deletion src/ImgFinder/Cache/CacheTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 9 additions & 12 deletions src/ImgFinder/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
];
}

Expand Down
6 changes: 3 additions & 3 deletions src/ImgFinder/Repository/PexelsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions src/ImgFinder/Repository/UnsplashRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
75 changes: 35 additions & 40 deletions src/ImgFinder/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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
);
}

Expand All @@ -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;
}


Expand Down Expand Up @@ -191,6 +185,7 @@ public function cacheKey(): string

private function __construct()
{
$this->slugify = new Slugify();
$this->repositories = [];
$this->slugify = new Slugify();
}
}
38 changes: 18 additions & 20 deletions src/ImgFinder/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 5d15ded

Please sign in to comment.