|
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
|
|
|
final class CreateUserMapper extends DataMapper |
|
17
|
|
|
{ |
|
18
|
|
|
protected function mapToForm(FormInterface $form, DataWrapper $data, PropertyPathInterface $propertyPath): void |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var \eZ\Publish\Core\Repository\Values\User\UserCreateStruct $userCreateStruct */ |
|
21
|
|
|
$userCreateStruct = $data->payload; |
|
22
|
|
|
$contentType = $userCreateStruct->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\API\Repository\Values\User\UserCreateStruct $userCreateStruct */ |
|
47
|
|
|
$userCreateStruct = $data->payload; |
|
48
|
|
|
$contentType = $userCreateStruct->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
|
|
|
$convertedData = $handler->convertFieldValueFromForm($form->getData()); |
|
63
|
|
|
|
|
64
|
|
|
// 'ezuser' is an exceptional case, here we need to map form data to the properties |
|
65
|
|
|
// in the UserCreateStruct |
|
66
|
|
|
if ($fieldTypeIdentifier === 'ezuser') { |
|
67
|
|
|
$userCreateStruct->login = $convertedData['username']; |
|
68
|
|
|
$userCreateStruct->email = $convertedData['email']; |
|
69
|
|
|
$userCreateStruct->password = $convertedData['password'] ?? null; |
|
70
|
|
|
} else { |
|
71
|
|
|
$userCreateStruct->setField($fieldDefinitionIdentifier, $convertedData); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|