|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ubiquity\orm\parser; |
|
3
|
|
|
|
|
4
|
|
|
use Ubiquity\utils\base\UArray; |
|
5
|
|
|
use Ubiquity\contents\transformation\TransformersManager; |
|
6
|
|
|
use Ubiquity\exceptions\TransformerException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Parse model annotation for cache generation. |
|
10
|
|
|
* Ubiquity\orm\parser$ModelParser |
|
11
|
|
|
* This class is part of Ubiquity |
|
12
|
|
|
* |
|
13
|
|
|
* @author jcheron <[email protected]> |
|
14
|
|
|
* @version 1.0.1 |
|
15
|
|
|
* |
|
16
|
|
|
*/ |
|
17
|
|
|
class ModelParser { |
|
18
|
|
|
|
|
19
|
|
|
protected $global; |
|
20
|
|
|
|
|
21
|
|
|
protected $primaryKeys; |
|
22
|
|
|
|
|
23
|
|
|
protected $manytoOneMembers; |
|
24
|
|
|
|
|
25
|
|
|
protected $oneToManyMembers; |
|
26
|
|
|
|
|
27
|
|
|
protected $manyToManyMembers; |
|
28
|
|
|
|
|
29
|
|
|
protected $joinColumnMembers; |
|
30
|
|
|
|
|
31
|
|
|
protected $joinTableMembers; |
|
32
|
|
|
|
|
33
|
|
|
protected $nullableMembers = []; |
|
34
|
|
|
|
|
35
|
6 |
|
protected $notSerializableMembers = []; |
|
36
|
6 |
|
|
|
37
|
6 |
|
protected $fieldNames; |
|
38
|
6 |
|
|
|
39
|
6 |
|
protected $fieldTypes = []; |
|
40
|
6 |
|
|
|
41
|
6 |
|
protected $transformers = []; |
|
42
|
6 |
|
|
|
43
|
6 |
|
protected $accessors = []; |
|
44
|
6 |
|
|
|
45
|
6 |
|
protected $yuml; |
|
46
|
|
|
|
|
47
|
6 |
|
public function parse($modelClass) { |
|
48
|
6 |
|
$instance = new $modelClass(); |
|
49
|
6 |
|
$this->primaryKeys = Reflexion::getKeyFields($instance); |
|
50
|
6 |
|
$this->oneToManyMembers = Reflexion::getMembersAnnotationWithAnnotation($modelClass, "@oneToMany"); |
|
51
|
6 |
|
$this->manytoOneMembers = Reflexion::getMembersNameWithAnnotation($modelClass, "@manyToOne"); |
|
52
|
6 |
|
$this->manyToManyMembers = Reflexion::getMembersAnnotationWithAnnotation($modelClass, "@manyToMany"); |
|
53
|
6 |
|
$this->joinColumnMembers = Reflexion::getMembersAnnotationWithAnnotation($modelClass, "@joinColumn"); |
|
54
|
6 |
|
$this->joinTableMembers = Reflexion::getMembersAnnotationWithAnnotation($modelClass, "@joinTable"); |
|
55
|
5 |
|
$this->transformers = Reflexion::getMembersAnnotationWithAnnotation($modelClass, "@transformer"); |
|
56
|
6 |
|
$yuml = Reflexion::getAnnotationClass($modelClass, "@yuml"); |
|
57
|
6 |
|
if (\sizeof($yuml) > 0) |
|
58
|
6 |
|
$this->yuml = $yuml[0]; |
|
59
|
6 |
|
$properties = Reflexion::getProperties($instance); |
|
60
|
5 |
|
foreach ($properties as $property) { |
|
61
|
6 |
|
$propName = $property->getName(); |
|
62
|
6 |
|
$fieldName = Reflexion::getFieldName($modelClass, $propName); |
|
63
|
6 |
|
$this->fieldNames[$propName] = $fieldName; |
|
64
|
6 |
|
$nullable = Reflexion::isNullable($modelClass, $propName); |
|
65
|
|
|
$serializable = Reflexion::isSerializable($modelClass, $propName); |
|
66
|
|
|
if ($nullable) |
|
67
|
|
|
$this->nullableMembers[] = $propName; |
|
68
|
6 |
|
if (! $serializable) |
|
69
|
6 |
|
$this->notSerializableMembers[] = $propName; |
|
70
|
|
|
$type = Reflexion::getDbType($modelClass, $propName); |
|
71
|
6 |
|
if ($type === false) |
|
72
|
6 |
|
$type = "mixed"; |
|
73
|
6 |
|
$this->fieldTypes[$propName] = $type; |
|
74
|
6 |
|
$accesseur = "set" . ucfirst($propName); |
|
75
|
6 |
|
if (! isset($this->accessors[$fieldName]) && method_exists($modelClass, $accesseur)) { |
|
76
|
6 |
|
$this->accessors[$fieldName] = $accesseur; |
|
77
|
6 |
|
} |
|
78
|
6 |
|
} |
|
79
|
6 |
|
|
|
80
|
6 |
|
$this->global["#tableName"] = Reflexion::getTableName($modelClass); |
|
81
|
6 |
|
} |
|
82
|
|
|
|
|
83
|
6 |
|
public function asArray() { |
|
84
|
5 |
|
$result = $this->global; |
|
85
|
|
|
$result["#primaryKeys"] = $this->primaryKeys; |
|
86
|
6 |
|
$result["#manyToOne"] = $this->manytoOneMembers; |
|
87
|
5 |
|
$result["#fieldNames"] = $this->fieldNames; |
|
88
|
|
|
$result["#fieldTypes"] = $this->fieldTypes; |
|
89
|
|
|
$result["#nullable"] = $this->nullableMembers; |
|
90
|
6 |
|
$result["#notSerializable"] = $this->notSerializableMembers; |
|
91
|
5 |
|
$result["#transformers"] = []; |
|
92
|
|
|
$result["#accessors"] = $this->accessors; |
|
93
|
|
|
if (isset($this->yuml)) |
|
94
|
6 |
|
$result["#yuml"] = $this->yuml->getPropertiesAndValues(); |
|
95
|
6 |
|
foreach ($this->oneToManyMembers as $member => $annotation) { |
|
96
|
6 |
|
$result["#oneToMany"][$member] = $annotation->getPropertiesAndValues(); |
|
97
|
|
|
} |
|
98
|
|
|
foreach ($this->manyToManyMembers as $member => $annotation) { |
|
99
|
|
|
$result["#manyToMany"][$member] = $annotation->getPropertiesAndValues(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
foreach ($this->joinTableMembers as $member => $annotation) { |
|
103
|
|
|
$result["#joinTable"][$member] = $annotation->getPropertiesAndValues(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (class_exists("Ubiquity\\contents\\transformation\\TransformersManager")) { |
|
107
|
|
|
TransformersManager::start(); |
|
108
|
|
|
foreach ($this->transformers as $member => $annotation) { |
|
109
|
|
|
$goodTransformer = false; |
|
110
|
|
|
if (array_search($member, $this->notSerializableMembers, false) !== false) { |
|
111
|
|
|
throw new TransformerException(sprintf('%s member is not serializable and does not supports transformers!', $member)); |
|
112
|
|
|
} |
|
113
|
|
|
$trans = TransformersManager::getTransformerClass($annotation->name); |
|
114
|
|
|
if ($trans == null) { |
|
115
|
|
|
throw new TransformerException(sprintf('%s value is not a declared transformer.', $annotation->name)); |
|
116
|
|
|
} else { |
|
117
|
|
|
foreach (TransformersManager::TRANSFORMER_TYPES as $transType => $transInterface) { |
|
118
|
6 |
|
if (is_subclass_of($trans, $transInterface, true)) { |
|
119
|
6 |
|
$goodTransformer = true; |
|
120
|
6 |
|
$result["#transformers"][$transType][$member] = $trans; |
|
121
|
|
|
} |
|
122
|
6 |
|
} |
|
123
|
|
|
if (! $goodTransformer) { |
|
124
|
|
|
throw new TransformerException(sprintf('%s does not implements %s', $trans, 'TransformerInterfaces')); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
foreach ($this->joinColumnMembers as $member => $annotation) { |
|
131
|
|
|
$result["#joinColumn"][$member] = $annotation->getPropertiesAndValues(); |
|
132
|
|
|
$result["#invertedJoinColumn"][$annotation->name] = [ |
|
133
|
|
|
"member" => $member, |
|
134
|
|
|
"className" => $annotation->className |
|
135
|
|
|
]; |
|
136
|
|
|
} |
|
137
|
|
|
return $result; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function __toString() { |
|
141
|
|
|
return "return " . UArray::asPhpArray($this->asArray(), "array") . ";"; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|