1 | <?php |
||
9 | abstract class DataTransferObject |
||
10 | { |
||
11 | /** |
||
12 | * DTO attributes. |
||
13 | * |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $attributes = []; |
||
17 | |||
18 | /** |
||
19 | * DTO validation rules. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $rules = []; |
||
24 | |||
25 | /** |
||
26 | * Validation errors bag. |
||
27 | * |
||
28 | * @var \Illuminate\Support\MessageBag |
||
29 | */ |
||
30 | protected $errors; |
||
31 | |||
32 | /** |
||
33 | * Get DTO attribute value. |
||
34 | * |
||
35 | * @param $name |
||
36 | * |
||
37 | * @return mixed |
||
38 | */ |
||
39 | public function __get($name) |
||
47 | |||
48 | /** |
||
49 | * Set DTO attribute value. |
||
50 | * |
||
51 | * @param $name |
||
52 | * @param $value |
||
53 | */ |
||
54 | public function __set($name, $value) |
||
58 | |||
59 | /** |
||
60 | * Check if DTO attribute exists. |
||
61 | * |
||
62 | * @param $name |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function __isset($name) |
||
70 | |||
71 | /** |
||
72 | * Call virtual getter or setter. |
||
73 | * |
||
74 | * @param $method |
||
75 | * @param $arguments |
||
76 | * |
||
77 | * @throws DtoMethodNotFoundException |
||
78 | * |
||
79 | * @return $this|mixed |
||
80 | */ |
||
81 | public function __call($method, $arguments) |
||
91 | |||
92 | /** |
||
93 | * Creates new DTO instance from given array. |
||
94 | * |
||
95 | * @param array $data |
||
96 | * |
||
97 | * @return static |
||
98 | */ |
||
99 | public static function fromArray(array $data) |
||
109 | |||
110 | /** |
||
111 | * @param string $method |
||
112 | * |
||
113 | * @return mixed |
||
114 | */ |
||
115 | protected function virtualGetter(string $method) |
||
121 | |||
122 | /** |
||
123 | * @param string $method |
||
124 | * @param array $arguments |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | protected function virtualSetter(string $method, array $arguments) |
||
136 | |||
137 | /** |
||
138 | * Check if given attribute exists in current DTO. |
||
139 | * |
||
140 | * @param string $attribute |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | public function hasAttribute(string $attribute): bool |
||
148 | |||
149 | /** |
||
150 | * Check if given attribute exists in current DTO & has not-NULL value. |
||
151 | * |
||
152 | * @param string $attribute |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | public function hasNotNullAttribute(string $attribute): bool |
||
160 | |||
161 | /** |
||
162 | * Get attribute value. |
||
163 | * |
||
164 | * @param string $attribute |
||
165 | * |
||
166 | * @return mixed |
||
167 | */ |
||
168 | public function getAttribute(string $attribute) |
||
172 | |||
173 | /** |
||
174 | * Validates DTO. |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function validate(): bool |
||
193 | |||
194 | /** |
||
195 | * Get validation errors bag. |
||
196 | * |
||
197 | * @return \Illuminate\Support\MessageBag |
||
198 | */ |
||
199 | public function errors() |
||
203 | |||
204 | /** |
||
205 | * Validation rules. |
||
206 | * |
||
207 | * @return array |
||
208 | */ |
||
209 | public function rules() |
||
213 | |||
214 | /** |
||
215 | * Set validation rules from external source. |
||
216 | * |
||
217 | * @param array $rules |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function setRules(array $rules) |
||
227 | |||
228 | /** |
||
229 | * Merge existed validation rules with given rules. |
||
230 | * |
||
231 | * @param array $rules |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function mergeRules(array $rules) |
||
241 | } |
||
242 |