1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace prgTW\BaseCRM\Resource; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
6
|
|
|
use prgTW\BaseCRM\Resource\Partial\CreateResource; |
7
|
|
|
use prgTW\BaseCRM\Transport\Transport; |
8
|
|
|
|
9
|
|
|
abstract class ListResource extends Resource implements \ArrayAccess, \IteratorAggregate, \Countable |
10
|
|
|
{ |
11
|
|
|
use CreateResource; |
12
|
|
|
|
13
|
|
|
/** @var ResourceCollection */ |
14
|
|
|
protected $collection; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
private $query; |
18
|
|
|
|
19
|
|
|
/** {@inheritdoc} */ |
20
|
|
|
public function __construct(Transport $transport, $uri) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($transport, $uri); |
23
|
|
|
$this->reset(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function reset() |
27
|
|
|
{ |
28
|
|
|
$this->collection = null; |
29
|
|
|
$this->query = []; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $id |
34
|
|
|
* |
35
|
|
|
* @return InstanceResource |
36
|
|
|
*/ |
37
|
|
|
public function get($id) |
38
|
|
|
{ |
39
|
|
|
$instanceClassName = Inflector::singularize(static::class); |
40
|
|
|
$uri = sprintf('%s/%s', $this->uri, $id); |
41
|
|
|
/** @var InstanceResource $resource */ |
42
|
|
|
$resource = new $instanceClassName($this->transport, $uri); |
43
|
|
|
|
44
|
|
|
return $resource; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $id |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function delete($id) |
53
|
|
|
{ |
54
|
|
|
$uri = $this->getFullUri(sprintf('/%d', $id)); |
55
|
|
|
$result = $this->transport->delete($uri); |
56
|
|
|
|
57
|
|
|
return in_array($result, [null, true], true); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $query |
62
|
|
|
* |
63
|
|
|
* @return ResourceCollection |
64
|
|
|
*/ |
65
|
|
|
public function all($query = []) |
66
|
|
|
{ |
67
|
|
|
if (null !== $this->collection && $this->query === $query) |
68
|
|
|
{ |
69
|
|
|
return $this->collection; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$uri = $this->getFullUri(); |
73
|
|
|
$options = [] === $query ? [] : ['query' => $query]; |
74
|
|
|
$data = $this->transport->get($uri, null, $options); |
75
|
|
|
|
76
|
|
|
$this->query = $options; |
77
|
|
|
$this->collection = $this->postAll($data); |
|
|
|
|
78
|
|
|
|
79
|
|
|
return $this->collection; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** {@inheritdoc} */ |
83
|
|
|
public function count() |
84
|
|
|
{ |
85
|
|
|
$collection = $this->all(); |
86
|
|
|
|
87
|
|
|
return count($collection); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return ResourceCollection |
92
|
|
|
*/ |
93
|
|
|
public function getIterator() |
94
|
|
|
{ |
95
|
|
|
$collection = $this->all(); |
96
|
|
|
|
97
|
|
|
return $collection; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $data |
102
|
|
|
* |
103
|
|
|
* @return ResourceCollection |
104
|
|
|
*/ |
105
|
|
|
protected function postAll(array $data) |
106
|
|
|
{ |
107
|
|
|
$childResourceName = $this->getChildResourceName(); |
108
|
|
|
|
109
|
|
|
foreach ($data as $key => $resourceData) |
110
|
|
|
{ |
111
|
|
|
$data[$key] = $this->getObjectFromArray($resourceData[$childResourceName]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return new ResourceCollection($data, $childResourceName); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** {@inheritdoc} */ |
118
|
|
|
public function offsetExists($offset) |
119
|
|
|
{ |
120
|
|
|
$this->all($this->query); |
121
|
|
|
|
122
|
|
|
return $this->collection->offsetExists($offset); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** {@inheritdoc} */ |
126
|
|
|
public function offsetGet($offset) |
127
|
|
|
{ |
128
|
|
|
$this->all($this->query); |
129
|
|
|
|
130
|
|
|
return $this->collection->offsetGet($offset); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** {@inheritdoc} */ |
134
|
|
|
public function offsetSet($offset, $value) |
135
|
|
|
{ |
136
|
|
|
$this->all($this->query); |
137
|
|
|
|
138
|
|
|
$this->collection->offsetSet($offset, $value); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** {@inheritdoc} */ |
142
|
|
|
public function offsetUnset($offset) |
143
|
|
|
{ |
144
|
|
|
$this->all($this->query); |
145
|
|
|
|
146
|
|
|
return $this->collection->offsetUnset($offset); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.