1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Http; |
4: | |
5: | use Salient\Contract\Core\Arrayable; |
6: | use Salient\Contract\Core\DateFormatterInterface; |
7: | use Salient\Contract\Core\Jsonable; |
8: | use Salient\Contract\Http\FormDataFlag; |
9: | use Salient\Core\DateFormatter; |
10: | use Salient\Utility\Arr; |
11: | use Salient\Utility\Get; |
12: | use Salient\Utility\Json; |
13: | use Salient\Utility\Test; |
14: | use DateTimeInterface; |
15: | use InvalidArgumentException; |
16: | use JsonSerializable; |
17: | use Traversable; |
18: | |
19: | |
20: | |
21: | |
22: | final class FormData |
23: | { |
24: | |
25: | private $Data; |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | public function __construct($data) |
33: | { |
34: | $this->Data = $data; |
35: | } |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | |
71: | public function getValues( |
72: | int $flags = FormDataFlag::PRESERVE_NUMERIC_KEYS | FormDataFlag::PRESERVE_STRING_KEYS, |
73: | ?DateFormatterInterface $dateFormatter = null, |
74: | ?callable $callback = null |
75: | ): array { |
76: | $dateFormatter ??= new DateFormatter(); |
77: | |
78: | return $this->doGetData($this->Data, $flags, $dateFormatter, $callback); |
79: | } |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | public function getQuery( |
93: | int $flags = FormDataFlag::PRESERVE_NUMERIC_KEYS | FormDataFlag::PRESERVE_STRING_KEYS, |
94: | ?DateFormatterInterface $dateFormatter = null, |
95: | ?callable $callback = null |
96: | ): string { |
97: | $dateFormatter ??= new DateFormatter(); |
98: | $data = $this->doGetData($this->Data, $flags, $dateFormatter, $callback); |
99: | foreach ($data as [$key, $value]) { |
100: | if (!is_string($value)) { |
101: | throw new InvalidArgumentException(sprintf( |
102: | "Invalid value at '%s': %s", |
103: | $key, |
104: | Get::type($value), |
105: | )); |
106: | } |
107: | $query[] = rawurlencode($key) . '=' . rawurlencode($value); |
108: | } |
109: | return implode('&', $query ?? []); |
110: | } |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | public function getData( |
125: | int $flags = FormDataFlag::PRESERVE_NUMERIC_KEYS | FormDataFlag::PRESERVE_STRING_KEYS, |
126: | ?DateFormatterInterface $dateFormatter = null, |
127: | ?callable $callback = null |
128: | ): array { |
129: | $dateFormatter ??= new DateFormatter(); |
130: | return $this->doGetData($this->Data, $flags, $dateFormatter, $callback, false); |
131: | } |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | private function doGetData( |
145: | $data, |
146: | int $flags, |
147: | DateFormatterInterface $df, |
148: | ?callable $cb, |
149: | bool $flatten = true, |
150: | bool $fromCallback = false, |
151: | &$query = [], |
152: | ?string $name = null |
153: | ) { |
154: | if ($name === null) { |
155: | $data = $flatten |
156: | ? $this->flattenValue($data, $df, $cb) |
157: | : $this->resolveValue($data, $df, $cb, $fromCallback); |
158: | } |
159: | |
160: | |
161: | if ( |
162: | ($flatten && ($data === null || $data === [])) |
163: | || ($fromCallback && $data === null) |
164: | ) { |
165: | return $query; |
166: | } |
167: | |
168: | if (is_array($data)) { |
169: | $hasArray = false; |
170: | if ($flatten) { |
171: | foreach ($data as $key => &$value) { |
172: | $value = $this->flattenValue($value, $df, $cb); |
173: | if (!$hasArray) { |
174: | $hasArray = is_array($value); |
175: | } |
176: | } |
177: | } else { |
178: | |
179: | $fromCallback = []; |
180: | foreach ($data as $key => &$value) { |
181: | $value = $this->resolveValue($value, $df, $cb, $fromCallback[$key]); |
182: | } |
183: | } |
184: | unset($value); |
185: | |
186: | $preserveKeys = $name === null || $hasArray || ( |
187: | Arr::isList($data) |
188: | ? $flags & FormDataFlag::PRESERVE_LIST_KEYS |
189: | : (Arr::isIndexed($data) |
190: | ? $flags & FormDataFlag::PRESERVE_NUMERIC_KEYS |
191: | : $flags & FormDataFlag::PRESERVE_STRING_KEYS) |
192: | ); |
193: | |
194: | $format = $preserveKeys |
195: | ? ($flatten && $name !== null ? '[%s]' : '%s') |
196: | : ($flatten ? '[]' : ''); |
197: | |
198: | if ($flatten) { |
199: | |
200: | foreach ($data as $key => $value) { |
201: | $_key = sprintf($format, $key); |
202: | $this->doGetData($value, $flags, $df, $cb, true, false, $query, $name . $_key); |
203: | } |
204: | } else { |
205: | |
206: | foreach ($data as $key => $value) { |
207: | |
208: | $_key = sprintf($format, $key); |
209: | |
210: | if ($_key === '') { |
211: | $query[] = null; |
212: | $_key = array_key_last($query); |
213: | } |
214: | $this->doGetData($value, $flags, $df, $cb, false, $fromCallback[$key], $query[$_key], ''); |
215: | } |
216: | } |
217: | |
218: | return $query; |
219: | } |
220: | |
221: | if ($flatten) { |
222: | $query[] = [$name, $data]; |
223: | return $query; |
224: | } |
225: | |
226: | if ($name === null) { |
227: | throw new InvalidArgumentException('Argument #1 ($data) must resolve to an array'); |
228: | } |
229: | |
230: | $query = $data; |
231: | return $query; |
232: | } |
233: | |
234: | |
235: | |
236: | |
237: | |
238: | |
239: | |
240: | |
241: | private function flattenValue( |
242: | $value, |
243: | DateFormatterInterface $df, |
244: | ?callable $cb |
245: | ) { |
246: | if (is_bool($value)) { |
247: | return (string) (int) $value; |
248: | } |
249: | |
250: | if ($value === null || is_scalar($value)) { |
251: | return (string) $value; |
252: | } |
253: | |
254: | if (is_array($value)) { |
255: | return $value; |
256: | } |
257: | |
258: | if (is_object($value)) { |
259: | $value = $this->convertObject($value, $df, $cb, $fromCallback); |
260: | if (!$fromCallback && ($value === null || is_scalar($value))) { |
261: | return (string) $value; |
262: | } |
263: | |
264: | return $value; |
265: | } |
266: | |
267: | throw new InvalidArgumentException(sprintf( |
268: | 'Invalid value: %s', |
269: | Get::type($value), |
270: | )); |
271: | } |
272: | |
273: | |
274: | |
275: | |
276: | |
277: | |
278: | |
279: | |
280: | private function resolveValue( |
281: | $value, |
282: | DateFormatterInterface $df, |
283: | ?callable $cb, |
284: | ?bool &$fromCallback |
285: | ) { |
286: | $fromCallback = false; |
287: | |
288: | if ($value === null || is_scalar($value) || is_array($value)) { |
289: | return $value; |
290: | } |
291: | |
292: | if (is_object($value)) { |
293: | return $this->convertObject($value, $df, $cb, $fromCallback); |
294: | } |
295: | |
296: | throw new InvalidArgumentException(sprintf( |
297: | 'Invalid value: %s', |
298: | Get::type($value), |
299: | )); |
300: | } |
301: | |
302: | |
303: | |
304: | |
305: | |
306: | |
307: | |
308: | private function convertObject( |
309: | object $value, |
310: | DateFormatterInterface $df, |
311: | ?callable $cb, |
312: | ?bool &$fromCallback |
313: | ) { |
314: | $fromCallback = false; |
315: | |
316: | if ($value instanceof DateTimeInterface) { |
317: | return $df->format($value); |
318: | } |
319: | |
320: | if ($cb !== null) { |
321: | $result = $cb($value); |
322: | if ($result instanceof DateTimeInterface) { |
323: | return $df->format($result); |
324: | } |
325: | if ($result !== false) { |
326: | $fromCallback = true; |
327: | return $result; |
328: | } |
329: | } |
330: | |
331: | if ($value instanceof Arrayable) { |
332: | return $value->toArray(); |
333: | } |
334: | |
335: | if ($value instanceof JsonSerializable) { |
336: | return Json::parseObjectAsArray(Json::stringify($value)); |
337: | } |
338: | |
339: | if ($value instanceof Jsonable) { |
340: | return Json::parseObjectAsArray($value->toJson()); |
341: | } |
342: | |
343: | if ($value instanceof Traversable) { |
344: | return iterator_to_array($value); |
345: | } |
346: | |
347: | |
348: | $result = []; |
349: | |
350: | foreach ($value as $key => $val) { |
351: | $result[$key] = $val; |
352: | } |
353: | if ($result !== []) { |
354: | return $result; |
355: | } |
356: | |
357: | if (Test::isStringable($value)) { |
358: | return (string) $value; |
359: | } |
360: | |
361: | return []; |
362: | } |
363: | } |
364: | |