-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathMissingDataHandler.php
More file actions
76 lines (59 loc) · 1.88 KB
/
MissingDataHandler.php
File metadata and controls
76 lines (59 loc) · 1.88 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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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();
}
public function handle(FormInterface $form, mixed $data): mixed
{
$processedData = $this->handleMissingData($form, $data);
return $processedData === $this->missingData ? $data : $processedData;
}
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)) {
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;
}
}