Skip to content

Commit b8faca3

Browse files
authored
Merge pull request #481 from ptmcnally/257-region-prices
257 region prices
2 parents d012fc2 + 3972020 commit b8faca3

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

src/eCloud/AvailabilityZoneClient.php

+39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace UKFast\SDK\eCloud;
44

5+
use UKFast\SDK\eCloud\Entities\Product;
56
use UKFast\SDK\Entities\ClientEntityInterface;
67
use UKFast\SDK\Traits\PageItems;
78
use UKFast\SDK\eCloud\Entities\AvailabilityZone;
@@ -28,4 +29,42 @@ public function getEntityMap()
2829
'datacentre_site_id' => 'datacentreSiteId'
2930
];
3031
}
32+
33+
/**
34+
* Get an array of all products and prices for the availability zone
35+
*
36+
* @param int $id Region ID
37+
* @param array $filters
38+
* @return array
39+
*/
40+
public function getProducts($id, $filters = [])
41+
{
42+
$page = $this->paginatedRequest($this->collectionPath . '/' . $id . '/prices', 1, 15, $filters);
43+
44+
if ($page->totalItems() == 0) {
45+
return [];
46+
}
47+
48+
$loadEntity = function ($data) {
49+
return new Product($this->apiToFriendly($data, Product::$entityMap));
50+
};
51+
52+
$page->serializeWith($loadEntity);
53+
54+
$items = $page->getItems();
55+
if ($page->totalPages() == 1) {
56+
return $items;
57+
}
58+
59+
while ($page->pageNumber() < $page->totalPages()) {
60+
$page = $this->getPage($page->pageNumber() + 1, 15, $filters);
61+
62+
$page->serializeWith($loadEntity);
63+
$items = array_merge(
64+
$items,
65+
$page->getItems()
66+
);
67+
}
68+
return $items;
69+
}
3170
}

src/eCloud/Entities/Product.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace UKFast\SDK\eCloud\Entities;
4+
5+
use UKFast\SDK\Entity;
6+
7+
/**
8+
* @property string $availability_zone_id
9+
* @property string $name
10+
* @property string $category
11+
* @property string $price
12+
* @property string $rate
13+
*/
14+
class Product extends Entity
15+
{
16+
public static $entityMap = [
17+
'availability_zone_id' => 'availabilityZoneId',
18+
'name' => 'name',
19+
'description' => 'description',
20+
'category' => 'category',
21+
'price' => 'price',
22+
'rate' => 'rate',
23+
];
24+
}

src/eCloud/RegionClient.php

+39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace UKFast\SDK\eCloud;
44

5+
use UKFast\SDK\eCloud\Entities\Product;
56
use UKFast\SDK\Entities\ClientEntityInterface;
67
use UKFast\SDK\Traits\PageItems;
78
use UKFast\SDK\eCloud\Entities\Region;
@@ -26,4 +27,42 @@ public function getEntityMap()
2627
'name' => 'name',
2728
];
2829
}
30+
31+
/**
32+
* Get an array of all products and prices for the region
33+
*
34+
* @param int $id Region ID
35+
* @param array $filters
36+
* @return array
37+
*/
38+
public function getProducts($id, $filters = [])
39+
{
40+
$page = $this->paginatedRequest($this->collectionPath . '/' . $id . '/prices', 1, 15, $filters);
41+
42+
if ($page->totalItems() == 0) {
43+
return [];
44+
}
45+
46+
$loadEntity = function ($data) {
47+
return new Product($this->apiToFriendly($data, Product::$entityMap));
48+
};
49+
50+
$page->serializeWith($loadEntity);
51+
52+
$items = $page->getItems();
53+
if ($page->totalPages() == 1) {
54+
return $items;
55+
}
56+
57+
while ($page->pageNumber() < $page->totalPages()) {
58+
$page = $this->getPage($page->pageNumber() + 1, 15, $filters);
59+
60+
$page->serializeWith($loadEntity);
61+
$items = array_merge(
62+
$items,
63+
$page->getItems()
64+
);
65+
}
66+
return $items;
67+
}
2968
}

0 commit comments

Comments
 (0)