Skip to content

Commit 8743ff2

Browse files
authored
Merge pull request #12 from nstack-io/develop
Develop -> Master
2 parents 556562e + 799ba84 commit 8743ff2

12 files changed

+440
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ $config = new \NStack\Config('APP_ID', 'REST_KEY');
3030
$nstack = new \NStack\NStack($config);
3131
```
3232

33-
## Features
33+
## 💡 Features
3434

3535
[x] Geographic continent
3636
[x] Geographic countries
@@ -42,7 +42,7 @@ $nstack = new \NStack\NStack($config);
4242
[x] Content Localize languages
4343
[x] Content Localize proposals
4444
[x] Content Files
45-
[ ] Content Collections
45+
[x] Content Collections
4646
[ ] Notify updates
4747
[ ] UGC pushlogs
4848
[ ] Valitors

src/Clients/CollectionsClient.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace NStack\Clients;
4+
5+
use NStack\Exceptions\FailedToParseException;
6+
use NStack\Models\IpAddress;
7+
8+
/**
9+
* Class CollectionsClient
10+
*
11+
* @package NStack\Clients
12+
* @author Tiago Araujo <tiar@nodesagency.com>
13+
*/
14+
class CollectionsClient extends NStackClient
15+
{
16+
/** @var string */
17+
protected $path = 'content/collections';
18+
19+
/**
20+
* view
21+
*
22+
* @param int $collectionId
23+
* @return array
24+
*/
25+
public function view(int $collectionId): array
26+
{
27+
$response = $this->client->get($this->buildPath($this->path . '/' . $collectionId));
28+
$contents = $response->getBody()->getContents();
29+
return json_decode($contents, true)['data'];
30+
}
31+
32+
/**
33+
* createItem
34+
*
35+
* @param int $collectionId
36+
* @param array $params
37+
* @return array
38+
*/
39+
public function createItem(int $collectionId, array $params): array
40+
{
41+
$response = $this->client->post($this->buildPath($this->path . '/' . $collectionId . '/items'), [
42+
'form_params' => $params
43+
]);
44+
$contents = $response->getBody()->getContents();
45+
return json_decode($contents, true)['data'];
46+
}
47+
48+
/**
49+
* deleteItem
50+
*
51+
* @param int $collectionId
52+
* @param int $itemId
53+
*/
54+
public function deleteItem(int $collectionId, int $itemId)
55+
{
56+
$this->client->delete($this->buildPath($this->path . '/' . $collectionId . '/items/' . $itemId));
57+
}
58+
59+
/**
60+
* updateItem
61+
*
62+
* @param int $collectionId
63+
* @param int $itemId
64+
* @param array $params
65+
* @return array
66+
*/
67+
public function updateItem(int $collectionId, int $itemId, array $params): array
68+
{
69+
$response = $this->client->post($this->buildPath($this->path . '/' . $collectionId . '/items/' . $itemId . '/update'), [
70+
'form_params' => $params
71+
]);
72+
$contents = $response->getBody()->getContents();
73+
return json_decode($contents, true)['data'];
74+
}
75+
76+
/**
77+
* viewItem
78+
*
79+
* @param int $collectionId
80+
* @param int $itemId
81+
* @return array
82+
*/
83+
public function viewItem(int $collectionId, int $itemId): array
84+
{
85+
$response = $this->client->get($this->buildPath($this->path . '/' . $collectionId . '/items/' . $itemId));
86+
$contents = $response->getBody()->getContents();
87+
return json_decode($contents, true)['data'];
88+
}
89+
}

src/Clients/IpAddressesClient.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function index(): IpAddress
3434
/**
3535
* show
3636
*
37-
*
3837
* @param String $ip
3938
* @return IpAddress
4039
* @throws FailedToParseException

tests/CollectionsTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace NStack\Tests;
4+
5+
use NStack\Clients\CollectionsClient;
6+
use NStack\Tests\objects\CollectionItem;
7+
use NStack\Tests\objects\CollectionShow1;
8+
use NStack\Tests\objects\CollectionShow2;
9+
10+
class CollectionsTest extends TestCase
11+
{
12+
public function testView()
13+
{
14+
$client = $this->getClientWithMockedGet('collection-show-1.json');
15+
$client = new CollectionsClient($this->getConfig(), $client);
16+
$entry = $client->view(10);
17+
foreach ($entry as $item) {
18+
$this->assertInstanceOf(CollectionShow1::class, new CollectionShow1($item));
19+
}
20+
21+
$client = $this->getClientWithMockedGet('collection-show-2.json');
22+
$client = new CollectionsClient($this->getConfig(), $client);
23+
$entry = $client->view(10);
24+
foreach ($entry as $item) {
25+
$this->assertInstanceOf(CollectionShow2::class, new CollectionShow2($item));
26+
}
27+
}
28+
29+
public function testCreateItem()
30+
{
31+
$client = $this->getClientWithMockedPost('collection-create-item.json');
32+
$client = new CollectionsClient($this->getConfig(), $client);
33+
$entry = $client->createItem(10, ["id" => 39, "key" => "41"]);
34+
$this->assertInstanceOf(CollectionItem::class, new CollectionItem($entry));
35+
}
36+
37+
public function testDeleteItem()
38+
{
39+
$mock = $this->getMockBuilder('CollectionsClient')
40+
->setMethods(array('delete'))
41+
->getMock();
42+
43+
$mock->expects($this->once())
44+
->method('delete');
45+
46+
$mock->delete(10, 15);
47+
}
48+
49+
public function testUpdateItem()
50+
{
51+
$client = $this->getClientWithMockedPost('collection-update-item.json');
52+
$client = new CollectionsClient($this->getConfig(), $client);
53+
$entry = $client->updateItem(10, 315, ["id" => 39, "key" => "50"]);
54+
$this->assertInstanceOf(CollectionItem::class, new CollectionItem($entry));
55+
}
56+
57+
public function testViewItem()
58+
{
59+
$client = $this->getClientWithMockedGet('collection-show-item.json');
60+
$client = new CollectionsClient($this->getConfig(), $client);
61+
$entry = $client->viewItem(39, 315);
62+
$this->assertInstanceOf(CollectionItem::class, new CollectionItem($entry));
63+
}
64+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"data": {
3+
"id": 315,
4+
"key": 41
5+
}
6+
}

tests/mocks/collection-show-1.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"data": [
3+
{
4+
"id": 10,
5+
"text": "1",
6+
"image": "http://d1gwekl0pol55k.cloudfront.net/nstack/images/original/collections/Pastedimageat2016_09_2204_38PM_M1HbuXPagn.png"
7+
}
8+
],
9+
"meta": {
10+
"pagination": {
11+
"total": 1,
12+
"count": 1,
13+
"per_page": 20,
14+
"current_page": 1,
15+
"total_pages": 1,
16+
"links": {}
17+
}
18+
}
19+
}

tests/mocks/collection-show-2.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"data": [
3+
{
4+
"id": 309,
5+
"Title": "Bread",
6+
"Description": "A delicious loaf of rye bread",
7+
"Price": 5
8+
},
9+
{
10+
"id": 310,
11+
"Title": "Water",
12+
"Description": "A 1l bottle of spring water",
13+
"Price": 2
14+
}
15+
],
16+
"meta": {
17+
"pagination": {
18+
"total": 2,
19+
"count": 2,
20+
"per_page": 20,
21+
"current_page": 1,
22+
"total_pages": 1,
23+
"links": {}
24+
}
25+
}
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"data": {
3+
"id": 315,
4+
"key": 1515151
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"data": {
3+
"id": 315,
4+
"key": 1515151
5+
}
6+
}

tests/objects/CollectionItem.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace NStack\Tests\objects;
4+
5+
use NStack\Models\Model;
6+
7+
/**
8+
* Class CollectionItem
9+
*
10+
* @package NStack\Tests\objects
11+
* @author Tiago Araujo <tiar@nodesagency.com>
12+
*/
13+
class CollectionItem extends Model
14+
{
15+
/** @var int */
16+
protected $id;
17+
18+
/** @var string */
19+
protected $key;
20+
21+
/**
22+
* parse
23+
*
24+
* @param array $data
25+
*/
26+
public function parse(array $data)
27+
{
28+
$this->id = (int)$data['id'];
29+
$this->key = (string)$data['key'];
30+
}
31+
32+
/**
33+
* toArray
34+
*
35+
* @return array
36+
*/
37+
public function toArray(): array
38+
{
39+
return [
40+
'id' => $this->id,
41+
'key' => $this->key,
42+
];
43+
}
44+
45+
/**
46+
* @return int
47+
*/
48+
public function getId(): int
49+
{
50+
return $this->id;
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getKey(): string
57+
{
58+
return $this->key;
59+
}
60+
61+
}

0 commit comments

Comments
 (0)