forked from phpcr/phpcr-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeNormalizer.php
222 lines (186 loc) · 6.21 KB
/
NodeNormalizer.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
* This file is part of the PHPCR Shell package
*
* (c) Daniel Leech <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PHPCR\Shell\Serializer;
use PHPCR\NodeInterface;
use PHPCR\PropertyInterface;
use PHPCR\PropertyType;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Normalizer for PHPCR Nodes.
*
* @author Daniel Leech <[email protected]>
*/
class NodeNormalizer implements NormalizerInterface, DenormalizerInterface
{
protected $allowBinary;
protected $notes = [];
public function __construct($allowBinary = false)
{
$this->allowBinary = $allowBinary;
}
public function getNotes()
{
return $this->notes;
}
public function getSupportedTypes(?string $format): array
{
return [
NodeInterface::class => true,
];
}
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = []): array
{
$res = [];
foreach ($object->getProperties() as $property) {
if (false === $this->isPropertyEditable($property)) {
continue;
}
$propertyType = $property->getType();
$propertyValue = $property->getValue();
$propertyName = $property->getName();
if (in_array($property->getType(), [PropertyType::REFERENCE, PropertyType::WEAKREFERENCE])) {
$nodeUuids = [];
if (false === is_array($propertyValue)) {
$propertyValue = [$propertyValue];
}
foreach ($propertyValue as $node) {
$nodeUuids[] = $node->getIdentifier();
}
$propertyValue = $nodeUuids;
if (false === $property->isMultiple()) {
$propertyValue = reset($propertyValue);
}
}
$res[$propertyName] = [
'type' => PropertyType::nameFromValue($propertyType),
'value' => $propertyValue,
];
}
return $res;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null, array $context = []): bool
{
return is_object($data) && $data instanceof NodeInterface;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = []): mixed
{
if (!$data) {
throw new \InvalidArgumentException(
'Editor returned nothing .. nodes must have at least one property (i.e. the jcr:primaryType property)'
);
}
if (!isset($context['node'])) {
throw new \InvalidArgumentException(sprintf(
'You must provide the PHPCR node instance to update in the context using the "node" key.'
));
}
$node = $context['node'];
$errors = [];
// Update / remove existing properties
foreach ($node->getProperties() as $property) {
if (false === $this->isPropertyEditable($property)) {
continue;
}
try {
if (!isset($data[$property->getName()])) {
$property->remove();
continue;
}
$datum = $this->normalizeDatum($data[$property->getName()]);
$typeValue = isset($datum['type']) ? PropertyType::valueFromName($datum['type']) : null;
if (isset($datum['value'])) {
// if the type or the value is differnet, update the property
if ($datum['value'] != $property->getValue() || $typeValue != $property->getType()) {
// setValue doesn't like being passed a null value as a type ...
if ($typeValue !== null) {
$property->setValue($datum['value'], $typeValue);
} else {
$property->setValue($datum['value']);
}
}
}
} catch (\Exception $e) {
$errors[] = $e->getMessage();
}
unset($data[$property->getName()]);
}
// Add new properties
foreach ($data as $pName => $datum) {
$datum = $this->normalizeDatum($datum);
$pValue = isset($datum['value']) ? $datum['value'] : null;
$pType = isset($datum['type']) ? PropertyType::valueFromName($datum['type']) : null;
if ($pValue !== null) {
$node->setProperty($pName, $pValue, $pType);
}
}
if (count($errors) > 0) {
throw new InvalidArgumentException(sprintf(
'Errors encountered during denormalization: %s',
implode("\n", $errors)
));
}
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
{
return $type === 'PHPCR\NodeInterface';
}
/**
* If the value is a scalar value convert it into
* an array with default values.
*
* @param mixed
*
* @return string
*/
private function normalizeDatum($value)
{
if (is_scalar($value)) {
return [
'value' => $value,
'type' => null,
];
}
return $value;
}
/**
* Return false if property type is not editable.
*
* (e.g. property type is binary)
*
* @return bool
*/
private function isPropertyEditable(PropertyInterface $property)
{
// do not serialize binary objects
if (false === $this->allowBinary && PropertyType::BINARY == $property->getType()) {
$this->notes[] = sprintf(
'Binary property "%s" has been omitted',
$property->getName()
);
return false;
}
return true;
}
}