1 | <?php |
||
24 | class NotifynderBuilder implements ArrayAccess |
||
25 | { |
||
26 | use BuilderRules; |
||
27 | |||
28 | /** |
||
29 | * @var string notification to store |
||
30 | */ |
||
31 | public $date; |
||
32 | |||
33 | /** |
||
34 | * Builder data. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $notifications = []; |
||
39 | |||
40 | /** |
||
41 | * @var Repository |
||
42 | */ |
||
43 | protected $config; |
||
44 | |||
45 | /** |
||
46 | * @var NotifynderCategory |
||
47 | */ |
||
48 | private $notifynderCategory; |
||
49 | |||
50 | /** |
||
51 | * @param NotifynderCategory $notifynderCategory |
||
52 | */ |
||
53 | public function __construct(NotifynderCategory $notifynderCategory) |
||
54 | { |
||
55 | $this->notifynderCategory = $notifynderCategory; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Set who will send the notification. |
||
60 | * |
||
61 | * @return $this |
||
62 | */ |
||
63 | public function from() |
||
64 | { |
||
65 | $from = func_get_args(); |
||
66 | |||
67 | $this->setEntityAction($from, 'from'); |
||
68 | |||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Set who will receive the notification. |
||
74 | * |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function to() |
||
78 | { |
||
79 | $from = func_get_args(); |
||
80 | |||
81 | $this->setEntityAction($from, 'to'); |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Set the url of the notification. |
||
88 | * |
||
89 | * @param $url |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function url($url) |
||
93 | { |
||
94 | $this->isString($url); |
||
95 | |||
96 | $this->setBuilderData('url', $url); |
||
97 | |||
98 | return $this; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Set expire time. |
||
103 | * |
||
104 | * @param $datetime |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function expire($datetime) |
||
114 | |||
115 | /** |
||
116 | * Set Category and covert it, to the id |
||
117 | * if name of it given. |
||
118 | * |
||
119 | * @param $category |
||
120 | * @return $this |
||
121 | */ |
||
122 | public function category($category) |
||
133 | |||
134 | /** |
||
135 | * Set extra value. |
||
136 | * |
||
137 | * @param $extra |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function extra(array $extra = []) |
||
141 | { |
||
142 | $this->isReadyArrToFormatInJson($extra); |
||
143 | |||
144 | $jsonExtraValues = json_encode($extra); |
||
145 | |||
146 | $this->setBuilderData('extra', $jsonExtraValues); |
||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Set additional fields value. |
||
153 | * |
||
154 | * @param $key |
||
155 | * @param $value |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function setField($key, $value) |
||
164 | |||
165 | /** |
||
166 | * Build the array with the builder inside |
||
167 | * a Closure, it has more flexibility for |
||
168 | * the generation of your array. |
||
169 | * |
||
170 | * |
||
171 | * @param callable|Closure $closure |
||
172 | * @return array|false |
||
173 | * @throws NotificationBuilderException |
||
174 | */ |
||
175 | public function raw(Closure $closure) |
||
185 | |||
186 | /** |
||
187 | * Loop the data for create |
||
188 | * multi notifications array. |
||
189 | * |
||
190 | * @param $dataToIterate |
||
191 | * @param Closure $builder |
||
192 | * @return $this |
||
193 | * @throws \Fenos\Notifynder\Exceptions\IterableIsEmptyException |
||
194 | * @throws \Fenos\Notifynder\Exceptions\EntityNotIterableException |
||
195 | */ |
||
196 | public function loop($dataToIterate, Closure $builder) |
||
197 | { |
||
198 | if (! $this->isIterable($dataToIterate)) { |
||
199 | throw new EntityNotIterableException('The data passed must be iterable'); |
||
200 | } |
||
201 | if (count($dataToIterate) <= 0) { |
||
202 | throw new IterableIsEmptyException('The Iterable passed must contain at least one element'); |
||
203 | } |
||
204 | |||
205 | $notifications = []; |
||
206 | |||
207 | $newBuilder = new self($this->notifynderCategory); |
||
208 | |||
209 | foreach ($dataToIterate as $key => $data) { |
||
210 | $builder($newBuilder, $data, $key); |
||
211 | $notifications[] = $newBuilder->toArray(); |
||
212 | } |
||
213 | |||
214 | $this->notifications = $notifications; |
||
215 | |||
216 | return $this; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Compose the builder to |
||
221 | * the array. |
||
222 | * |
||
223 | * @throws NotificationBuilderException |
||
224 | * @return mixed |
||
225 | */ |
||
226 | public function toArray() |
||
257 | |||
258 | /** |
||
259 | * Refresh the state of the notifications. |
||
260 | */ |
||
261 | public function refresh() |
||
267 | |||
268 | /** |
||
269 | * @param $var |
||
270 | * @return bool |
||
271 | */ |
||
272 | protected function isIterable($var) |
||
276 | |||
277 | /** |
||
278 | * It set the entity who will do |
||
279 | * the action of receive or |
||
280 | * send. |
||
281 | * |
||
282 | * @param $from |
||
283 | * @param $property |
||
284 | * @return array |
||
285 | */ |
||
286 | protected function setEntityAction($from, $property) |
||
304 | |||
305 | /** |
||
306 | * If the values passed are 2 or more, |
||
307 | * it means that you specified the entity |
||
308 | * over then the id. |
||
309 | * |
||
310 | * @param array $info |
||
311 | * @return bool |
||
312 | */ |
||
313 | protected function hasEntity(array $info) |
||
317 | |||
318 | /** |
||
319 | * Set date on the array. |
||
320 | */ |
||
321 | protected function setDate() |
||
328 | |||
329 | /** |
||
330 | * @return string |
||
331 | */ |
||
332 | protected function getDate() |
||
336 | |||
337 | /** |
||
338 | * Set builder Data. |
||
339 | * |
||
340 | * @param $field |
||
341 | * @param $data |
||
342 | */ |
||
343 | public function setBuilderData($field, $data) |
||
347 | |||
348 | /** |
||
349 | * @param mixed $offset |
||
350 | * @return bool |
||
351 | */ |
||
352 | public function offsetExists($offset) |
||
356 | |||
357 | /** |
||
358 | * @param mixed $offset |
||
359 | * @return mixed |
||
360 | */ |
||
361 | public function offsetGet($offset) |
||
365 | |||
366 | /** |
||
367 | * @param mixed $offset |
||
368 | * @param mixed $value |
||
369 | */ |
||
370 | public function offsetSet($offset, $value) |
||
380 | |||
381 | /** |
||
382 | * @param Repository $config |
||
383 | */ |
||
384 | public function setConfig(Repository $config) |
||
388 | |||
389 | /** |
||
390 | * @param mixed $offset |
||
391 | * @return null |
||
392 | */ |
||
393 | public function offsetUnset($offset) |
||
397 | } |
||
398 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..