1 | <?php |
||||
2 | |||||
3 | |||||
4 | namespace ElePHPant\Trigger; |
||||
5 | |||||
6 | |||||
7 | /** |
||||
8 | * Class Trigger |
||||
9 | * @author Sérgio Danilo Jr. <https://github.com/sergiodanilojr> |
||||
10 | * @package Source\Support |
||||
11 | */ |
||||
12 | class Trigger |
||||
13 | { |
||||
14 | /** |
||||
15 | * @var int |
||||
16 | */ |
||||
17 | protected $timeOut = 5000; |
||||
18 | /** |
||||
19 | * @var array |
||||
20 | */ |
||||
21 | private $type = []; |
||||
22 | /** |
||||
23 | * @var array |
||||
24 | */ |
||||
25 | private $title = []; |
||||
26 | /** |
||||
27 | * @var array |
||||
28 | */ |
||||
29 | private $message = []; |
||||
30 | /** |
||||
31 | * @var array |
||||
32 | */ |
||||
33 | private $fields = []; |
||||
34 | |||||
35 | |||||
36 | /** |
||||
37 | * @return false|string |
||||
38 | */ |
||||
39 | public function __toString() |
||||
40 | { |
||||
41 | return json_encode($this->render(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
||||
42 | } |
||||
43 | |||||
44 | /** |
||||
45 | * @return mixed|null |
||||
46 | */ |
||||
47 | public function render(bool $unique = true) |
||||
48 | { |
||||
49 | $triggers = []; |
||||
50 | $index = -1; |
||||
51 | if ($this->message) { |
||||
0 ignored issues
–
show
|
|||||
52 | foreach ($this->message as $trigger) { |
||||
53 | $index++; |
||||
54 | $triggers[] = (object)[ |
||||
55 | "icon" => $this->types()[$index], |
||||
56 | "type" => $this->types()[$index], |
||||
57 | "title" => $this->titles()[$index], |
||||
58 | "message" => $trigger, |
||||
59 | "field" => ($this->fields()[$index] ?? null), |
||||
60 | "timeout" => $this->timeOut |
||||
61 | ]; |
||||
62 | } |
||||
63 | return ($unique ? $triggers[0] : $triggers); |
||||
64 | } |
||||
65 | return null; |
||||
66 | } |
||||
67 | |||||
68 | /** |
||||
69 | * @return array|null |
||||
70 | */ |
||||
71 | public function fields(): ?array |
||||
72 | { |
||||
73 | return ($this->fields ?? null); |
||||
74 | } |
||||
75 | |||||
76 | /** |
||||
77 | * @return array |
||||
78 | */ |
||||
79 | public function messages(): array |
||||
80 | { |
||||
81 | return $this->message; |
||||
82 | } |
||||
83 | |||||
84 | /** |
||||
85 | * @return array |
||||
86 | */ |
||||
87 | public function titles(): array |
||||
88 | { |
||||
89 | return $this->title; |
||||
90 | } |
||||
91 | |||||
92 | /** |
||||
93 | * @return array |
||||
94 | */ |
||||
95 | public function types(): array |
||||
96 | { |
||||
97 | return $this->type; |
||||
98 | } |
||||
99 | |||||
100 | /** |
||||
101 | * @param string $message |
||||
102 | * @param string|null $title |
||||
103 | * @param array|null $fields |
||||
104 | * @param int $timeOut |
||||
105 | * @return $this |
||||
106 | */ |
||||
107 | public function success(string $message, ?string $title = null, ?array $fields = null, int $timeOut = 5000): Trigger |
||||
108 | { |
||||
109 | $this->setTrigger(__FUNCTION__, $message, $title, $fields, $timeOut); |
||||
110 | return $this; |
||||
111 | } |
||||
112 | |||||
113 | /** |
||||
114 | * @param string $message |
||||
115 | * @param string|null $title |
||||
116 | * @param array|null $fields |
||||
117 | * @param int $timeOut |
||||
118 | * @return $this |
||||
119 | */ |
||||
120 | public function info(string $message, ?string $title = null, ?array $fields = null, int $timeOut = 5000): Trigger |
||||
121 | { |
||||
122 | $this->setTrigger(__FUNCTION__, $message, $title, $fields, $timeOut); |
||||
123 | return $this; |
||||
124 | } |
||||
125 | |||||
126 | /** |
||||
127 | * @param string $message |
||||
128 | * @param string|null $title |
||||
129 | * @param array|null $fields |
||||
130 | * @param int $timeOut |
||||
131 | * @return $this |
||||
132 | */ |
||||
133 | public function warning(string $message, ?string $title = null, ?array $fields = null, int $timeOut = 5000): Trigger |
||||
134 | { |
||||
135 | $this->setTrigger(__FUNCTION__, $message, $title, $fields, $timeOut); |
||||
136 | return $this; |
||||
137 | } |
||||
138 | |||||
139 | /** |
||||
140 | * @param string $message |
||||
141 | * @param string|null $title |
||||
142 | * @param array|null $fields |
||||
143 | * @param int $timeOut |
||||
144 | * @return $this |
||||
145 | */ |
||||
146 | public function error(string $message, ?string $title = null, ?array $fields = null, int $timeOut = 5000): Trigger |
||||
147 | { |
||||
148 | $this->setTrigger(__FUNCTION__, $message, $title, $fields, $timeOut); |
||||
149 | return $this; |
||||
150 | } |
||||
151 | |||||
152 | public function flash() |
||||
153 | { |
||||
154 | $_COOKIE['flash'] = $this->render(); |
||||
155 | } |
||||
156 | |||||
157 | /** |
||||
158 | * @param string $message |
||||
159 | * @return string |
||||
160 | */ |
||||
161 | protected function filter(string $message): string |
||||
162 | { |
||||
163 | return filter_var($message, FILTER_SANITIZE_STRIPPED); |
||||
164 | } |
||||
165 | |||||
166 | /** |
||||
167 | * @param array $fields |
||||
168 | */ |
||||
169 | protected function filterFields(array $fields): void |
||||
170 | { |
||||
171 | $fields = filter_var_array($fields, FILTER_SANITIZE_STRIPPED); |
||||
172 | |||||
173 | $fields = array_unique(array_merge($this->fields, $fields)); |
||||
0 ignored issues
–
show
It seems like
$fields can also be of type null ; however, parameter $arrays of array_merge() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
174 | $reset = array_values($fields); |
||||
175 | $this->fields = $reset; |
||||
176 | } |
||||
177 | |||||
178 | /** |
||||
179 | * @param string $trigger |
||||
180 | * @param string $message |
||||
181 | * @param string|null $title |
||||
182 | * @param array|null $fields |
||||
183 | * @param int $timeOut |
||||
184 | */ |
||||
185 | protected function setTrigger(string $trigger, string $message, ?string $title = null, ?array $fields = null, int $timeOut = 5000) |
||||
186 | { |
||||
187 | $this->message[] = $this->filter($message); |
||||
188 | $this->type[] = $trigger; |
||||
189 | $this->timeOut = $timeOut; |
||||
190 | |||||
191 | $this->title[] = ($title ? $this->filter($title) : null); |
||||
192 | |||||
193 | if ($fields) { |
||||
194 | $this->filterFields($fields); |
||||
195 | } |
||||
196 | } |
||||
197 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.