1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\Bundle\EzFormsBundle\Form\DataMapper; |
6
|
|
|
|
7
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\ContentType; |
8
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataMapper; |
9
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataWrapper; |
10
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\Payload\InformationCollectionStruct; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use Symfony\Component\Form\FormInterface; |
13
|
|
|
use Symfony\Component\PropertyAccess\PropertyPathInterface; |
14
|
|
|
|
15
|
|
View Code Duplication |
final class InformationCollectionMapper extends DataMapper |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Maps data from eZ Publish structure to the form. |
19
|
|
|
*/ |
20
|
|
|
protected function mapToForm( |
21
|
|
|
FormInterface $form, |
22
|
|
|
DataWrapper $data, |
23
|
|
|
PropertyPathInterface $propertyPath |
24
|
|
|
): void { |
25
|
|
|
/** @var ContentType $contentType */ |
26
|
|
|
$contentType = $data->definition; |
27
|
|
|
|
28
|
|
|
$fieldDefinitionIdentifier = (string) $propertyPath; |
29
|
|
|
$fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
30
|
|
|
|
31
|
|
|
if (null === $fieldDefinition) { |
32
|
|
|
throw new RuntimeException( |
33
|
|
|
"Data definition does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
38
|
|
|
|
39
|
|
|
$handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
40
|
|
|
|
41
|
|
|
$form->setData( |
42
|
|
|
$handler->convertFieldValueToForm( |
43
|
|
|
$contentType->getFieldDefinition($fieldDefinitionIdentifier)->defaultValue, |
44
|
|
|
$fieldDefinition |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Maps data from form to the eZ Publish structure. |
51
|
|
|
*/ |
52
|
|
|
protected function mapFromForm( |
53
|
|
|
FormInterface $form, |
54
|
|
|
DataWrapper $data, |
55
|
|
|
PropertyPathInterface $propertyPath |
56
|
|
|
): void { |
57
|
|
|
/** @var InformationCollectionStruct $payload */ |
58
|
|
|
$payload = $data->payload; |
59
|
|
|
/** @var ContentType $contentType */ |
60
|
|
|
$contentType = $data->definition; |
61
|
|
|
|
62
|
|
|
$fieldDefinitionIdentifier = (string) $propertyPath; |
63
|
|
|
$fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
64
|
|
|
|
65
|
|
|
if (null === $fieldDefinition) { |
66
|
|
|
throw new RuntimeException( |
67
|
|
|
"Data definition does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
72
|
|
|
$handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
73
|
|
|
|
74
|
|
|
$payload->setCollectedFieldValue( |
75
|
|
|
$fieldDefinitionIdentifier, |
76
|
|
|
$handler->convertFieldValueFromForm($form->getData()) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.