|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Netgen\Bundle\EzFormsBundle\Form; |
|
6
|
|
|
|
|
7
|
|
|
use DateTime; |
|
8
|
|
|
use Symfony\Component\Form\DataMapperInterface; |
|
9
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
|
10
|
|
|
use Symfony\Component\Form\FormInterface; |
|
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
12
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
13
|
|
|
use Symfony\Component\PropertyAccess\PropertyPathInterface; |
|
14
|
|
|
use function is_array; |
|
15
|
|
|
use function is_object; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* A data mapper using property paths to read/write data. |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class DataMapper implements DataMapperInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var \Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandlerRegistry |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $fieldTypeHandlerRegistry; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Symfony\Component\PropertyAccess\PropertyAccessorInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $propertyAccessor; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Creates a new property path mapper. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandlerRegistry $fieldTypeHandlerRegistry |
|
36
|
|
|
* @param \Symfony\Component\PropertyAccess\PropertyAccessorInterface $propertyAccessor |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( |
|
39
|
|
|
FieldTypeHandlerRegistry $fieldTypeHandlerRegistry, |
|
40
|
|
|
?PropertyAccessorInterface $propertyAccessor = null |
|
41
|
|
|
) { |
|
42
|
|
|
$this->fieldTypeHandlerRegistry = $fieldTypeHandlerRegistry; |
|
43
|
|
|
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function mapDataToForms($data, $forms): void |
|
47
|
|
|
{ |
|
48
|
|
|
$empty = null === $data || [] === $data; |
|
49
|
|
|
|
|
50
|
|
|
if (!$empty && !is_array($data) && !is_object($data)) { |
|
51
|
|
|
throw new UnexpectedTypeException($data, 'object, array or empty'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
foreach ($forms as $form) { |
|
55
|
|
|
$propertyPath = $form->getPropertyPath(); |
|
56
|
|
|
$config = $form->getConfig(); |
|
57
|
|
|
|
|
58
|
|
|
if ($data instanceof DataWrapper && null !== $propertyPath && $config->getMapped()) { |
|
59
|
|
|
/* @var $data \Netgen\Bundle\EzFormsBundle\Form\DataWrapper */ |
|
60
|
|
|
$this->mapToForm($form, $data, $propertyPath); |
|
61
|
|
|
} elseif (!$empty && null !== $propertyPath && $config->getMapped()) { |
|
62
|
|
|
$form->setData($this->propertyAccessor->getValue($data, $propertyPath)); |
|
63
|
|
|
} else { |
|
64
|
|
|
$form->setData($form->getConfig()->getData()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function mapFormsToData($forms, &$data): void |
|
70
|
|
|
{ |
|
71
|
|
|
if (null === $data) { |
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (!is_array($data) && !is_object($data)) { |
|
76
|
|
|
throw new UnexpectedTypeException($data, 'object, array or empty'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
foreach ($forms as $form) { |
|
80
|
|
|
$propertyPath = $form->getPropertyPath(); |
|
81
|
|
|
$config = $form->getConfig(); |
|
82
|
|
|
|
|
83
|
|
|
// Write-back is disabled if the form is not synchronized (transformation failed), |
|
84
|
|
|
// if the form was not submitted and if the form is disabled (modification not allowed) |
|
85
|
|
|
if ( |
|
86
|
|
|
null === $propertyPath || |
|
87
|
|
|
!$config->getMapped() || |
|
88
|
|
|
!$form->isSubmitted() || |
|
89
|
|
|
!$form->isSynchronized() || |
|
90
|
|
|
$form->isDisabled() |
|
91
|
|
|
) { |
|
92
|
|
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// If $data is out ContentCreateStruct, we need to map it to the corresponding field |
|
96
|
|
|
// in the struct |
|
97
|
|
|
if ($data instanceof DataWrapper) { |
|
98
|
|
|
/* @var $data \Netgen\Bundle\EzFormsBundle\Form\DataWrapper */ |
|
99
|
|
|
$this->mapFromForm($form, $data, $propertyPath); |
|
100
|
|
|
|
|
101
|
|
|
continue; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// If the field is of type DateTime and the data is the same skip the update to |
|
105
|
|
|
// keep the original object hash |
|
106
|
|
|
if ( |
|
107
|
|
|
$form->getData() instanceof DateTime && |
|
108
|
|
|
$form->getData() === $this->propertyAccessor->getValue($data, $propertyPath) |
|
109
|
|
|
) { |
|
110
|
|
|
continue; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// If the data is identical to the value in $data, we are |
|
114
|
|
|
// dealing with a reference |
|
115
|
|
|
if ( |
|
116
|
|
|
is_object($data) && |
|
117
|
|
|
$config->getByReference() && |
|
118
|
|
|
$form->getData() === $this->propertyAccessor->getValue($data, $propertyPath) |
|
119
|
|
|
) { |
|
120
|
|
|
continue; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$this->propertyAccessor->setValue($data, $propertyPath, $form->getData()); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Maps data from eZ Publish structure to the form. |
|
129
|
|
|
*/ |
|
130
|
|
|
abstract protected function mapToForm( |
|
131
|
|
|
FormInterface $form, |
|
132
|
|
|
DataWrapper $data, |
|
133
|
|
|
PropertyPathInterface $propertyPath |
|
134
|
|
|
): void; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Maps data from form to the eZ Publish structure. |
|
138
|
|
|
*/ |
|
139
|
|
|
abstract protected function mapFromForm( |
|
140
|
|
|
FormInterface $form, |
|
141
|
|
|
DataWrapper $data, |
|
142
|
|
|
PropertyPathInterface $propertyPath |
|
143
|
|
|
): void; |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Returns if the update should be skipped for empty value. |
|
147
|
|
|
* |
|
148
|
|
|
* @param \Symfony\Component\Form\FormInterface $form |
|
149
|
|
|
* @param mixed $value |
|
150
|
|
|
* @param string $fieldDefinitionIdentifier |
|
151
|
|
|
* |
|
152
|
|
|
* @return bool |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function shouldSkipForEmptyUpdate(FormInterface $form, $value, string $fieldDefinitionIdentifier): bool |
|
155
|
|
|
{ |
|
156
|
|
|
return |
|
157
|
|
|
$value === null && |
|
158
|
|
|
( |
|
159
|
|
|
$form->getRoot()->has("ezforms_skip_empty_update_{$fieldDefinitionIdentifier}") && |
|
160
|
|
|
$form->getRoot()->get("ezforms_skip_empty_update_{$fieldDefinitionIdentifier}")->getData() === 'yes' |
|
161
|
|
|
) |
|
162
|
|
|
; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|