|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\FieldDefinitionConverterInterface; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface; |
|
8
|
|
|
use eZ\Publish\API\Repository\FieldTypeService; |
|
9
|
|
|
|
|
10
|
|
|
class FieldHandlerManager |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var FieldValueImporterInterface[][] */ |
|
13
|
|
|
protected $fieldTypeMap; |
|
14
|
|
|
protected $fieldTypeService; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(FieldTypeService $fieldTypeService) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->fieldTypeService = $fieldTypeService; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param FieldValueImporterInterface $fieldHandler |
|
23
|
|
|
* @param string $fieldTypeIdentifier |
|
24
|
|
|
* @param string $contentTypeIdentifier |
|
25
|
|
|
* @throws \Exception |
|
26
|
|
|
*/ |
|
27
|
|
|
public function addFieldHandler($fieldHandler, $fieldTypeIdentifier, $contentTypeIdentifier = null) |
|
28
|
|
|
{ |
|
29
|
|
|
// This is purely BC; at some point we will typehint to FieldValueImporterInterface |
|
30
|
|
|
if (!$fieldHandler instanceof FieldValueImporterInterface) { |
|
31
|
|
|
throw new \Exception("Can not register object of class '" . get_class($fieldHandler) . "' as field handler because it does not support the desired interface"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if ($contentTypeIdentifier == null) { |
|
|
|
|
|
|
35
|
|
|
$contentTypeIdentifier = '*'; |
|
36
|
|
|
} |
|
37
|
|
|
$this->fieldTypeMap[$contentTypeIdentifier][$fieldTypeIdentifier] = $fieldHandler; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $fieldTypeIdentifier |
|
42
|
|
|
* @param string $contentTypeIdentifier |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
|
|
public function managesField($fieldTypeIdentifier, $contentTypeIdentifier) |
|
46
|
|
|
{ |
|
47
|
|
|
return (isset($this->fieldTypeMap[$contentTypeIdentifier][$fieldTypeIdentifier]) || |
|
48
|
|
|
isset($this->fieldTypeMap['*'][$fieldTypeIdentifier])); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns true when a fieldHandler allows string refs to be pre-resolved for the field value it gets as hash. |
|
53
|
|
|
* "pre-resolved" means: resolved before the field-handler-specific propcessing kicks in |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $fieldTypeIdentifier |
|
56
|
|
|
* @param string $contentTypeIdentifier |
|
57
|
|
|
*/ |
|
58
|
|
|
public function doPreResolveStringReferences($fieldTypeIdentifier, $contentTypeIdentifier) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!$this->managesField($fieldTypeIdentifier, $contentTypeIdentifier)) { |
|
61
|
|
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$fieldHandler = $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier); |
|
65
|
|
|
// BC: not alwys defined |
|
66
|
|
|
if (is_callable(array($fieldHandler, 'doPreResolveStringReferences'))) { |
|
67
|
|
|
return $fieldHandler->doPreResolveStringReferences(); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $fieldTypeIdentifier |
|
75
|
|
|
* @param string $contentTypeIdentifier |
|
76
|
|
|
* @param mixed $hashValue |
|
77
|
|
|
* @param array $context |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public function hashToFieldValue($fieldTypeIdentifier, $contentTypeIdentifier, $hashValue, array $context = array()) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->managesField($fieldTypeIdentifier, $contentTypeIdentifier)) { |
|
83
|
|
|
$fieldHandler = $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier); |
|
84
|
|
|
// BC |
|
85
|
|
|
if (!$fieldHandler instanceof FieldValueImporterInterface) { |
|
86
|
|
|
return $fieldHandler->createValue($hashValue, $context); |
|
87
|
|
|
} |
|
88
|
|
|
return $fieldHandler->hashToFieldValue($hashValue, $context); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$fieldType = $this->fieldTypeService->getFieldType($fieldTypeIdentifier); |
|
92
|
|
|
return $fieldType->fromHash($hashValue); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $fieldTypeIdentifier |
|
97
|
|
|
* @param string $contentTypeIdentifier |
|
98
|
|
|
* @param \eZ\Publish\SPI\FieldType\Value $value |
|
99
|
|
|
* @param array $context |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
*/ |
|
102
|
|
|
public function fieldValueToHash($fieldTypeIdentifier, $contentTypeIdentifier, $value, array $context = array()) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($this->managesField($fieldTypeIdentifier, $contentTypeIdentifier)) { |
|
105
|
|
|
$fieldHandler = $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier); |
|
106
|
|
|
if ($fieldHandler instanceof FieldValueConverterInterface) { |
|
107
|
|
|
return $fieldHandler->fieldValueToHash($value, $context); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$fieldType = $this->fieldTypeService->getFieldType($fieldTypeIdentifier); |
|
112
|
|
|
return $fieldType->toHash($value); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $fieldTypeIdentifier |
|
117
|
|
|
* @param string $contentTypeIdentifier |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
|
|
public function managesFieldDefinition($fieldTypeIdentifier, $contentTypeIdentifier) |
|
121
|
|
|
{ |
|
122
|
|
|
if (!$this->managesField($fieldTypeIdentifier, $contentTypeIdentifier)) { |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$fieldHandler = $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier); |
|
127
|
|
|
return ($fieldHandler instanceof FieldDefinitionConverterInterface); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param string $fieldTypeIdentifier |
|
132
|
|
|
* @param string $contentTypeIdentifier |
|
133
|
|
|
* @param mixed $fieldSettingsHash |
|
134
|
|
|
* @param array $context |
|
135
|
|
|
* @return mixed |
|
136
|
|
|
*/ |
|
137
|
|
|
public function hashToFieldSettings($fieldTypeIdentifier, $contentTypeIdentifier, $fieldSettingsHash, array $context = array()) |
|
138
|
|
|
{ |
|
139
|
|
|
if ($this->managesFieldDefinition($fieldTypeIdentifier, $contentTypeIdentifier)) { |
|
140
|
|
|
return $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier)->hashToFieldSettings($fieldSettingsHash, $context); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $fieldSettingsHash; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param string $fieldTypeIdentifier |
|
148
|
|
|
* @param string $contentTypeIdentifier |
|
149
|
|
|
* @param mixed $fieldSettings |
|
150
|
|
|
* @param array $context |
|
151
|
|
|
* @return mixed |
|
152
|
|
|
*/ |
|
153
|
|
|
public function fieldSettingsToHash($fieldTypeIdentifier, $contentTypeIdentifier, $fieldSettings, array $context = array()) |
|
154
|
|
|
{ |
|
155
|
|
|
if ($this->managesFieldDefinition($fieldTypeIdentifier, $contentTypeIdentifier)) { |
|
156
|
|
|
return $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier)->fieldSettingsToHash($fieldSettings, $context); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $fieldSettings; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param string $fieldTypeIdentifier |
|
164
|
|
|
* @param string $contentTypeIdentifier |
|
165
|
|
|
* @return FieldValueImporterInterface |
|
166
|
|
|
* @throws \Exception |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier) { |
|
169
|
|
|
if (isset($this->fieldTypeMap[$contentTypeIdentifier][$fieldTypeIdentifier])) { |
|
170
|
|
|
return $this->fieldTypeMap[$contentTypeIdentifier][$fieldTypeIdentifier]; |
|
171
|
|
|
} else if (isset($this->fieldTypeMap['*'][$fieldTypeIdentifier])) { |
|
172
|
|
|
return $this->fieldTypeMap['*'][$fieldTypeIdentifier]; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
throw new \Exception("No complex field handler registered for field '$fieldTypeIdentifier' in content type '$contentTypeIdentifier'"); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|