-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathDirect.php
196 lines (169 loc) · 6.31 KB
/
Direct.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace Pimcore\Bundle\DataImporterBundle\Mapping\DataTarget;
use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\Localizedfields;
use Pimcore\Model\Element\ElementInterface;
class Direct implements DataTargetInterface
{
/**
* @var string
*/
protected $fieldName;
/**
* @var string
*/
protected $language;
/**
* @var bool
*/
protected $writeIfSourceIsEmpty;
/**
* @var bool
*/
protected $writeIfTargetIsNotEmpty;
/**
* @param array $settings
*
* @throws InvalidConfigurationException
*/
public function setSettings(array $settings): void
{
if (empty($settings['fieldName'])) {
throw new InvalidConfigurationException('Empty field name.');
}
$this->fieldName = $settings['fieldName'];
$this->language = $settings['language'] ?? null;
//note - cannot be replaced with ?? as $settings['writeIfSourceIsEmpty'] can be false on purpose
$this->writeIfSourceIsEmpty = isset($settings['writeIfSourceIsEmpty']) ? $settings['writeIfSourceIsEmpty'] : true;
$this->writeIfTargetIsNotEmpty = isset($settings['writeIfTargetIsNotEmpty']) ? $settings['writeIfTargetIsNotEmpty'] : true;
}
/**
* @param ElementInterface $element
* @param mixed $data
*
* @return void
*
* @throws InvalidConfigurationException
*/
public function assignData(ElementInterface $element, $data): void
{
$setterParts = explode('.', $this->fieldName);
if ($this->fieldName === 'key') {
$this->doAssignData($element, $this->fieldName, $data);
} elseif (count($setterParts) === 1) {
//direct class attribute
$getter = 'get' . ucfirst($this->fieldName);
if (!$this->checkAssignData($data, $element, $getter)) {
return;
}
$this->doAssignData($element, $this->fieldName, $data);
} elseif (count($setterParts) === 3) {
//brick attribute
$brickContainerGetter = 'get' . ucfirst($setterParts[0]);
$brickContainer = $element->$brickContainerGetter();
$brickGetter = 'get' . ucfirst($setterParts[1]);
$brick = $brickContainer->$brickGetter();
if (empty($brick)) {
$brickClassName = '\\Pimcore\\Model\\DataObject\\Objectbrick\\Data\\' . ucfirst($setterParts[1]);
$brick = new $brickClassName($element);
$brickSetter = 'set' . ucfirst($setterParts[1]);
$brickContainer->$brickSetter($brick);
}
$getter = 'get' . ucfirst($setterParts[2]);
if (!$this->checkAssignData($data, $brick, $getter)) {
return;
}
$this->doAssignData($brick, $setterParts[2], $data);
} else {
throw new InvalidConfigurationException('Invalid number of setter parts for ' . $this->fieldName);
}
}
/**
* @param ElementInterface $valueContainer
* @param string $fieldName
* @param mixed $data
*
* @return void
*/
protected function doAssignData($valueContainer, $fieldName, $data)
{
$setter = 'set' . ucfirst($fieldName);
$valueContainer->$setter($data, $this->language);
}
/**
* @param mixed $newData
* @param object $valueContainer
* @param string $getter
*
* @return bool
*
* @throws InvalidConfigurationException
*/
protected function checkAssignData($newData, $valueContainer, $getter)
{
if ($this->writeIfTargetIsNotEmpty === true && $this->writeIfSourceIsEmpty === true) {
return true;
}
$hideUnpublished = DataObject::getHideUnpublished();
DataObject::setHideUnpublished(false);
$currentData = $valueContainer->$getter($this->language);
DataObject::setHideUnpublished($hideUnpublished);
$fieldName = $this->fieldName;
//brick attribute
$fieldNameParts = explode('.', $this->fieldName);
if (count($fieldNameParts) === 3) {
$fieldName = $fieldNameParts[2];
}
$fieldDefinition = $this->getFieldDefinition($valueContainer, $fieldName);
if ($this->writeIfTargetIsNotEmpty === false && !$fieldDefinition->isEmpty($currentData)) {
return false;
}
if ($this->writeIfSourceIsEmpty === false && $fieldDefinition->isEmpty($newData)) {
return false;
}
return true;
}
/**
* @param DataObject\Concrete|DataObject\Objectbrick\Data\AbstractData $valueContainer
* @param string $fieldName
*
* @throws InvalidConfigurationException
*/
protected function getFieldDefinition(
Object $valueContainer,
string $fieldName
): Data {
if ($valueContainer instanceof DataObject\Concrete) {
$definition = $valueContainer->getClass();
} elseif ($valueContainer instanceof DataObject\Objectbrick\Data\AbstractData) {
$definition = $valueContainer->getDefinition();
} else {
throw new InvalidConfigurationException('Invalid container type for data attribute.');
}
$fieldDefinition = $definition->getFieldDefinition($fieldName);
if ($fieldDefinition === null) {
$localizedFields = $definition->getFieldDefinition('localizedfields');
if ($localizedFields instanceof LocalizedFields) {
$fieldDefinition = $localizedFields->getFieldDefinition($fieldName);
}
}
if ($fieldDefinition === null) {
throw new InvalidConfigurationException(sprintf('Field definition for field "%s" not found.', $fieldName));
}
return $fieldDefinition;
}
}