1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: dp |
5
|
|
|
* Date: 26.07.17 |
6
|
|
|
* Time: 12:30 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Lan\Ebs\Sdk\Classes; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use Lan\Ebs\Sdk\Client; |
13
|
|
|
use Lan\Ebs\Sdk\Common; |
14
|
|
|
|
15
|
|
|
abstract class Model implements Common |
16
|
|
|
{ |
17
|
|
|
const MESSAGE_ID_REQUIRED = 'Id is required'; |
18
|
|
|
const MESSAGE_ID_CAN_NOT_CHANGED = 'Id can not be changed'; |
19
|
|
|
|
20
|
|
|
private $client; |
21
|
|
|
|
22
|
|
|
private $fields = []; |
23
|
|
|
|
24
|
|
|
private $data = null; |
25
|
|
|
|
26
|
|
|
private $id = null; |
27
|
|
|
|
28
|
|
|
private $lastStatus = 0; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Model constructor. |
32
|
|
|
* |
33
|
|
|
* @param Client $client |
34
|
|
|
* @param $fields |
35
|
|
|
* @throws Exception |
36
|
|
|
*/ |
37
|
6 |
|
public function __construct(Client $client, array $fields) |
38
|
|
|
{ |
39
|
6 |
|
if (!$client) { |
40
|
|
|
throw new Exception('Client not defined'); |
41
|
|
|
} |
42
|
|
|
|
43
|
6 |
|
if (!is_array($fields)) { |
44
|
|
|
throw new Exception('Fields for model of collection mast be array'); |
45
|
|
|
} |
46
|
|
|
|
47
|
6 |
|
$this->client = $client; |
48
|
6 |
|
$this->fields = $fields; |
49
|
6 |
|
} |
50
|
|
|
|
51
|
2 |
|
public function setId($id) { |
52
|
2 |
|
return $this->set(['id' => $id]); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
public function get($id = null) |
56
|
|
|
{ |
57
|
2 |
|
if ($id === null && $this->id !== null) { |
58
|
|
|
return $this->data; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
$this->setId($id); |
62
|
|
|
|
63
|
|
|
$params = $this->fields ? ['fields' => implode(',', $this->fields)] : []; |
64
|
|
|
|
65
|
|
|
$response = $this->client->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]), $params); |
66
|
|
|
|
67
|
|
|
$this->set($response['data'], $response['status']); |
68
|
|
|
|
69
|
|
|
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
|
5 |
|
public function set(array $data, $status = null) |
81
|
|
|
{ |
82
|
5 |
|
if (empty($data['id']) && empty($this->id)) { |
83
|
4 |
|
throw new Exception(Model::MESSAGE_ID_REQUIRED); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
if (!empty($data['id']) && !empty($this->id) && $data['id'] != $this->id) { |
87
|
|
|
throw new Exception(Model::MESSAGE_ID_CAN_NOT_CHANGED); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$this->data = array_merge((array)$this->data, $data); |
91
|
|
|
|
92
|
1 |
|
$this->id = $this->data['id']; |
93
|
|
|
|
94
|
1 |
|
if ($status) { |
95
|
|
|
$this->lastStatus = $status; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getFields() |
102
|
|
|
{ |
103
|
|
|
return $this->fields; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function getId() |
107
|
|
|
{ |
108
|
1 |
|
return $this->id; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function post(array $data) |
112
|
|
|
{ |
113
|
1 |
|
$response = $this->client->getResponse($this->getUrl(__FUNCTION__), $data); |
114
|
|
|
|
115
|
|
|
$this->set($response['data'], $response['status']); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function put(array $data) |
121
|
|
|
{ |
122
|
|
|
$this->set($data); |
123
|
|
|
|
124
|
|
|
$response = $this->client->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]), $data); |
125
|
|
|
|
126
|
|
|
$this->set($response['data'], $response['status']); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function delete($id = null) |
132
|
|
|
{ |
133
|
|
|
if (empty($this->id)) { |
134
|
|
|
$this->set(['id' => $id]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$response = $this->client->getResponse($this->getUrl(__FUNCTION__, [$this->getId()])); |
138
|
|
|
|
139
|
|
|
$this->set($response['data'], $response['status']); |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function __get($name) |
145
|
|
|
{ |
146
|
|
|
$data = $this->get(); |
147
|
|
|
|
148
|
|
|
if (!array_key_exists($name, $data)) { |
149
|
|
|
throw new Exception('Param ' . $name . ' not defined for ' . get_class($this)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $data[$name]; |
153
|
|
|
} |
154
|
|
|
} |