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("class", required=false, type="string"), |
16
|
|
|
* }) |
17
|
|
|
*/ |
18
|
|
|
class Struct extends Annotation |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Class name of structure property |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $class; |
26
|
|
|
/** |
27
|
|
|
* Inline structure properties |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $properties = []; |
32
|
|
|
/** |
33
|
|
|
* Property configuration |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $config = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get structure class |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
68 |
|
public function getClass() |
45
|
|
|
{ |
46
|
68 |
|
return $this->class; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get inline structure properties definition |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
16 |
|
public function getProperties() |
55
|
|
|
{ |
56
|
16 |
|
return $this->properties; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get structure config |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
65 |
|
public function getConfig() |
65
|
|
|
{ |
66
|
65 |
|
return $this->config; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
71 |
|
protected function parseValues(array &$values) |
73
|
|
|
{ |
74
|
71 |
|
parent::parseValues($values); |
75
|
71 |
|
if (array_key_exists('class', $values)) { |
76
|
70 |
|
$this->class = $values['class']; |
77
|
70 |
|
unset($values['class']); |
78
|
70 |
|
} |
79
|
71 |
|
if (array_key_exists('value', $values) && is_array($values['value'])) { |
80
|
16 |
|
$this->properties = $values['value']; |
81
|
16 |
|
unset($values['value']); |
82
|
16 |
|
} |
83
|
71 |
|
$this->config = $values; |
84
|
71 |
|
foreach ($this->properties as $p) { |
85
|
16 |
|
if (!$p instanceof Annotation) { |
86
|
|
|
throw new AnnotationException('Inline structure property definition should be valid annotation'); |
87
|
|
|
} |
88
|
71 |
|
} |
89
|
|
|
// Check if we got required properties |
90
|
71 |
|
if (((!is_string($this->class)) || ($this->class === '')) && (!count($this->properties))) { |
91
|
|
|
// We should have either explicitly defined structure properties or structure class name |
92
|
1 |
|
throw new AnnotationException('Required property annotation is missed: class'); |
93
|
|
|
} |
94
|
70 |
|
} |
95
|
|
|
} |
96
|
|
|
|