1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\Bundle\EzFormsBundle\Form\DataMapper; |
6
|
|
|
|
7
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataMapper; |
8
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataWrapper; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Symfony\Component\Form\FormInterface; |
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyPathInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A data mapper using property paths to read/write data. |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
final class CreateContentMapper extends DataMapper |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
protected function mapToForm(FormInterface $form, DataWrapper $data, PropertyPathInterface $propertyPath): void |
19
|
|
|
{ |
20
|
|
|
/** @var \eZ\Publish\Core\Repository\Values\Content\ContentCreateStruct $contentCreateStruct */ |
21
|
|
|
$contentCreateStruct = $data->payload; |
22
|
|
|
$contentType = $contentCreateStruct->contentType; |
23
|
|
|
|
24
|
|
|
$fieldDefinitionIdentifier = (string) $propertyPath; |
25
|
|
|
$fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
26
|
|
|
|
27
|
|
|
if (null === $fieldDefinition) { |
28
|
|
|
throw new RuntimeException( |
29
|
|
|
"Data payload does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
34
|
|
|
|
35
|
|
|
$handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
36
|
|
|
$form->setData( |
37
|
|
|
$handler->convertFieldValueToForm( |
38
|
|
|
$contentType->getFieldDefinition($fieldDefinitionIdentifier)->defaultValue, |
39
|
|
|
$fieldDefinition |
40
|
|
|
) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function mapFromForm(FormInterface $form, DataWrapper $data, PropertyPathInterface $propertyPath): void |
45
|
|
|
{ |
46
|
|
|
/** @var \eZ\Publish\Core\Repository\Values\Content\ContentCreateStruct $contentCreateStruct */ |
47
|
|
|
$contentCreateStruct = $data->payload; |
48
|
|
|
$contentType = $contentCreateStruct->contentType; |
49
|
|
|
|
50
|
|
|
$fieldDefinitionIdentifier = (string) $propertyPath; |
51
|
|
|
$fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
52
|
|
|
|
53
|
|
|
if (null === $fieldDefinition) { |
54
|
|
|
throw new RuntimeException( |
55
|
|
|
"Data payload does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
60
|
|
|
|
61
|
|
|
$handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
62
|
|
|
$contentCreateStruct->setField( |
63
|
|
|
$fieldDefinitionIdentifier, |
64
|
|
|
$handler->convertFieldValueFromForm($form->getData()) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
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.