1 | <?php |
||
23 | abstract class Model implements Common |
||
24 | { |
||
25 | const MESSAGE_ID_REQUIRED = 'Id is required'; |
||
26 | const MESSAGE_ID_CAN_NOT_CHANGED = 'Id can not be changed'; |
||
27 | |||
28 | /** |
||
29 | * Инстанс клиента API |
||
30 | * |
||
31 | * @var Client |
||
32 | */ |
||
33 | private $client; |
||
34 | |||
35 | /** |
||
36 | * Имена полей, подлежаших получению через API |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $fields = []; |
||
41 | |||
42 | /** |
||
43 | * Данные модели |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private $data = []; |
||
48 | |||
49 | /** |
||
50 | * Идентификатор модели |
||
51 | * |
||
52 | * @var null |
||
53 | */ |
||
54 | private $id = null; |
||
55 | |||
56 | /** |
||
57 | * Статус последнего обращения по API |
||
58 | * |
||
59 | * @var int |
||
60 | */ |
||
61 | private $lastStatus = 0; |
||
62 | |||
63 | /** |
||
64 | * Конструктор модели |
||
65 | * |
||
66 | * @param Client $client Инстанс клиента |
||
67 | * @param array $fields Поля для выборки |
||
68 | * |
||
69 | * @throws Exception |
||
70 | */ |
||
71 | 10 | public function __construct(Client $client, array $fields) |
|
84 | |||
85 | /** |
||
86 | * Загружаемые поля модели |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | public function getFields() |
||
94 | |||
95 | /** |
||
96 | * Добавление новой записи по API |
||
97 | * |
||
98 | * @param array $data Устанавливаемые данные модели |
||
99 | * |
||
100 | * @return $this |
||
101 | * |
||
102 | * @throws Exception |
||
103 | */ |
||
104 | 1 | public function post(array $data) |
|
105 | { |
||
106 | 1 | $response = $this->getClient()->getResponse($this->getUrl(__FUNCTION__), $data); |
|
107 | |||
108 | 1 | $this->set($response['data'], $response['status']); |
|
109 | |||
110 | 1 | return $this; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Получение инстанса клиента |
||
115 | * |
||
116 | * @return Client |
||
117 | */ |
||
118 | 5 | protected function getClient() |
|
122 | |||
123 | /** |
||
124 | * Установка данных модели |
||
125 | * |
||
126 | * @param array $data Данные модели |
||
127 | * @param int $status Статус полученных данных |
||
128 | * |
||
129 | * @return $this |
||
130 | * |
||
131 | * @throws Exception |
||
132 | */ |
||
133 | 10 | public function set(array $data, $status = null) |
|
134 | { |
||
135 | 10 | if (empty($data['id']) && empty($this->getId())) { |
|
136 | 2 | throw new Exception(Model::MESSAGE_ID_REQUIRED); |
|
137 | } |
||
138 | |||
139 | 8 | if (!empty($data['id']) && !empty($this->getId()) && $data['id'] != $this->getId()) { |
|
140 | throw new Exception(Model::MESSAGE_ID_CAN_NOT_CHANGED); |
||
141 | } |
||
142 | |||
143 | 8 | if (!empty($data['id'])) { |
|
144 | 8 | $this->setId($data['id']); |
|
145 | } |
||
146 | |||
147 | 8 | $this->data = array_merge((array)$this->data, $data); |
|
148 | |||
149 | 8 | if ($status) { |
|
|
|||
150 | 5 | $this->lastStatus = $status; |
|
151 | } |
||
152 | |||
153 | 8 | return $this; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * Получение идентификатора модели |
||
158 | * |
||
159 | * @return int |
||
160 | */ |
||
161 | 10 | public function getId() |
|
165 | |||
166 | /** |
||
167 | * Установка идентификатора модели |
||
168 | * |
||
169 | * @param int $id Идентификатор модели |
||
170 | * |
||
171 | * @return int |
||
172 | * |
||
173 | * @throws Exception |
||
174 | */ |
||
175 | 8 | public function setId($id) |
|
179 | |||
180 | /** |
||
181 | * Обновление записи по API |
||
182 | * |
||
183 | * @param array $data Обновляемые данные |
||
184 | * |
||
185 | * @return $this |
||
186 | * |
||
187 | * @throws Exception |
||
188 | */ |
||
189 | 1 | public function put(array $data) |
|
199 | |||
200 | /** |
||
201 | * Удаление модели |
||
202 | * |
||
203 | * @param int $id Идентификатор модели |
||
204 | * |
||
205 | * @return $this |
||
206 | * |
||
207 | * @throws Exception |
||
208 | */ |
||
209 | 1 | public function delete($id = null) |
|
221 | |||
222 | /** |
||
223 | * Магический Get |
||
224 | * |
||
225 | * @param mixed $name Имя поля |
||
226 | * |
||
227 | * @return mixed |
||
228 | * |
||
229 | * @throws Exception |
||
230 | */ |
||
231 | 4 | public function __get($name) |
|
241 | |||
242 | /** |
||
243 | * Получение метаданных по идентификатору модели |
||
244 | * |
||
245 | * @param int $id Идентификатор модели |
||
246 | * |
||
247 | * @return array |
||
248 | * |
||
249 | * @throws Exception |
||
250 | */ |
||
251 | 5 | public function get($id = null) |
|
271 | } |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: