Skip to content

Commit

Permalink
Merge pull request #481 from ptmcnally/257-region-prices
Browse files Browse the repository at this point in the history
257 region prices
  • Loading branch information
ptmcnally authored Jun 1, 2022
2 parents d012fc2 + 3972020 commit b8faca3
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/eCloud/AvailabilityZoneClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UKFast\SDK\eCloud;

use UKFast\SDK\eCloud\Entities\Product;
use UKFast\SDK\Entities\ClientEntityInterface;
use UKFast\SDK\Traits\PageItems;
use UKFast\SDK\eCloud\Entities\AvailabilityZone;
Expand All @@ -28,4 +29,42 @@ public function getEntityMap()
'datacentre_site_id' => 'datacentreSiteId'
];
}

/**
* Get an array of all products and prices for the availability zone
*
* @param int $id Region ID
* @param array $filters
* @return array
*/
public function getProducts($id, $filters = [])
{
$page = $this->paginatedRequest($this->collectionPath . '/' . $id . '/prices', 1, 15, $filters);

if ($page->totalItems() == 0) {
return [];
}

$loadEntity = function ($data) {
return new Product($this->apiToFriendly($data, Product::$entityMap));
};

$page->serializeWith($loadEntity);

$items = $page->getItems();
if ($page->totalPages() == 1) {
return $items;
}

while ($page->pageNumber() < $page->totalPages()) {
$page = $this->getPage($page->pageNumber() + 1, 15, $filters);

$page->serializeWith($loadEntity);
$items = array_merge(
$items,
$page->getItems()
);
}
return $items;
}
}
24 changes: 24 additions & 0 deletions src/eCloud/Entities/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace UKFast\SDK\eCloud\Entities;

use UKFast\SDK\Entity;

/**
* @property string $availability_zone_id
* @property string $name
* @property string $category
* @property string $price
* @property string $rate
*/
class Product extends Entity
{
public static $entityMap = [
'availability_zone_id' => 'availabilityZoneId',
'name' => 'name',
'description' => 'description',
'category' => 'category',
'price' => 'price',
'rate' => 'rate',
];
}
39 changes: 39 additions & 0 deletions src/eCloud/RegionClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UKFast\SDK\eCloud;

use UKFast\SDK\eCloud\Entities\Product;
use UKFast\SDK\Entities\ClientEntityInterface;
use UKFast\SDK\Traits\PageItems;
use UKFast\SDK\eCloud\Entities\Region;
Expand All @@ -26,4 +27,42 @@ public function getEntityMap()
'name' => 'name',
];
}

/**
* Get an array of all products and prices for the region
*
* @param int $id Region ID
* @param array $filters
* @return array
*/
public function getProducts($id, $filters = [])
{
$page = $this->paginatedRequest($this->collectionPath . '/' . $id . '/prices', 1, 15, $filters);

if ($page->totalItems() == 0) {
return [];
}

$loadEntity = function ($data) {
return new Product($this->apiToFriendly($data, Product::$entityMap));
};

$page->serializeWith($loadEntity);

$items = $page->getItems();
if ($page->totalPages() == 1) {
return $items;
}

while ($page->pageNumber() < $page->totalPages()) {
$page = $this->getPage($page->pageNumber() + 1, 15, $filters);

$page->serializeWith($loadEntity);
$items = array_merge(
$items,
$page->getItems()
);
}
return $items;
}
}

0 comments on commit b8faca3

Please sign in to comment.