Skip to content

Commit

Permalink
add "limit" option to search
Browse files Browse the repository at this point in the history
  • Loading branch information
garak committed Oct 30, 2020
1 parent 6194b1a commit e45a021
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -48,6 +48,14 @@ sylius/promotions-bundle
...
```

#### You can limit results to a desired amount of pages:

```php
<?php

$client->search('sylius', [], 2) // get first 2 pages
```

#### Get package details:

```php
Expand Down
11 changes: 11 additions & 0 deletions spec/Packagist/Api/ClientSpec.php
Expand Up @@ -34,6 +34,17 @@ function it_search_for_packages(HttpClient $client, Factory $factory, Response $
$this->search('sylius');
}

function it_search_for_packages_with_limit(HttpClient $client, Factory $factory, Response $response)
{
$data = file_get_contents('spec/Packagist/Api/Fixture/search.json');
$response->getBody()->shouldBeCalled()->willReturn($data);

$client->request('get', 'https://packagist.org/search.json?q=sylius')->shouldBeCalled()->willReturn($response);
$factory->create(json_decode($data, true))->shouldBeCalled()->willReturn(array());

$this->search('sylius', [], 2);
}

function it_searches_for_packages_with_filters(HttpClient $client, Factory $factory, Response $response)
{
$data = file_get_contents('spec/Packagist/Api/Fixture/search.json');
Expand Down
5 changes: 3 additions & 2 deletions src/Packagist/Api/Client.php
Expand Up @@ -68,10 +68,11 @@ public function __construct(
*
* @param string $query Name of package
* @param array $filters An array of filters
* @param int $limit Pages tolimit results (0 = all pages)
*
* @return array The results
*/
public function search($query, array $filters = array())
public function search($query, array $filters = array(), int $limit = 0)
{
$results = $response = array();
$filters['q'] = $query;
Expand All @@ -86,7 +87,7 @@ public function search($query, array $filters = array())
$createResult = [$createResult];
}
$results = array_merge($results, $createResult);
} while (isset($response['next']));
} while (isset($response['next']) && (0 === $limit || $response['next'] <= $limit));

return $results;
}
Expand Down

0 comments on commit e45a021

Please sign in to comment.