|
1
|
|
|
<?php namespace Fenos\Notifynder\Builder; |
|
2
|
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
|
4
|
|
|
use Carbon\Carbon; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class BuilderRules |
|
8
|
|
|
* |
|
9
|
|
|
* Simple trait that define the rules that |
|
10
|
|
|
* the builder has to match. It required mandatory |
|
11
|
|
|
* fields listed in the $requiredFields property |
|
12
|
|
|
* |
|
13
|
|
|
* @package Fenos\Notifynder\Builder |
|
14
|
|
|
*/ |
|
15
|
|
|
trait BuilderRules |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private $requiredFields = ['from_id','to_id','url','category_id']; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Value has to be a string |
|
25
|
|
|
* |
|
26
|
|
|
* @param $value |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function isString($value) |
|
30
|
|
|
{ |
|
31
|
|
|
if (! is_string($value)) { |
|
32
|
|
|
throw new InvalidArgumentException("The value Passed is not a string"); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return true; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Value has to be a valid Carbon Instance |
|
40
|
|
|
* |
|
41
|
|
|
* @param $value |
|
42
|
|
|
* @return bool | InvalidArgumentException |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function isCarbon($value) |
|
45
|
|
|
{ |
|
46
|
|
|
if($value instanceof Carbon) return true; |
|
47
|
|
|
|
|
48
|
|
|
throw new InvalidArgumentException("The value Passed has to be an instance of Carbon\Carbon"); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Value has to be numeric |
|
53
|
|
|
* |
|
54
|
|
|
* @param $value |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function isNumeric($value) |
|
58
|
|
|
{ |
|
59
|
|
|
if (! is_numeric($value)) { |
|
60
|
|
|
throw new InvalidArgumentException("The value Passed must be a number"); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns all required fields including the config ones |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getRequiredFields() |
|
72
|
|
|
{ |
|
73
|
|
|
return array_unique($this->requiredFields + config('notifynder.additional_fields.required')); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Check that the builder has |
|
78
|
|
|
* the required field to send the |
|
79
|
|
|
* notifications correctly |
|
80
|
|
|
* |
|
81
|
|
|
* @param $array |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public function hasRequiredFields($array) |
|
85
|
|
|
{ |
|
86
|
|
|
foreach ($this->getRequiredFields() as $field) { |
|
87
|
|
|
if (! array_key_exists($field, $array)) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Check if is a required field |
|
97
|
|
|
* |
|
98
|
|
|
* @param $offset |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function isRequiredField($offset) |
|
102
|
|
|
{ |
|
103
|
|
|
return (in_array($offset,$this->getRequiredFields())); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Check if the array passed is |
|
108
|
|
|
* multidimensional |
|
109
|
|
|
* |
|
110
|
|
|
* @param $arr |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function isReadyArrToFormatInJson(array $arr) |
|
114
|
|
|
{ |
|
115
|
|
|
if ($this->isAssociativeArr($arr)) { |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (count($arr) > 0) { |
|
120
|
|
|
$error = "The 'extra' value must to be an associative array"; |
|
121
|
|
|
throw new InvalidArgumentException($error); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param array $arr |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function isAssociativeArr(array $arr) |
|
132
|
|
|
{ |
|
133
|
|
|
return array_keys($arr) !== range(0, count($arr) - 1); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Check if the array is |
|
138
|
|
|
* multidimensional |
|
139
|
|
|
* |
|
140
|
|
|
* @param $arr |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
View Code Duplication |
public function isMultidimensionalArray($arr) |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
$rv = array_filter($arr, 'is_array'); |
|
146
|
|
|
if (count($rv) > 0) { |
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.