1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: dp |
5
|
|
|
* Date: 26.07.17 |
6
|
|
|
* Time: 11:56 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Lan\Ebs\Sdk\Classes; |
10
|
|
|
|
11
|
|
|
use ArrayObject; |
12
|
|
|
use Exception; |
13
|
|
|
use Lan\Ebs\Sdk\Client; |
14
|
|
|
use Lan\Ebs\Sdk\Common; |
15
|
|
|
use ReflectionClass; |
16
|
|
|
|
17
|
|
|
abstract class Collection extends ArrayObject implements Common |
18
|
|
|
{ |
19
|
|
|
private $client; |
20
|
|
|
|
21
|
|
|
private $loadStatus = 0; |
22
|
|
|
|
23
|
|
|
private $fields = []; |
24
|
|
|
|
25
|
|
|
private $class = null; |
26
|
|
|
|
27
|
|
|
private $limit = null; |
28
|
|
|
|
29
|
|
|
private $offset = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Collection constructor. |
33
|
|
|
* |
34
|
|
|
* @param Client $client |
35
|
|
|
* @param array $fields |
36
|
|
|
* @param string $class |
37
|
|
|
* @param int $limit |
38
|
|
|
* @param int $offset |
39
|
|
|
* @throws Exception |
40
|
|
|
*/ |
41
|
6 |
|
public function __construct(Client $client, array $fields, $class, $limit, $offset) |
42
|
|
|
{ |
43
|
6 |
|
if (!$client) { |
44
|
|
|
throw new Exception('Client not defined'); |
45
|
|
|
} |
46
|
|
|
|
47
|
6 |
|
if (!is_array($fields)) { |
48
|
|
|
throw new Exception('Fields for model of collection mast be array'); |
49
|
|
|
} |
50
|
|
|
|
51
|
6 |
|
$reflectionClass = new ReflectionClass($class); |
52
|
|
|
|
53
|
6 |
|
if (!$reflectionClass->isSubclassOf(Model::class)) { |
54
|
|
|
throw new Exception('Class of model collection not subclass for Model'); |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
$this->client = $client; |
58
|
6 |
|
$this->fields = $fields; |
59
|
6 |
|
$this->class = $class; |
60
|
|
|
|
61
|
6 |
|
$this->setLimit($limit); |
62
|
6 |
|
$this->setOffset($offset); |
63
|
6 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $limit |
67
|
|
|
*/ |
68
|
6 |
|
public function setLimit($limit) |
69
|
|
|
{ |
70
|
6 |
|
$this->limit = $limit; |
71
|
|
|
|
72
|
6 |
|
if ($this->loadStatus == 200) { |
73
|
|
|
$this->load(true); |
74
|
|
|
} |
75
|
6 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param bool $force |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
6 |
|
public function load($force = false) |
82
|
|
|
{ |
83
|
6 |
|
if ($this->loadStatus == 200 && !$force) { |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$params = [ |
88
|
6 |
|
'limit' => $this->limit, |
89
|
6 |
|
'offset' => $this->offset |
90
|
|
|
]; |
91
|
|
|
|
92
|
6 |
|
if ($this->fields) { |
|
|
|
|
93
|
|
|
$params['fields'] = implode(',', (array)$this->fields); |
94
|
|
|
} |
95
|
|
|
|
96
|
6 |
|
$response = $this->client->getResponse($this->getUrl(__FUNCTION__), $params); |
97
|
|
|
|
98
|
|
|
$this->exchangeArray($response['data']); |
99
|
|
|
|
100
|
|
|
$this->loadStatus = $response['status']; |
101
|
|
|
|
102
|
|
|
unset($response); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param int $offset |
109
|
|
|
*/ |
110
|
6 |
|
public function setOffset($offset) |
111
|
|
|
{ |
112
|
6 |
|
$this->offset = $offset; |
113
|
|
|
|
114
|
6 |
|
if ($this->loadStatus == 200) { |
115
|
|
|
$this->load(true); |
116
|
|
|
} |
117
|
6 |
|
} |
118
|
|
|
|
119
|
|
|
public function getIterator() |
120
|
|
|
{ |
121
|
|
|
return new CollectionIterator($this); |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
public function count() |
125
|
|
|
{ |
126
|
2 |
|
$this->load(); |
127
|
|
|
|
128
|
|
|
return parent::count(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getData() |
132
|
|
|
{ |
133
|
|
|
$this->load(); |
134
|
|
|
|
135
|
|
|
return $this->getArrayCopy(); |
136
|
|
|
} |
137
|
|
|
|
138
|
4 |
|
public function reset() |
139
|
|
|
{ |
140
|
4 |
|
$this->load(); |
141
|
|
|
|
142
|
|
|
return $this->createModel(reset($this)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array $data |
147
|
|
|
* @return Model |
148
|
|
|
*/ |
149
|
|
|
public function createModel(array $data = null) |
150
|
|
|
{ |
151
|
|
|
$class = $this->class; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @var Model $model |
155
|
|
|
*/ |
156
|
|
|
$model = new $class($this->client, $this->fields); |
157
|
|
|
|
158
|
|
|
$model->set($data === null ? current($this) : $data); |
159
|
|
|
|
160
|
|
|
return $model; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function end() |
164
|
|
|
{ |
165
|
|
|
$this->load(); |
166
|
|
|
|
167
|
|
|
return $this->createModel(end($this)); |
168
|
|
|
} |
169
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.