Skip to content

Commit

Permalink
add criteria parameter to filter images - fix #87
Browse files Browse the repository at this point in the history
  • Loading branch information
toin0u committed Feb 26, 2015
1 parent 48e4e61 commit f894dba
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
filter:
paths: [src/*]
tools:
php_analyzer: true
php_mess_detector: true
php_pdepend: true
external_code_coverage:
timeout: '600'
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ $image = $digitalocean->image();
// return a collection of Image entity
$images = $image->getAll();

// return a collection of distribution Image entity
$images = $image->getAll(['type' => 'distribution']);

// return a collection of application Image entity
$images = $image->getAll(['type' => 'application']);

// return a collection of private Image entity
$images = $image->getAll(['private' => true]);

// return a collection of private application Image entity
$images = $image->getAll(['type' => 'application', 'private' => true]);

// return the Image entity 123
$image123 = $image->getById(123);

Expand Down
72 changes: 72 additions & 0 deletions spec/DigitalOceanV2/Api/ImageSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,78 @@ function it_returns_an_array_of_image_entity($adapter)
$meta->total->shouldBe($total);
}

function it_returns_an_array_of_distribution_image_entity($adapter)
{
$total = 3;
$adapter
->get('https://api.digitalocean.com/v2/images?per_page='.PHP_INT_MAX.'&type=distribution')
->willReturn(sprintf('{"images": [{},{},{}], "meta": {"total": %d}}', $total));

$images = $this->getAll(['type' => 'distribution']);
$images->shouldBeArray();
$images->shouldHaveCount($total);
foreach ($images as $image) {
$image->shouldReturnAnInstanceOf('DigitalOceanV2\Entity\Image');
}
$meta = $this->getMeta();
$meta->shouldHaveType('DigitalOceanV2\Entity\Meta');
$meta->total->shouldBe($total);
}

function it_returns_an_array_of_application_image_entity($adapter)
{
$total = 3;
$adapter
->get('https://api.digitalocean.com/v2/images?per_page='.PHP_INT_MAX.'&type=application')
->willReturn(sprintf('{"images": [{},{},{}], "meta": {"total": %d}}', $total));

$images = $this->getAll(['type' => 'application']);
$images->shouldBeArray();
$images->shouldHaveCount($total);
foreach ($images as $image) {
$image->shouldReturnAnInstanceOf('DigitalOceanV2\Entity\Image');
}
$meta = $this->getMeta();
$meta->shouldHaveType('DigitalOceanV2\Entity\Meta');
$meta->total->shouldBe($total);
}

function it_returns_an_array_of_private_application_image_entity($adapter)
{
$total = 3;
$adapter
->get('https://api.digitalocean.com/v2/images?per_page='.PHP_INT_MAX.'&type=application&private=true')
->willReturn(sprintf('{"images": [{},{},{}], "meta": {"total": %d}}', $total));

$images = $this->getAll(['type' => 'application', 'private' => true]);
$images->shouldBeArray();
$images->shouldHaveCount($total);
foreach ($images as $image) {
$image->shouldReturnAnInstanceOf('DigitalOceanV2\Entity\Image');
}
$meta = $this->getMeta();
$meta->shouldHaveType('DigitalOceanV2\Entity\Meta');
$meta->total->shouldBe($total);
}

function it_returns_an_array_of_private_image_entity($adapter)
{
$total = 3;
$adapter
->get('https://api.digitalocean.com/v2/images?per_page='.PHP_INT_MAX.'&private=true')
->willReturn(sprintf('{"images": [{},{},{}], "meta": {"total": %d}}', $total));

$images = $this->getAll(['private' => true]);
$images->shouldBeArray();
$images->shouldHaveCount($total);
foreach ($images as $image) {
$image->shouldReturnAnInstanceOf('DigitalOceanV2\Entity\Image');
}
$meta = $this->getMeta();
$meta->shouldHaveType('DigitalOceanV2\Entity\Meta');
$meta->total->shouldBe($total);
}

function it_returns_an_image_entity_get_by_its_id($adapter)
{
$adapter
Expand Down
20 changes: 16 additions & 4 deletions src/Api/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,29 @@
class Image extends AbstractApi
{
/**
* @param array $criteria
*
* @return ImageEntity[]
*/
public function getAll()
public function getAll(array $criteria = [])
{
$images = $this->adapter->get(sprintf('%s/images?per_page=%d', self::ENDPOINT, PHP_INT_MAX));
$query = sprintf('%s/images?per_page=%d', self::ENDPOINT, PHP_INT_MAX);

if (isset($criteria['type']) && in_array($criteria['type'], ['distribution', 'application'])) {
$query = sprintf('%s&type=%s', $query, $criteria['type']);
}

if (isset($criteria['private']) && true === (boolean) $criteria['private']) {
$query = sprintf('%s&private=true', $query);
}

$images = $this->adapter->get($query);
$images = json_decode($images);
$this->extractMeta($images);

return array_map(function ($image) {
return new ImageEntity($image);
}, $images->images);
return new ImageEntity($image);
}, $images->images);
}

/**
Expand Down

0 comments on commit f894dba

Please sign in to comment.