|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the "elao/form-simple-object-mapper" package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) 2016 Elao |
|
7
|
|
|
* |
|
8
|
|
|
* @author Elao <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Elao\FormSimpleObjectMapper\DataMapper; |
|
12
|
|
|
|
|
13
|
|
|
use Symfony\Component\Form\DataMapperInterface; |
|
14
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
|
15
|
|
|
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Maxime Steinhausser <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class SimpleObjectMapper implements DataMapperInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var FormDataToObjectConverterInterface */ |
|
23
|
|
|
private $converter; |
|
24
|
|
|
|
|
25
|
|
|
/** @var DataMapperInterface|null */ |
|
26
|
|
|
private $originalMapper; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct( |
|
29
|
|
|
FormDataToObjectConverterInterface $converter, |
|
30
|
|
|
DataMapperInterface $originalMapper = null |
|
31
|
|
|
) { |
|
32
|
|
|
$this->converter = $converter; |
|
33
|
|
|
$this->originalMapper = $originalMapper; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function mapDataToForms($data, $forms) |
|
40
|
|
|
{ |
|
41
|
|
|
// Fallback to original mapper instance or default to "PropertyPathMapper" |
|
42
|
|
|
// mapper implementation if not an "ObjectToFormDataConverterInterface" instance: |
|
43
|
|
|
if (!$this->converter instanceof ObjectToFormDataConverterInterface) { |
|
44
|
|
|
$propertyPathMapper = $this->originalMapper ?: new PropertyPathMapper(); |
|
45
|
|
|
$propertyPathMapper->mapDataToForms($data, $forms); |
|
46
|
|
|
|
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$data = $this->convertObjectToFormData($this->converter, $data); |
|
51
|
|
|
|
|
52
|
|
|
foreach ($forms as $form) { |
|
53
|
|
|
$config = $form->getConfig(); |
|
54
|
|
|
|
|
55
|
|
|
if ($config->getMapped() && isset($data[$form->getName()])) { |
|
56
|
|
|
$form->setData($data[$form->getName()]); |
|
57
|
|
|
|
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$form->setData($config->getData()); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function mapFormsToData($forms, &$data) |
|
69
|
|
|
{ |
|
70
|
|
|
$fieldsData = []; |
|
71
|
|
|
foreach ($forms as $form) { |
|
72
|
|
|
$fieldsData[$form->getName()] = $form->getData(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$data = $this->converter->convertFormDataToObject($fieldsData, $data); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function convertObjectToFormData(ObjectToFormDataConverterInterface $converter, $data) |
|
79
|
|
|
{ |
|
80
|
|
|
if (!is_object($data) && null !== $data) { |
|
81
|
|
|
throw new UnexpectedTypeException($data, 'object or null'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$data = $converter->convertObjectToFormData($data); |
|
85
|
|
|
|
|
86
|
|
|
if (!is_array($data)) { |
|
87
|
|
|
throw new UnexpectedTypeException($data, 'array'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $data; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|