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