1 | <?php |
||
9 | abstract class Model implements Common |
||
10 | { |
||
11 | const MESSAGE_ID_REQUIRED = 'Id is required'; |
||
12 | const MESSAGE_ID_CAN_NOT_CHANGED = 'Id can not be changed'; |
||
13 | |||
14 | private $client; |
||
15 | |||
16 | private $fields = []; |
||
17 | |||
18 | private $data = null; |
||
19 | |||
20 | private $id = null; |
||
21 | |||
22 | private $lastStatus = 0; |
||
23 | |||
24 | /** |
||
25 | * Model constructor. |
||
26 | * |
||
27 | * @param Client $client |
||
28 | * @param $fields |
||
29 | * @throws Exception |
||
30 | */ |
||
31 | 6 | public function __construct(Client $client, array $fields) |
|
44 | |||
45 | /** |
||
46 | * @return array |
||
47 | */ |
||
48 | public function getFields() |
||
52 | |||
53 | /** |
||
54 | * @param array $data |
||
55 | * @return $this |
||
56 | * @throws Exception |
||
57 | */ |
||
58 | 1 | public function post(array $data) |
|
66 | |||
67 | /** |
||
68 | * Set data to model |
||
69 | * |
||
70 | * @param array $data |
||
71 | * @param null $status |
||
72 | * @return $this |
||
73 | * @throws Exception |
||
74 | */ |
||
75 | 3 | public function set(array $data, $status = null) |
|
76 | { |
||
77 | 3 | if (empty($data['id']) && empty($this->getId())) { |
|
78 | 2 | throw new Exception(Model::MESSAGE_ID_REQUIRED); |
|
79 | } |
||
80 | |||
81 | 1 | if (!empty($data['id']) && !empty($this->getId()) && $data['id'] != $this->getId()) { |
|
82 | throw new Exception(Model::MESSAGE_ID_CAN_NOT_CHANGED); |
||
83 | } |
||
84 | |||
85 | 1 | if (!empty($data['id'])) { |
|
86 | 1 | $this->setId($data['id']); |
|
87 | 1 | } |
|
88 | |||
89 | 1 | $this->data = array_merge((array)$this->data, $data); |
|
90 | |||
91 | 1 | if ($status) { |
|
92 | $this->lastStatus = $status; |
||
93 | } |
||
94 | |||
95 | 1 | return $this; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param array $data |
||
100 | * @return $this |
||
101 | * @throws Exception |
||
102 | */ |
||
103 | public function put(array $data) |
||
113 | |||
114 | /** |
||
115 | * @return null |
||
116 | */ |
||
117 | 5 | public function getId() |
|
121 | |||
122 | /** |
||
123 | * @param $id |
||
124 | * @return Model |
||
125 | * @throws Exception |
||
126 | */ |
||
127 | 1 | public function setId($id) |
|
131 | |||
132 | /** |
||
133 | * @param null $id |
||
134 | * @return $this |
||
135 | * @throws Exception |
||
136 | */ |
||
137 | public function delete($id = null) |
||
138 | { |
||
139 | if (empty($this->getId())) { |
||
140 | $this->set(['id' => $id]); |
||
141 | } |
||
142 | |||
143 | $response = $this->getClient()->getResponse($this->getUrl(__FUNCTION__, [$this->getId()])); |
||
144 | |||
145 | $this->set($response['data'], $response['status']); |
||
146 | |||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param $name |
||
152 | * @return mixed |
||
153 | * @throws Exception |
||
154 | */ |
||
155 | public function __get($name) |
||
165 | |||
166 | /** |
||
167 | * @param null $id |
||
168 | * @return null |
||
169 | * @throws Exception |
||
170 | */ |
||
171 | 2 | public function get($id = null) |
|
191 | |||
192 | /** |
||
193 | * @return Client |
||
194 | */ |
||
195 | 1 | protected function getClient() |
|
199 | } |