Skip to content

Commit 9bb5a70

Browse files
authored
Merge pull request #138 from Gman98ish/filter-properties
Friendly-name filter properties
2 parents 624b2ae + 08b6d2a commit 9bb5a70

File tree

7 files changed

+145
-179
lines changed

7 files changed

+145
-179
lines changed

src/Client.php

+40
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,44 @@ protected function getUserAgent()
243243
'php/'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION
244244
]);
245245
}
246+
247+
/**
248+
* @param array|Entity $item
249+
* @param array $map
250+
* @return array
251+
*/
252+
public function apiToFriendly($item, $map)
253+
{
254+
$newItem = [];
255+
if ($item instanceof Entity) {
256+
$item = $item->toArray();
257+
}
258+
foreach ($item as $apiName => $value) {
259+
$filter = "";
260+
if (strpos($apiName, ':') !== false) {
261+
list($apiName, $filter) = explode(":", $apiName);
262+
$filter = ":$filter";
263+
}
264+
265+
$apiName = $apiName.$filter;
266+
if (isset($map[$apiName])) {
267+
$newItem[$map[$apiName]] = $value;
268+
continue;
269+
}
270+
271+
$newItem[$apiName] = $value;
272+
}
273+
274+
return $newItem;
275+
}
276+
277+
/**
278+
* @param array|Entity $item
279+
* @param array $map
280+
* @return array
281+
*/
282+
public function friendlyToApi($item, $map)
283+
{
284+
return $this->apiToFriendly($item, array_flip($map));
285+
}
246286
}

src/Entity.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace UKFast\SDK;
44

5+
use DateTime;
6+
57
abstract class Entity
68
{
79
/**
810
* @var array
911
*/
1012
protected $attributes = [];
1113

14+
protected $dates = [];
15+
1216
/**
1317
* @param array $attributes
1418
*/
@@ -17,8 +21,8 @@ public function __construct($attributes = [])
1721
if (is_object($attributes)) {
1822
$attributes = (array) $attributes;
1923
}
20-
21-
$this->attributes = $attributes;
24+
25+
$this->fill($attributes);
2226
}
2327

2428
/**
@@ -48,6 +52,10 @@ public function get($attr, $default = null)
4852
*/
4953
public function set($attr, $value)
5054
{
55+
if (in_array($attr, $this->dates)) {
56+
$value = DateTime::createFromFormat(DateTime::ISO8601, $value);
57+
}
58+
5159
$this->attributes[$attr] = $value;
5260
}
5361

@@ -68,7 +76,7 @@ public function all()
6876
public function fill($attributes)
6977
{
7078
foreach ($attributes as $name => $value) {
71-
$this->attributes[$name] = $value;
79+
$this->set($name, $value);
7280
}
7381
}
7482

src/PSS/Entities/Author.php

+8-27
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,15 @@
22

33
namespace UKFast\SDK\PSS\Entities;
44

5-
class Author
5+
use UKFast\SDK\Entity;
6+
7+
/**
8+
* @property int $id
9+
* @property string $name
10+
* @property string $type
11+
*/
12+
class Author extends Entity
613
{
7-
/**
8-
* @var int
9-
*/
10-
public $id;
11-
12-
/**
13-
* @var string
14-
*/
15-
public $name;
16-
17-
/**
18-
* @var string
19-
*/
20-
public $type;
21-
22-
public function __construct($item = null)
23-
{
24-
if (is_null($item)) {
25-
return;
26-
}
27-
28-
$this->id = $item->id;
29-
$this->name = $item->name;
30-
$this->type = $item->type;
31-
}
32-
3314
/**
3415
* @return bool
3516
*/

src/PSS/Entities/Request.php

+25-91
Original file line numberDiff line numberDiff line change
@@ -2,98 +2,32 @@
22

33
namespace UKFast\SDK\PSS\Entities;
44

5-
class Request
5+
use UKFast\SDK\Entity;
6+
7+
/**
8+
* @property int $id
9+
* @property \UKFast\SDK\PSS\Entities\Author $author
10+
* @property string $type
11+
* @property string $subject
12+
* @property bool $secure
13+
* @property \DateTime $createdAt
14+
* @property string $priority
15+
* @property bool $archived
16+
* @property string $status
17+
* @property bool $requestSms
18+
* @property string $customerReference
19+
* @property \UKFast\SDK\PSS\Entities\Product $product
20+
* @property \DateTime $lastRepliedAt
21+
* @property string $systemReference
22+
* @property string $details
23+
* @property array $cc
24+
* @property int $unreadReplies
25+
* @property string $contactMethod
26+
*/
27+
class Request extends Entity
628
{
7-
/**
8-
* @var int
9-
*/
10-
public $id;
11-
12-
/**
13-
* @var \UKFast\SDK\Pss\Entities\Author
14-
*/
15-
public $author;
16-
17-
/**
18-
* @var string
19-
*/
20-
public $type;
21-
22-
/**
23-
* @var string
24-
*/
25-
public $subject;
26-
27-
/**
28-
* @var bool
29-
*/
30-
public $secure = true;
31-
32-
/**
33-
* @var \Datetime
34-
*/
35-
public $createdAt;
36-
37-
/**
38-
* @var string
39-
*/
40-
public $priority = 'Normal';
41-
42-
/**
43-
* @var bool
44-
*/
45-
public $archived;
46-
47-
/**
48-
* @var string
49-
*/
50-
public $status;
51-
52-
/**
53-
* @var bool
54-
*/
55-
public $requestSms = false;
56-
57-
/**
58-
* @var string
59-
*/
60-
public $customerReference;
61-
62-
/**
63-
* @var \UKFast\SDK\Pss\Entities\Author
64-
*/
65-
public $product;
66-
67-
/**
68-
* @var \DateTime
69-
*/
70-
public $lastRepliedAt;
71-
72-
/**
73-
* @var string
74-
*/
75-
public $systemReference;
76-
77-
/**
78-
* @var string
79-
*/
80-
public $details;
81-
82-
/**
83-
* @var array
84-
*/
85-
public $cc = [];
86-
87-
/**
88-
* @var integer
89-
*/
90-
public $unreadReplies;
91-
92-
/**
93-
* @var string
94-
*/
95-
public $contactMethod;
96-
29+
protected $dates = ['createdAt', 'lastRepliedAt'];
30+
9731
public function isCompleted()
9832
{
9933
return in_array($this->status, ['Completed', 'Replied and Completed']);

src/PSS/RequestClient.php

+22-57
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ class RequestClient extends BaseClient
1010
{
1111
protected $basePath = 'pss/';
1212

13+
protected $requestMap = [
14+
'created_at' => 'createdAt',
15+
'request_sms' => 'requestSms',
16+
'customer_reference' => 'customerReference',
17+
'last_replied_at' => 'lastRepliedAt',
18+
'system_reference' => 'systemReference',
19+
'unread_replies' => 'unreadReplies',
20+
'contact_method' => 'contactMethod',
21+
];
22+
1323
/**
1424
* Gets a paginated response of all PSS requests
1525
*
@@ -20,6 +30,8 @@ class RequestClient extends BaseClient
2030
*/
2131
public function getPage($page = 1, $perPage = 15, $filters = [])
2232
{
33+
$filters = $this->friendlyToApi($filters, $this->requestMap);
34+
2335
$page = $this->paginatedRequest('v1/requests', $page, $perPage, $filters);
2436
$page->serializeWith(function ($item) {
2537
return $this->serializeRequest($item);
@@ -138,31 +150,9 @@ public function getFeedback($id)
138150
*/
139151
protected function serializeRequest($item)
140152
{
141-
$request = new Entities\Request;
142-
143-
$request->id = $item->id;
144-
$request->author = new Entities\Author($item->author);
145-
$request->type = $item->type;
146-
$request->secure = $item->secure;
147-
$request->subject = $item->subject;
148-
$request->createdAt = DateTime::createFromFormat(DateTime::ISO8601, $item->created_at);
149-
$request->priority = $item->priority;
150-
$request->archived = $item->archived;
151-
$request->status = $item->status;
152-
$request->requestSms = $item->request_sms;
153-
$request->customerReference = $item->customer_reference;
154-
$request->product = new Entities\Product($item->product);
155-
$request->lastRepliedAt = null;
156-
$request->systemReference = $item->system_reference;
157-
$request->unreadReplies = $item->unread_replies;
158-
$request->contactMethod = $item->contact_method;
159-
if ($item->last_replied_at) {
160-
$request->lastRepliedAt = DateTime::createFromFormat(DateTime::ISO8601, $item->last_replied_at);
161-
}
162-
163-
if (!empty($item->cc)) {
164-
$request->cc = $item->cc;
165-
}
153+
$request = new Entities\Request($this->apiToFriendly($item, $this->requestMap));
154+
$request->author = new Entities\Author($item);
155+
$request->product = new Entities\Product($item);
166156

167157
return $request;
168158
}
@@ -184,41 +174,16 @@ public function serializeFeedback($item)
184174

185175
protected function requestToJson($request)
186176
{
187-
$payload = [
188-
'subject' => $request->subject,
189-
'details' => $request->details,
190-
'priority' => $request->priority,
191-
'secure' => $request->secure,
192-
'author' => [
193-
'id' => $request->author->id
194-
],
195-
'request_sms' => $request->requestSms,
196-
'archived' => $request->archived,
197-
];
198-
199-
if (isset($request->status)) {
200-
$payload['status'] = $request->status;
201-
}
177+
$json = $this->friendlyToApi($request, $this->requestMap);
202178

203-
if ($request->product) {
204-
$payload['product'] = [
205-
'type' => $request->product->type,
206-
'id' => $request->product->id,
207-
];
179+
if ($request->author) {
180+
$json['author'] = $request->author->toArray();
208181
}
209182

210-
if (!empty($request->cc)) {
211-
$payload['cc'] = $request->cc;
212-
}
213-
214-
if (!empty($request->contactMethod)) {
215-
$payload['contact_method'] = $request->contactMethod;
216-
}
217-
218-
if ($request->customerReference) {
219-
$payload['customer_reference'] = $request->customerReference;
183+
if ($request->product) {
184+
$json['product'] = $request->product->toArray();
220185
}
221-
222-
return json_encode($payload);
186+
187+
return json_encode($json);
223188
}
224189
}

src/eCloud/Entities/Iops.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use UKFast\SDK\Entity;
66

77
/**
8-
* @property int $id
8+
* @property string $id
99
* @property string $name
1010
* @property int $limit
1111
*/

0 commit comments

Comments
 (0)