|
1
|
|
|
<?php |
|
2
|
|
|
namespace Graviton\GeneratorBundle\Definition; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Represents a hash of fields as defined in the JSON format |
|
6
|
|
|
* |
|
7
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
8
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
9
|
|
|
* @link http://swisscom.ch |
|
10
|
|
|
*/ |
|
11
|
|
|
class JsonDefinitionHash implements DefinitionElementInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string Name of this hash |
|
15
|
|
|
*/ |
|
16
|
|
|
private $name; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var JsonDefinition |
|
19
|
|
|
*/ |
|
20
|
|
|
private $parent; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var DefinitionElementInterface[] Array of fields.. |
|
23
|
|
|
*/ |
|
24
|
|
|
private $fields = []; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Schema\Field Field definition |
|
27
|
|
|
*/ |
|
28
|
|
|
private $definition; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $name Name of this hash |
|
34
|
|
|
* @param JsonDefinition $parent Parent definiton |
|
35
|
|
|
* @param DefinitionElementInterface[] $fields Fields of the hash |
|
36
|
|
|
* @param Schema\Field $definition Field definition |
|
37
|
|
|
*/ |
|
38
|
21 |
|
public function __construct($name, JsonDefinition $parent, array $fields, Schema\Field $definition = null) |
|
39
|
|
|
{ |
|
40
|
21 |
|
$this->name = $name; |
|
41
|
21 |
|
$this->parent = $parent; |
|
42
|
21 |
|
$this->fields = $fields; |
|
43
|
21 |
|
$this->definition = $definition; |
|
44
|
21 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns the hash name |
|
48
|
|
|
* |
|
49
|
|
|
* @return string Name |
|
50
|
|
|
*/ |
|
51
|
11 |
|
public function getName() |
|
52
|
|
|
{ |
|
53
|
11 |
|
return $this->name; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns the definition as array.. |
|
58
|
|
|
* |
|
59
|
|
|
* @return array the definition |
|
60
|
|
|
*/ |
|
61
|
3 |
|
public function getDefAsArray() |
|
62
|
|
|
{ |
|
63
|
3 |
|
return array_replace( |
|
64
|
|
|
[ |
|
65
|
3 |
|
'name' => $this->getName(), |
|
66
|
3 |
|
'type' => $this->getType(), |
|
67
|
3 |
|
'exposedName' => $this->getName(), |
|
68
|
3 |
|
'doctrineType' => $this->getTypeDoctrine(), |
|
69
|
3 |
|
'serializerType' => $this->getTypeSerializer(), |
|
70
|
3 |
|
'relType' => self::REL_TYPE_EMBED, |
|
71
|
|
|
'isClassType' => true, |
|
72
|
|
|
'constraints' => [], |
|
73
|
|
|
'required' => true, |
|
74
|
|
|
'searchable' => false, |
|
75
|
|
|
], |
|
76
|
3 |
|
$this->definition === null ? [] : [ |
|
77
|
1 |
|
'exposedName' => $this->definition->getExposeAs() ?: $this->getName(), |
|
78
|
|
|
|
|
79
|
1 |
|
'title' => $this->definition->getTitle(), |
|
80
|
1 |
|
'description' => $this->definition->getDescription(), |
|
81
|
1 |
|
'readOnly' => $this->definition->getReadOnly(), |
|
82
|
1 |
|
'required' => $this->definition->getRequired(), |
|
83
|
3 |
|
'searchable' => $this->definition->getSearchable(), |
|
84
|
|
|
] |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritDoc} |
|
90
|
|
|
* |
|
91
|
|
|
* @return string type |
|
92
|
|
|
*/ |
|
93
|
4 |
|
public function getType() |
|
94
|
|
|
{ |
|
95
|
4 |
|
return self::TYPE_HASH; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritDoc} |
|
100
|
|
|
* |
|
101
|
|
|
* @return string type |
|
102
|
|
|
*/ |
|
103
|
4 |
|
public function getTypeDoctrine() |
|
104
|
|
|
{ |
|
105
|
4 |
|
return $this->getClassName(true); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns the field type in a serializer-understandable way.. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string Type |
|
112
|
|
|
*/ |
|
113
|
4 |
|
public function getTypeSerializer() |
|
114
|
|
|
{ |
|
115
|
4 |
|
return $this->getClassName(true); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns the field definition of this hash from "local perspective", |
|
120
|
|
|
* meaning that we only include fields inside this hash BUT with all |
|
121
|
|
|
* the stuff from the json file. this is needed to generate a Document/Model |
|
122
|
|
|
* from this hash (generate a json file again) |
|
123
|
|
|
* |
|
124
|
|
|
* @return JsonDefinition the definition of this hash in a standalone array ready to be json_encoded() |
|
125
|
|
|
*/ |
|
126
|
5 |
|
public function getJsonDefinition() |
|
127
|
|
|
{ |
|
128
|
5 |
|
$definition = (new Schema\Definition()) |
|
129
|
5 |
|
->setId($this->getClassName()) |
|
130
|
5 |
|
->setDescription($this->definition === null ? null : $this->definition->getDescription()) |
|
131
|
5 |
|
->setTitle($this->definition === null ? null : $this->definition->getTitle()) |
|
132
|
5 |
|
->setIsSubDocument(true) |
|
133
|
5 |
|
->setTarget(new Schema\Target()); |
|
134
|
|
|
|
|
135
|
5 |
|
foreach ($this->fields as $field) { |
|
136
|
5 |
|
foreach ($this->processFieldDefinitionsRecursive($field) as $definitions) { |
|
137
|
5 |
|
$definition->getTarget()->addField($definitions); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
5 |
|
foreach ($this->parent->getRelations() as $relation) { |
|
141
|
2 |
|
$relation = $this->processParentRelation($relation); |
|
142
|
2 |
|
if ($relation !== null) { |
|
143
|
2 |
|
$definition->getTarget()->addRelation($relation); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
5 |
|
return new JsonDefinition($definition); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Method getFieldDefinitionsRecursive |
|
152
|
|
|
* |
|
153
|
|
|
* @param DefinitionElementInterface $field |
|
154
|
|
|
* @return Schema\Field[] |
|
155
|
|
|
*/ |
|
156
|
5 |
|
private function processFieldDefinitionsRecursive(DefinitionElementInterface $field) |
|
157
|
|
|
{ |
|
158
|
5 |
|
if ($field instanceof JsonDefinitionField) { |
|
159
|
5 |
|
return [$this->cloneFieldDefinition($field->getDef())]; |
|
160
|
|
|
} elseif ($field instanceof JsonDefinitionArray) { |
|
161
|
4 |
|
return $this->processFieldDefinitionsRecursive($field->getElement()); |
|
162
|
|
|
} elseif ($field instanceof JsonDefinitionHash) { |
|
163
|
2 |
|
return array_reduce( |
|
164
|
2 |
|
$field->fields, |
|
165
|
2 |
|
function (array $subfields, DefinitionElementInterface $subfield) { |
|
166
|
2 |
|
return array_merge($subfields, $this->processFieldDefinitionsRecursive($subfield)); |
|
167
|
2 |
|
}, |
|
168
|
2 |
|
$field->definition === null ? [] : [$this->cloneFieldDefinition($field->definition)] |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown field type "%s"', get_class($field))); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Clone field definition |
|
177
|
|
|
* |
|
178
|
|
|
* @param Schema\Field $field Field |
|
179
|
|
|
* @return Schema\Field |
|
180
|
|
|
*/ |
|
181
|
5 |
|
private function cloneFieldDefinition(Schema\Field $field) |
|
182
|
|
|
{ |
|
183
|
5 |
|
$clone = clone $field; |
|
184
|
5 |
|
$clone->setName(preg_replace('/^'.preg_quote($this->name, '/').'\.(\d+\.)*/', '', $clone->getName())); |
|
185
|
5 |
|
return $clone; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Process parent relation |
|
190
|
|
|
* |
|
191
|
|
|
* @param Schema\Relation $relation Parent relation |
|
192
|
|
|
* @return Schema\Relation|null |
|
193
|
|
|
*/ |
|
194
|
2 |
|
private function processParentRelation(Schema\Relation $relation) |
|
195
|
|
|
{ |
|
196
|
2 |
|
$prefixRegex = '/^'.preg_quote($this->name, '/').'\.(\d+\.)*(?P<sub>.*)/'; |
|
197
|
2 |
|
if (!preg_match($prefixRegex, $relation->getLocalProperty(), $matches)) { |
|
198
|
1 |
|
return null; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
2 |
|
$clone = clone $relation; |
|
202
|
2 |
|
$clone->setLocalProperty($matches['sub']); |
|
203
|
2 |
|
return $clone; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Returns the class name of this hash, possibly |
|
208
|
|
|
* taking the parent element into the name. this |
|
209
|
|
|
* string here results in the name of the generated Document. |
|
210
|
|
|
* |
|
211
|
|
|
* @param boolean $fq if true, we'll return the class name full qualified |
|
212
|
|
|
* |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
10 |
|
private function getClassName($fq = false) |
|
216
|
|
|
{ |
|
217
|
10 |
|
$className = ucfirst($this->parent->getId()).ucfirst($this->getName()); |
|
218
|
10 |
|
if ($fq) { |
|
219
|
5 |
|
$className = $this->parent->getNamespace().'\\Document\\'.$className; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
10 |
|
return $className; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|