|
1
|
|
|
<?php namespace Fenos\Notifynder\Builder; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Config\Repository; |
|
4
|
|
|
use InvalidArgumentException; |
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class BuilderRules |
|
9
|
|
|
* |
|
10
|
|
|
* Simple trait that define the rules that |
|
11
|
|
|
* the builder has to match. It required mandatory |
|
12
|
|
|
* fields listed in the $requiredFields property |
|
13
|
|
|
* |
|
14
|
|
|
* @package Fenos\Notifynder\Builder |
|
15
|
|
|
*/ |
|
16
|
|
|
trait BuilderRules |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $requiredFields = ['from_id','to_id','url','category_id']; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Value has to be a string |
|
26
|
|
|
* |
|
27
|
|
|
* @param $value |
|
28
|
|
|
* @return bool |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function isString($value) |
|
31
|
|
|
{ |
|
32
|
|
|
if (! is_string($value)) { |
|
33
|
|
|
throw new InvalidArgumentException("The value Passed is not a string"); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Value has to be a valid Carbon Instance |
|
41
|
|
|
* |
|
42
|
|
|
* @param $value |
|
43
|
|
|
* @return bool | InvalidArgumentException |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function isCarbon($value) |
|
46
|
|
|
{ |
|
47
|
|
|
if($value instanceof Carbon) return true; |
|
48
|
|
|
|
|
49
|
|
|
throw new InvalidArgumentException("The value Passed has to be an instance of Carbon\Carbon"); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Value has to be numeric |
|
54
|
|
|
* |
|
55
|
|
|
* @param $value |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function isNumeric($value) |
|
59
|
|
|
{ |
|
60
|
|
|
if (! is_numeric($value)) { |
|
61
|
|
|
throw new InvalidArgumentException("The value Passed must be a number"); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns all required fields including the config ones |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getRequiredFields() |
|
73
|
|
|
{ |
|
74
|
|
|
if (property_exists($this,'config') && $this->config instanceof Repository) { |
|
75
|
|
|
$customRequiredFields = $this->config->get('notifynder.additional_fields.required'); |
|
|
|
|
|
|
76
|
|
|
} else { |
|
77
|
|
|
$customRequiredFields = []; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return array_unique($this->requiredFields + $customRequiredFields); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Check that the builder has |
|
85
|
|
|
* the required field to send the |
|
86
|
|
|
* notifications correctly |
|
87
|
|
|
* |
|
88
|
|
|
* @param $array |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function hasRequiredFields($array) |
|
92
|
|
|
{ |
|
93
|
|
|
foreach ($this->getRequiredFields() as $field) { |
|
94
|
|
|
if (! array_key_exists($field, $array)) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Check if is a required field |
|
104
|
|
|
* |
|
105
|
|
|
* @param $offset |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
|
|
public function isRequiredField($offset) |
|
109
|
|
|
{ |
|
110
|
|
|
return (in_array($offset,$this->getRequiredFields())); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Check if the array passed is |
|
115
|
|
|
* multidimensional |
|
116
|
|
|
* |
|
117
|
|
|
* @param $arr |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function isReadyArrToFormatInJson(array $arr) |
|
121
|
|
|
{ |
|
122
|
|
|
if ($this->isAssociativeArr($arr)) { |
|
123
|
|
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if (count($arr) > 0) { |
|
127
|
|
|
$error = "The 'extra' value must to be an associative array"; |
|
128
|
|
|
throw new InvalidArgumentException($error); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param array $arr |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function isAssociativeArr(array $arr) |
|
139
|
|
|
{ |
|
140
|
|
|
return array_keys($arr) !== range(0, count($arr) - 1); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Check if the array is |
|
145
|
|
|
* multidimensional |
|
146
|
|
|
* |
|
147
|
|
|
* @param $arr |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
|
View Code Duplication |
public function isMultidimensionalArray($arr) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
$rv = array_filter($arr, 'is_array'); |
|
153
|
|
|
if (count($rv) > 0) { |
|
154
|
|
|
return true; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: