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) |
||
108 | { |
||
109 | $this->isCarbon($datetime); |
||
110 | $this->setBuilderData('expire_time', $datetime); |
||
111 | |||
112 | return $this; |
||
113 | } |
||
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) |
||
123 | { |
||
124 | if (! is_numeric($category)) { |
||
125 | $category = $this->notifynderCategory |
||
126 | ->findByName($category)->id; |
||
127 | } |
||
128 | |||
129 | $this->setBuilderData('category_id', $category); |
||
130 | |||
131 | return $this; |
||
132 | } |
||
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 | * Build the array with the builder inside |
||
153 | * a Closure, it has more flexibility for |
||
154 | * the generation of your array. |
||
155 | * |
||
156 | * |
||
157 | * @param callable|Closure $closure |
||
158 | * @return array|false |
||
159 | * @throws NotificationBuilderException |
||
160 | */ |
||
161 | public function raw(Closure $closure) |
||
162 | { |
||
163 | $builder = $closure($this); |
||
164 | |||
165 | if (! is_null($builder)) { |
||
166 | return $this->toArray(); |
||
167 | } |
||
168 | |||
169 | return false; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Loop the data for create |
||
174 | * multi notifications array. |
||
175 | * |
||
176 | * @param $dataToIterate |
||
177 | * @param Closure $builder |
||
178 | * @return $this |
||
179 | * @throws \Fenos\Notifynder\Exceptions\IterableIsEmptyException |
||
180 | * @throws \Fenos\Notifynder\Exceptions\EntityNotIterableException |
||
181 | */ |
||
182 | public function loop($dataToIterate, Closure $builder) |
||
183 | { |
||
184 | if (! $this->isIterable($dataToIterate)) { |
||
185 | throw new EntityNotIterableException('The data passed must be iterable'); |
||
186 | } |
||
187 | if (count($dataToIterate) <= 0) { |
||
188 | throw new IterableIsEmptyException('The Iterable passed must contain at least one element'); |
||
189 | } |
||
190 | |||
191 | $notifications = []; |
||
192 | |||
193 | $newBuilder = new self($this->notifynderCategory); |
||
194 | |||
195 | foreach ($dataToIterate as $key => $data) { |
||
196 | $builder($newBuilder, $data, $key); |
||
197 | $notifications[] = $newBuilder->toArray(); |
||
198 | } |
||
199 | |||
200 | $this->notifications = $notifications; |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Compose the builder to |
||
207 | * the array. |
||
208 | * |
||
209 | * @throws NotificationBuilderException |
||
210 | * @return mixed |
||
211 | */ |
||
212 | public function toArray() |
||
213 | { |
||
214 | $hasMultipleNotifications = $this->isMultidimensionalArray($this->notifications); |
||
215 | |||
216 | // If the builder is handling a single notification |
||
217 | // we will validate only it |
||
218 | if (! $hasMultipleNotifications) { |
||
219 | $this->setDate(); |
||
220 | |||
221 | if ($this->hasRequiredFields($this->notifications)) { |
||
222 | return $this->notifications; |
||
223 | } |
||
224 | } |
||
225 | |||
226 | // If has multiple Notifications |
||
227 | // we will validate one by one |
||
228 | if ($hasMultipleNotifications) { |
||
229 | $allow = []; |
||
230 | |||
231 | foreach ($this->notifications as $index => $notification) { |
||
232 | $allow[$index] = $this->hasRequiredFields($notification); |
||
233 | } |
||
234 | |||
235 | if (! in_array(false, $allow)) { |
||
236 | return $this->notifications; |
||
237 | } |
||
238 | } |
||
239 | |||
240 | $error = 'The fields: '.implode(',', $this->getRequiredFields()).' are required'; |
||
241 | throw new NotificationBuilderException($error); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Refresh the state of the notifications. |
||
246 | */ |
||
247 | public function refresh() |
||
253 | |||
254 | /** |
||
255 | * @param $var |
||
256 | * @return bool |
||
257 | */ |
||
258 | protected function isIterable($var) |
||
259 | { |
||
260 | return is_array($var) || $var instanceof Traversable; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * It set the entity who will do |
||
265 | * the action of receive or |
||
266 | * send. |
||
267 | * |
||
268 | * @param $from |
||
269 | * @param $property |
||
270 | * @return array |
||
271 | */ |
||
272 | protected function setEntityAction($from, $property) |
||
273 | { |
||
274 | // Check if has the entity as parameter |
||
275 | // it should be the firstOne |
||
276 | if ($this->hasEntity($from)) { |
||
277 | $this->isString($from[0]); |
||
278 | $this->isNumeric($from[1]); |
||
279 | |||
280 | $this->setBuilderData("{$property}_type", $from[0]); |
||
281 | $this->setBuilderData("{$property}_id", $from[1]); |
||
282 | } elseif ($from[0] instanceof Model) { |
||
283 | $this->setBuilderData("{$property}_type", $from[0]->getMorphClass()); |
||
284 | $this->setBuilderData("{$property}_id", $from[0]->getKey()); |
||
285 | } else { |
||
286 | $this->isNumeric($from[0]); |
||
287 | $this->setBuilderData("{$property}_id", $from[0]); |
||
288 | } |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * If the values passed are 2 or more, |
||
293 | * it means that you specified the entity |
||
294 | * over then the id. |
||
295 | * |
||
296 | * @param array $info |
||
297 | * @return bool |
||
298 | */ |
||
299 | protected function hasEntity(array $info) |
||
300 | { |
||
301 | return count($info) >= 2; |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Set date on the array. |
||
306 | */ |
||
307 | protected function setDate() |
||
308 | { |
||
309 | $this->date = $data = Carbon::now(); |
||
|
|||
310 | |||
311 | $this->setBuilderData('updated_at', $data); |
||
312 | $this->setBuilderData('created_at', $data); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @return string |
||
317 | */ |
||
318 | protected function getDate() |
||
319 | { |
||
320 | return $this->date; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Set builder Data. |
||
325 | * |
||
326 | * @param $field |
||
327 | * @param $data |
||
328 | */ |
||
329 | public function setBuilderData($field, $data) |
||
330 | { |
||
331 | return $this->notifications[$field] = $data; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @param mixed $offset |
||
336 | * @return bool |
||
337 | */ |
||
338 | public function offsetExists($offset) |
||
342 | |||
343 | /** |
||
344 | * @param mixed $offset |
||
345 | * @return mixed |
||
346 | */ |
||
347 | public function offsetGet($offset) |
||
351 | |||
352 | /** |
||
353 | * @param mixed $offset |
||
354 | * @param mixed $value |
||
355 | */ |
||
356 | public function offsetSet($offset, $value) |
||
366 | |||
367 | /** |
||
368 | * @param Repository $config |
||
369 | */ |
||
370 | public function setConfig(Repository $config) |
||
374 | |||
375 | /** |
||
376 | * @param mixed $offset |
||
377 | * @return null |
||
378 | */ |
||
379 | public function offsetUnset($offset) |
||
383 | } |
||
384 |
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..