1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flying\Struct\Annotation; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Annotation\Attribute; |
6
|
|
|
use Doctrine\Common\Annotations\Annotation\Attributes; |
7
|
|
|
use Doctrine\Common\Annotations\Annotation\Target; |
8
|
|
|
use Doctrine\Common\Annotations\AnnotationException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @Annotation |
12
|
|
|
* @Target({"CLASS", "ANNOTATION"}) |
13
|
|
|
* @Attributes({ |
14
|
|
|
* @Attribute("name", required=true, type="string"), |
15
|
|
|
* @Attribute("type", required=false, type="string"), |
16
|
|
|
* @Attribute("default", required=false, type="mixed"), |
17
|
|
|
* @Attribute("nullable", required=false, type="boolean") |
18
|
|
|
* }) |
19
|
|
|
*/ |
20
|
|
|
class Property extends Annotation |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Types mapping to allow use of standard type names |
24
|
|
|
* in PHP 7.x where use of reserved words as class names is not allowed |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $typesMap = [ |
29
|
|
|
'string' => 'str', |
30
|
|
|
'int' => 'integer', |
31
|
|
|
]; |
32
|
|
|
/** |
33
|
|
|
* Property type |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $type; |
38
|
|
|
/** |
39
|
|
|
* Property configuration |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $config = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get property type |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
181 |
|
public function getType() |
51
|
|
|
{ |
52
|
181 |
|
return $this->type; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get property config |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
179 |
|
public function getConfig() |
61
|
|
|
{ |
62
|
179 |
|
return $this->config; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
185 |
|
protected function parseValues(array &$values) |
69
|
|
|
{ |
70
|
185 |
|
parent::parseValues($values); |
71
|
184 |
|
$this->type = $this->getDefaultType(); |
72
|
184 |
|
if (array_key_exists('type', $values)) { |
73
|
150 |
|
$this->type = $values['type']; |
74
|
150 |
|
if (array_key_exists($this->type, self::$typesMap)) { |
75
|
146 |
|
$this->type = self::$typesMap[$this->type]; |
76
|
146 |
|
} |
77
|
150 |
|
unset($values['type']); |
78
|
150 |
|
} |
79
|
184 |
|
$this->config = $values; |
80
|
|
|
// Check if we got required properties |
81
|
184 |
|
if ((!is_string($this->type)) || ($this->type === '')) { |
82
|
1 |
|
throw new AnnotationException('Required property annotation is missed: type'); |
83
|
|
|
} |
84
|
183 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get default property type |
88
|
|
|
* |
89
|
|
|
* @return string|null |
90
|
|
|
*/ |
91
|
184 |
|
protected function getDefaultType() |
92
|
|
|
{ |
93
|
184 |
|
$type = explode('\\', get_class($this)); |
94
|
184 |
|
$type = array_pop($type); |
95
|
184 |
|
$type = lcfirst($type); |
96
|
184 |
|
return ($type !== 'property') ? $type : null; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|