-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathMissingDataHandler.php
More file actions
86 lines (69 loc) · 2.33 KB
/
MissingDataHandler.php
File metadata and controls
86 lines (69 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form;
/**
* @internal
*/
class MissingDataHandler
{
public readonly \stdClass $missingData;
public function __construct()
{
$this->missingData = new \stdClass();
}
/**
* Folds default values (typically from `false_values`) into $data for children the payload omits.
*
* Callers may pass `$this->missingData` as the sentinel for "no data submitted". Returns $data
* unchanged when no child synthesises a value.
*/
public function handle(FormInterface $form, mixed $data): mixed
{
$processedData = $this->handleMissingData($form, $data);
return $processedData === $this->missingData ? $data : $processedData;
}
/**
* Returns `$this->missingData` to signal "nothing to synthesise for this branch"; the public
* wrapper substitutes the caller's original $data back in.
*/
private function handleMissingData(FormInterface $form, mixed $data): mixed
{
$config = $form->getConfig();
$missingData = $this->missingData;
$falseValues = $config->getOption('false_values', null);
if (\is_array($falseValues)) {
if ($data === $missingData) {
return $falseValues[0] ?? null;
}
if (\in_array($data, $falseValues, true)) {
return $data;
}
}
if (null === $data || $missingData === $data) {
$data = $config->getCompound() ? [] : $data;
}
if (\is_array($data)) {
$children = $config->getCompound() ? $form->all() : [$form];
foreach ($children as $child) {
$name = $child->getName();
$childData = $missingData;
if (\array_key_exists($name, $data)) {
$childData = $data[$name];
}
$value = $this->handleMissingData($child, $childData);
if ($missingData !== $value) {
$data[$name] = $value;
}
}
return $data ?: $missingData;
}
return $data;
}
}