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