PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.7
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.7
2.11.0 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 All 78 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 2.7.7, at vendor/wpfluent/framework/src/WPFluent/Http/Request/InputHelperMethodsTrait.php

226 lines 5.8 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 use FluentCommunity\Framework\Support\Sanitizer;
7
8 trait InputHelperMethodsTrait
9 {
10 /**
11 * Get an item from the request filtering by the callback(s).
12 *
13 * @param string|array|null $key
14 * @param callable|array|string|null $callback
15 * @param mixed $default
16 * @return mixed
17 */
18 public function getSafe($key, $callback = null, $default = null)
19 {
20 $originalKey = $key;
21 $array = $result = [];
22 $expectsArray = true;
23
24 if (!is_array($key)) {
25 $key = [$key];
26 $expectsArray = false;
27 }
28
29 if ($callback) {
30 $callback = is_array($callback) ? $callback : [$callback];
31
32 foreach ($key as $field) {
33 $array[$field] = $callback;
34 }
35 } else {
36 foreach ($key as $k => $v) {
37 if (is_int($k)) {
38 $k = $v;
39 $v = fn($v) => $this->sanitizeByType($v);
40 }
41
42 $array[$k] = is_array($v) ? $v : [$v];
43 }
44 }
45
46 $result = Sanitizer::sanitize($this->all(), $array);
47
48 $result = $this->pickKeys(array_keys($array), $result);
49
50 if (is_array($originalKey)) {
51 return $expectsArray ? $result : reset($result);
52 }
53
54 // return Arr::get($result, $originalKey, $result);
55
56 $rootKey = strstr($originalKey, '.', true) ?: $originalKey;
57 return $result[$rootKey] ?? $default;
58 }
59
60 protected function sanitizeByType($value)
61 {
62 if (is_array($value)) {
63 foreach ($value as $k => $v) {
64 $value[$k] = $this->sanitizeByType($v);
65 }
66 return $value;
67 } elseif (is_int($value)) {
68 return intval($value);
69 } elseif (is_float($value)) {
70 return floatval($value);
71 } elseif (is_bool($value)) {
72 return boolval($value);
73 } elseif (is_string($value)) {
74 if (str_contains($value, "\n") || str_contains($value, "\r")) {
75 return sanitize_textarea_field($value);
76 }
77 return sanitize_text_field($value);
78 }
79
80 return $value;
81 }
82
83 /**
84 * Pick only the given keys from data.
85 *
86 * @param array $keys
87 * @param array $allData
88 * @return array
89 */
90 public function pickKeys($keys, $allData, $default = null)
91 {
92 $result = [];
93
94 $walk = function ($data, $segments, &$res) use (&$walk, $default) {
95 $segment = array_shift($segments);
96
97 if ($segment === null) {
98 if (is_array($data)) {
99 $res = array_merge_recursive($res ?? [], $data);
100 } else {
101 $res = $data ?? $default;
102 }
103 return;
104 }
105
106 if ($segment === '*') {
107 if (!is_array($data)) return;
108
109 foreach ($data as $index => $value) {
110 $res[$index] ??= [];
111 $walk($value, $segments, $res[$index]);
112 }
113 } else {
114 if (is_array($data) && array_key_exists($segment, $data)) {
115 $res[$segment] ??= [];
116 $walk($data[$segment], $segments, $res[$segment]);
117 } else {
118 if (empty($segments)) {
119 $res[$segment] = $default;
120 }
121 }
122 }
123 };
124
125 foreach ($keys as $k) {
126 if (is_array($k)) {
127 $k = implode('.', $k);
128 }
129
130 $segments = explode('.', $k);
131 $walk($allData, $segments, $result);
132 }
133
134 return $result;
135 }
136
137 /**
138 * Returns a sanitized integer.
139 *
140 * @param string $key
141 * @param string $default
142 * @return int
143 */
144 public function getInt($key, $default = null)
145 {
146 return Sanitizer::sanitizeInt($this->get($key, $default));
147 }
148
149 /**
150 * Retrieve input as a float value.
151 *
152 * @param string|null $key
153 * @param float $default
154 * @return float
155 */
156 public function getFloat($key, $default = null)
157 {
158 return Sanitizer::sanitizeFloat($this->get($key, $default));
159 }
160
161 /**
162 * Returns a sanitized string.
163 *
164 * @param string $key
165 * @param string $default
166 * @return string
167 */
168 public function getText($key, $default = null)
169 {
170 return Sanitizer::sanitizeTextField($this->get($key, $default));
171 }
172
173 /**
174 * Returns a string as title.
175 *
176 * @param string $key
177 * @param string $default
178 * @return string
179 */
180 public function getTitle($key, $default = null)
181 {
182 return Sanitizer::sanitizeTitle($this->get($key, $default));
183 }
184
185 /**
186 * Returns sanitized email.
187 *
188 * @param string $key
189 * @param string $default
190 * @return string
191 */
192 public function getEmail($key, $default = null)
193 {
194 return Sanitizer::sanitizeEmail($this->get($key, $default));
195 }
196
197 /**
198 * Returns boolean value.
199 *
200 * @param string $key
201 * @param string $default
202 * @return bool|null TRUE for "1", "true", "on", "yes"; FALSE for "0", "false", "off", "no"; NULL otherwise
203 */
204 public function getBool($key, $default = null)
205 {
206 return Sanitizer::sanitizeBool($this->get($key, $default));
207 }
208
209 /**
210 * Returns a DateTime object.
211 *
212 * @param string $key
213 * @param string|null $format
214 * @param string|null $tz
215 * @return \FluentCommunity\Framework\Support\DateTime|null
216 */
217 public function getDate($key, $format = null, $tz = null)
218 {
219 if (!$value = $this->get($key)) {
220 return null;
221 }
222
223 return Sanitizer::sanitizeDate($value, $format, $tz);
224 }
225 }
226