-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmptyClassMetadata.php
209 lines (176 loc) · 5.67 KB
/
EmptyClassMetadata.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
<?php
/*
* This file is part of the Orbitale EntityMerger package.
*
* (c) Alexandre Rock Ancelet <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Orbitale\Component\EntityMerger;
use Doctrine\Common\Annotations\SimpleAnnotationReader;
use ReflectionClass;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
class EmptyClassMetadata implements ClassMetadata
{
protected $className;
/**
* @var ReflectionClass
*/
protected $reflClass;
public function __construct($className)
{
$this->className = $className;
}
public function getName()
{
return $this->className;
}
/**
* @return ReflectionClass
*/
public function getReflectionClass()
{
if (!$this->reflClass) {
$this->reflClass = new ReflectionClass($this->getName());
}
return $this->reflClass;
}
public function hasField($fieldName)
{
return $this->getReflectionClass()->hasProperty($fieldName);
}
public function getFieldNames()
{
$props = [];
$reflProps = $this->getReflectionClass()->getProperties();
foreach ($reflProps as $prop) {
if ($this->hasField($prop->getName())) {
$props[] = $prop->getName();
}
}
return $props;
}
public function getTypeOfField($fieldName)
{
if ($this->getReflectionClass()->hasProperty($fieldName)) {
$reflProp = $this->getReflectionClass()->getProperty($fieldName);
$doc = (string) $reflProp->getDocComment();
if (strpos($doc, '@var') === false) {
// First, if we don't have any "@var" annotation,
// we try to check if the property has a default value
$defaultProps = $this->getReflectionClass()->getDefaultProperties();
if (isset($defaultProps[$fieldName])) {
return gettype($defaultProps[$fieldName]);
}
} else {
return $this->getTypeByAnnotation($reflProp);
}
}
return null;
}
protected function getTypeByAnnotation(\ReflectionProperty $reflProp)
{
preg_match('~@var +(\S+)(?:\[\])?(?:.+)?\n~is', $reflProp->getDocComment(), $annotations);
$annotations = array_map('trim', $annotations);
if (isset($annotations[1])) {
$annotation = str_replace('[]', '', $annotations[1]); // Trim from the potential array notation "type[]"
if ($validType = $this->getValidType($annotation)) {
return $validType;
}
if (class_exists($annotation)) {
return trim($annotation, '\\');
}
if (class_exists($this->getReflectionClass()->getNamespaceName().'\\'.$annotation)) {
return trim($this->getReflectionClass()->getNamespaceName().'\\'.$annotation, '\\');
}
$reader = new SimpleAnnotationReader();
$reader->addNamespace($this->getReflectionClass()->getNamespaceName());
$readAnnotations = $reader->getPropertyAnnotations($reflProp);
foreach ($readAnnotations as $readAnnotation) {
if (strpos($readAnnotation, $annotation) === strlen($readAnnotation) - strlen($annotation)) {
return $readAnnotation;
}
}
$file = $this->getReflectionClass()->getFileName();
$content = file_get_contents($file);
// In case PHP5.5+ and traits
$content = preg_replace('~(class [^{]+{)(?:\s*use [^;]+;)*~iu', '$1', $content);
preg_match_all('~^\s*use ([^;]+);~m', $content, $matches);
$matches = isset($matches[1]) ? $matches[1] : [];
foreach ($matches as $fqcn) {
if (strpos($fqcn, $annotation) === strlen($fqcn) - strlen($annotation)) {
return $fqcn;
}
}
}
return null;
}
public function isIdentifier($fieldName)
{
return null;
}
public function hasAssociation($fieldName)
{
return null;
}
public function isSingleValuedAssociation($fieldName)
{
return null;
}
public function isCollectionValuedAssociation($fieldName)
{
return null;
}
public function isAssociationInverseSide($assocName)
{
return null;
}
public function getAssociationTargetClass($assocName)
{
return null;
}
public function getAssociationMappedByTargetField($assocName)
{
return null;
}
public function getIdentifierValues($object)
{
return null;
}
public function getIdentifier()
{
return null;
}
public function getIdentifierFieldNames()
{
return null;
}
public function getAssociationNames()
{
return null;
}
private function getValidType($type = null)
{
$types = [
'integer' => 'integer',
'number' => 'integer',
'int' => 'integer',
'float' => 'float',
'double' => 'float',
'decimal' => 'float',
'string' => 'string',
'boolean' => 'boolean',
'bool' => 'boolean',
'array' => 'array',
'resource' => 'resource',
'object' => 'object',
'null' => 'null',
'callable' => 'callable',
];
if (isset($types[$type])) {
return $types[$type];
}
return null;
}
}