1 | <?php |
||
17 | abstract class Model implements Common |
||
18 | { |
||
19 | const MESSAGE_ID_REQUIRED = 'Id is required'; |
||
20 | const MESSAGE_ID_CAN_NOT_CHANGED = 'Id can not be changed'; |
||
21 | |||
22 | private $client; |
||
23 | |||
24 | private $fields = []; |
||
25 | |||
26 | private $data = null; |
||
27 | |||
28 | private $id = null; |
||
29 | |||
30 | private $lastStatus = 0; |
||
31 | |||
32 | /** |
||
33 | * Model constructor. |
||
34 | * |
||
35 | * @param Client $client |
||
36 | * @param $fields |
||
37 | * @throws Exception |
||
38 | */ |
||
39 | 8 | public function __construct(Client $client, array $fields) |
|
52 | |||
53 | 2 | public function setId($id) { |
|
56 | |||
57 | 2 | public function get($id = null) |
|
58 | { |
||
59 | 2 | if ($id === null && $this->id !== null) { |
|
60 | 1 | return $this->data; |
|
61 | } |
||
62 | |||
63 | 2 | $this->setId($id); |
|
64 | |||
65 | 1 | $response = $this->client->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]), ['fields' => implode(',', $this->getFields())]); |
|
66 | |||
67 | 1 | $this->set($response['data'], $response['status']); |
|
68 | |||
69 | 1 | return $this->data; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Set data to model |
||
74 | * |
||
75 | * @param array $data |
||
76 | * @param null $status |
||
77 | * @return $this |
||
78 | * @throws Exception |
||
79 | */ |
||
80 | 7 | public function set(array $data, $status = null) |
|
81 | { |
||
82 | 7 | if (empty($data['id']) && empty($this->id)) { |
|
83 | 4 | throw new Exception(Model::MESSAGE_ID_REQUIRED); |
|
84 | } |
||
85 | |||
86 | 4 | if (!empty($data['id']) && !empty($this->id) && $data['id'] != $this->id) { |
|
87 | throw new Exception(Model::MESSAGE_ID_CAN_NOT_CHANGED); |
||
88 | } |
||
89 | |||
90 | 4 | $this->data = array_merge( |
|
91 | 4 | (array)$this->data, |
|
92 | 4 | array_intersect_key($data, array_flip(array_merge(['id'], $this->getFields()))) |
|
93 | 4 | ); |
|
94 | |||
95 | 4 | $this->id = $this->data['id']; |
|
96 | |||
97 | 4 | if ($status) { |
|
98 | 1 | $this->lastStatus = $status; |
|
99 | 1 | } |
|
100 | |||
101 | 4 | return $this; |
|
102 | } |
||
103 | |||
104 | 4 | public function getFields() |
|
105 | { |
||
106 | 4 | $class = get_class($this); |
|
107 | |||
108 | 4 | return $this->fields ? $this->fields : $class::$defaultFields; |
|
109 | } |
||
110 | |||
111 | 4 | public function getId() |
|
112 | { |
||
113 | 4 | return $this->id; |
|
114 | } |
||
115 | |||
116 | 1 | public function post(array $data) |
|
117 | { |
||
118 | 1 | $response = $this->client->getResponse($this->getUrl(__FUNCTION__), $data); |
|
119 | |||
120 | $this->set($response['data'], $response['status']); |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | public function put(array $data) |
||
135 | |||
136 | public function delete($id = null) |
||
137 | { |
||
138 | if (empty($this->id)) { |
||
139 | $this->set(['id' => $id]); |
||
140 | } |
||
148 | |||
149 | 1 | public function __get($name) |
|
159 | } |