PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.97
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.97
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Http / Request / InputHelperMethodsTrait.php

InputHelperMethodsTrait.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.97, at vendor/wpfluent/framework/src/WPFluent/Http/Request/InputHelperMethodsTrait.php

224 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Http\Request;
4
5 use FluentCommunity\Framework\Support\Arr;
6
7 trait InputHelperMethodsTrait
8 {
9 /**
10 * Get an item from the request filtering by the callback
11 *
12 * @param string|array|null $key
13 * @param callable $callback
14 * @param mixed $default
15 * @return mixed
16 */
17 public function getSafe($key, $callback = null, $default = null)
18 {
19 $array = $result = [];
20
21 $expectsArray = true;
22
23 if(!is_array($key)) {
24 $key = [$key];
25 $expectsArray = false;
26 }
27
28 // Normalize all to ['field' => ['cb1', 'cb2']] style array
29 if ($callback) {
30 $callback = is_array($callback) ? $callback : [$callback];
31 foreach ($key as $k => $field) {
32 $array[$field] = $callback;
33 if (str_contains($field, '*')) {
34 $array = $this->substituteWildcardKeys($array, $field);
35 }
36 }
37 } else {
38 foreach ($key as $k => $v) {
39 // Add a simple closure to normalize when
40 // there's no callback given for a field
41 if (is_int($k)) {
42 $k = $v;
43 $v = function($v) { return $v; };
44 }
45
46 $array[$k] = is_array($v) ? $v : [$v];
47
48 $array = $this->substituteWildcardKeys($array, $k);
49 }
50 }
51
52 // Sanitize all the fields using given callbacks
53 foreach ($array as $field => $callbacks) {
54 // In case someone used 'cb1|cb2|cb3' style callbacks
55 $callbacks = $this->mayBeFixCallbacks($callbacks);
56
57 if (($value = $this->get($field, $default)) !== null) {
58 $callbacks = is_callable($callbacks) ? [$callbacks] : $callbacks;
59
60 while ($callback = array_shift($callbacks)) {
61 if (is_array($value)) {
62 $value = array_map($callback, $value);
63 } else {
64 $value = $callback($value);
65 }
66 }
67
68 Arr::set($result, $field, $value);
69 }
70 }
71
72 // Return the first item if only one item in the array
73 // because some one asked for one field, otherwise all.
74 return $expectsArray ? $result : reset($result);
75 }
76
77 /**
78 * Normalize wildcard rules to dotted rule, i.e:
79 * key_one.*.key_two.*.key_three becomes:
80 * key_one.0.key_two.0.key_three.0
81 * key_one.0.key_two.1.key_three.0
82 * depending on the data array.
83 *
84 * @param array $array
85 * @param string $field
86 * @return array
87 */
88 protected function substituteWildcardKeys($array, $field)
89 {
90 $callback = $array[$field];
91
92 $keys = array_map(function($v) {
93 return trim($v, '.');
94 }, explode('*', $field));
95
96 $key = array_shift($keys);
97
98 if ($key && ($data = $this->get($key)) && is_array($data)) {
99
100 $dotted = array_keys(Arr::dot($data, $key . '.'));
101
102 foreach ($dotted as $dottedField) {
103
104 $r = preg_replace('/[0-9]+/', '*', $dottedField);
105
106 if (preg_match("/{$field}/", $r)) {
107
108 $array[$dottedField] = $callback;
109
110 if (isset($array[$field])) {
111 unset($array[$field]);
112 }
113 }
114 }
115 }
116
117 return $array;
118 }
119
120 /**
121 * Check and fix if callbacks are given
122 * as: callback1|callback2\callback3.
123 *
124 * @param array|string $callbacks
125 * @return array
126 */
127 protected function mayBeFixCallbacks($callbacks)
128 {
129 $nonFunctionCallables = $functionCallables = [];
130
131 foreach ($callbacks as $cb) {
132 if (is_callable($cb)) {
133 $nonFunctionCallables[] = $cb;
134 } elseif (is_string($cb)) {
135 $functionCallables[] = explode('|', $cb);
136 } elseif (is_array($cb) && !str_contains($cb[0], '::')) {
137 $functionCallables[] = $cb[0];
138 } elseif (is_array($cb) && str_contains($cb[0], '::')) {
139 $nonFunctionCallables[] = explode('::', $cb[0]);
140 }
141 }
142
143 $callbacks = array_merge(
144 Arr::flatten($functionCallables), $nonFunctionCallables
145 );
146
147 return $callbacks;
148 }
149
150 /**
151 * Returns an sanitized integer
152 * @param string $key
153 * @param string $dafault
154 * @return [type] using intval function
155 */
156 public function getInt($key, $dafault = null)
157 {
158 return intval($this->get($key, $dafault));
159 }
160
161 /**
162 * Retrieve input as a float value.
163 *
164 * @param string|null $key
165 * @param float $default
166 * @return float
167 */
168 public function getFloat($key, $default = null)
169 {
170 return floatval($this->get($key, $default));
171 }
172
173 /**
174 * Returns a sanitized string
175 * @param string $key
176 * @param string $dafault
177 * @return string using sanitize_text_field function
178 */
179 public function getText($key, $dafault = null)
180 {
181 return sanitize_text_field($this->get($key, $dafault));
182 }
183
184 /**
185 * Returns a string as title
186 * @param string $key
187 * @param string $dafault
188 * @return string using sanitize_title function
189 */
190 public function getTitle($key, $dafault = null)
191 {
192 return sanitize_title($this->get($key, $dafault));
193 }
194
195 /**
196 * Returns sanitized email
197 *
198 * @param string $key
199 * @param string $dafault
200 * @return string using sanitize_email function
201 */
202 public function getEmail($key, $dafault = null)
203 {
204 return sanitize_email($this->get($key, $dafault));
205 }
206
207 /**
208 * Returns boolean value
209 *
210 * @param string $key
211 * @param string $dafault
212 * @return bool TRUE for "1", "true", "on" and "yes"
213 * @return bool FALSE for "0", "false", "off" and "no"
214 */
215 public function getBool($key, $dafault = null)
216 {
217 return filter_var(
218 $this->get($key, $dafault),
219 FILTER_VALIDATE_BOOLEAN,
220 FILTER_NULL_ON_FAILURE
221 );
222 }
223 }
224