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
|
|
|
* @param Client $client |
34
|
|
|
* @param array $fields |
35
|
|
|
* @param string $class |
36
|
|
|
* @param int $limit |
37
|
|
|
* @param int $offset |
38
|
|
|
* @throws Exception |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Client $client, array $fields, $class, $limit, $offset) |
41
|
|
|
{ |
42
|
|
|
if (!$client) { |
43
|
|
|
throw new Exception('Client not defined'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!is_array($fields)) { |
47
|
|
|
throw new Exception('Fields for model of collection mast be array'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$reflectionClass = new ReflectionClass($class); |
51
|
|
|
|
52
|
|
|
if (!$reflectionClass->isSubclassOf(Model::class)) { |
53
|
|
|
throw new Exception('Class of model collection not subclass for Model'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->client = $client; |
57
|
|
|
$this->fields = $fields; |
58
|
|
|
$this->class = $class; |
59
|
|
|
|
60
|
|
|
$this->setLimit($limit); |
61
|
|
|
$this->setOffset($offset); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getIterator() |
65
|
|
|
{ |
66
|
|
|
return new CollectionIterator($this); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param bool $force |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function load($force = false) |
74
|
|
|
{ |
75
|
|
|
if ($this->loadStatus == 200 && !$force) { |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$params = [ |
80
|
|
|
'limit' => $this->limit, |
81
|
|
|
'offset' => $this->offset |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
if ($this->fields) { |
|
|
|
|
85
|
|
|
$params['fields'] = implode(',', (array) $this->fields); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$response = $this->client->getResponse($this->getUrl(__FUNCTION__), $params); |
89
|
|
|
|
90
|
|
|
$this->exchangeArray($response['data']); |
91
|
|
|
|
92
|
|
|
$this->loadStatus = $response['status']; |
93
|
|
|
|
94
|
|
|
unset($response); |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param int $limit |
101
|
|
|
*/ |
102
|
|
|
public function setLimit($limit) |
103
|
|
|
{ |
104
|
|
|
$this->limit = $limit; |
105
|
|
|
|
106
|
|
|
if ($this->loadStatus == 200) { |
107
|
|
|
$this->load(true); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param int $offset |
113
|
|
|
*/ |
114
|
|
|
public function setOffset($offset) |
115
|
|
|
{ |
116
|
|
|
$this->offset = $offset; |
117
|
|
|
|
118
|
|
|
if ($this->loadStatus == 200) { |
119
|
|
|
$this->load(true); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param array $data |
125
|
|
|
* @return Model |
126
|
|
|
*/ |
127
|
|
|
public function createModel(array $data = null) |
128
|
|
|
{ |
129
|
|
|
$class = $this->class; |
130
|
|
|
|
131
|
|
|
/** @var Model $model */ |
132
|
|
|
$model = new $class($this->client, $this->fields); |
133
|
|
|
|
134
|
|
|
$model->set($data === null ? current($this) : $data); |
135
|
|
|
|
136
|
|
|
return $model; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function count() { |
140
|
|
|
$this->load(); |
141
|
|
|
|
142
|
|
|
return parent::count(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getData() |
146
|
|
|
{ |
147
|
|
|
$this->load(); |
148
|
|
|
|
149
|
|
|
return $this->getArrayCopy(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function reset() |
153
|
|
|
{ |
154
|
|
|
$this->load(); |
155
|
|
|
|
156
|
|
|
return $this->createModel(reset($this)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function end() |
160
|
|
|
{ |
161
|
|
|
$this->load(); |
162
|
|
|
|
163
|
|
|
return $this->createModel(end($this)); |
164
|
|
|
} |
165
|
|
|
} |
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.