|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace neilherbertuk\celcatwebapi\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use neilherbertuk\celcatwebapi\CelcatWebAPI; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait GetResourcesTrait |
|
9
|
|
|
* @package neilherbertuk\celcatwebapi\Traits |
|
10
|
|
|
*/ |
|
11
|
|
|
trait GetResourcesTrait |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $celcatWebAPI; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $parameters = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* GetResourcesTrait constructor. |
|
25
|
|
|
* @param CelcatWebAPI $celcatWebAPI |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(CelcatWebAPI $celcatWebAPI) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->celcatWebAPI = $celcatWebAPI; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get Operator - Performs a single request for a resource |
|
34
|
|
|
* |
|
35
|
|
|
* @param int $pageSize |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
* @throws \ReflectionException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function get($parameters = []) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->parameters = array_merge($this->parameters, $parameters); |
|
42
|
|
|
if (empty($this->parameters['pageSize'])) { |
|
43
|
|
|
$this->parameters['pageSize'] = 50; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->celcatWebAPI->log()->info('Getting '.(new \ReflectionClass($this))->getShortName()); |
|
47
|
|
|
return $this->celcatWebAPI->get((empty($this->name) ? (new \ReflectionClass($this))->getShortName() : $this->name), $this->parameters); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* GetAll Operator - Uses the get operator and pagination results to request all results for a resource |
|
52
|
|
|
* |
|
53
|
|
|
* @param int $pageSize |
|
54
|
|
|
* @return array|mixed |
|
55
|
|
|
* @throws \ReflectionException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getAll($parameters = []) |
|
58
|
|
|
{ |
|
59
|
|
|
|
|
60
|
|
|
$this->parameters = array_merge($this->parameters, $parameters); |
|
61
|
|
|
if (empty($this->parameters['pageSize'])) { |
|
62
|
|
|
$this->parameters['pageSize'] = 1000; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$results = $this->get(); |
|
66
|
|
|
|
|
67
|
|
|
if ($this->hasPagination($results)) { |
|
68
|
|
|
for ($page = 1; $page < $results['pagination']['TotalPages']; $page++) { |
|
69
|
|
|
$this->parameters['page'] = $page; |
|
70
|
|
|
$pageResults = $this->get(); |
|
71
|
|
|
|
|
72
|
|
|
$results['pagination']['CurrentPage'] = $pageResults['parameters']['page'] + 1; |
|
73
|
|
|
unset($pageResults['pagination'], $pageResults['parameters']); |
|
74
|
|
|
$results = array_merge_recursive($results, $pageResults); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
return $results; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* First Operator - Requests the first element of a resource |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
* @throws \ReflectionException |
|
85
|
|
|
*/ |
|
86
|
|
|
public function first() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->parameters['pageSize'] = 1; |
|
89
|
|
|
$this->parameters['page'] = 0; |
|
90
|
|
|
return $this->get(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Where Operator - Chainable method to add parameters to requests |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $parameters |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
|
|
public function where($parameters = []) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->parameters = array_merge($this->parameters, $parameters); |
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Does the result have pagination? |
|
107
|
|
|
* |
|
108
|
|
|
* @param $results |
|
109
|
|
|
* @return bool |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function hasPagination($results) |
|
112
|
|
|
{ |
|
113
|
|
|
return !empty($results['pagination']['TotalPages']); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|