|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL, Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/addendum |
|
7
|
|
|
* @licence AGPL, Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes) |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes) |
|
10
|
|
|
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser) |
|
11
|
|
|
* @link https://maslosoft.com/addendum/ - maslosoft addendum |
|
12
|
|
|
* @link https://code.google.com/p/addendum/ - original addendum project |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Maslosoft\Addendum\Collections; |
|
16
|
|
|
|
|
17
|
|
|
use Maslosoft\Addendum\Addendum; |
|
18
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotationInterface; |
|
19
|
|
|
|
|
20
|
|
|
class AnnotationsCollection |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Annotations holder |
|
25
|
|
|
* @var AnnotationInterface[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $annotations; |
|
28
|
|
|
|
|
29
|
58 |
|
public function __construct($annotations) |
|
30
|
|
|
{ |
|
31
|
58 |
|
$this->annotations = $annotations; |
|
32
|
58 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Check if has annotation. |
|
36
|
|
|
* This method should be used with annotation name, not class name, ie: |
|
37
|
|
|
* |
|
38
|
|
|
* ``` |
|
39
|
|
|
* $this->hasAnnotation('Label'); |
|
40
|
|
|
* ``` |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $name |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
55 |
|
public function hasAnnotation($name) |
|
46
|
|
|
{ |
|
47
|
55 |
|
$class = Addendum::resolveClassName($name); |
|
48
|
55 |
|
return isset($this->annotations[$class]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get annotation. |
|
53
|
|
|
* This method should be used with annotation name, not class name, ie: |
|
54
|
|
|
* |
|
55
|
|
|
* ``` |
|
56
|
|
|
* $this->getAnnotation('Label'); |
|
57
|
|
|
* ``` |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $name |
|
60
|
|
|
* @return AnnotationInterface|bool |
|
61
|
|
|
*/ |
|
62
|
17 |
|
public function getAnnotation($name) |
|
63
|
|
|
{ |
|
64
|
17 |
|
$class = Addendum::resolveClassName($name); |
|
65
|
17 |
|
return isset($this->annotations[$class]) ? end($this->annotations[$class]) : false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getAnnotations() |
|
69
|
|
|
{ |
|
70
|
|
|
$result = []; |
|
71
|
|
|
foreach ($this->annotations as $instances) |
|
72
|
|
|
{ |
|
73
|
|
|
$result[] = end($instances); |
|
74
|
|
|
} |
|
75
|
|
|
return $result; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get all annotations with optional restriction to $restriction annotation name |
|
80
|
|
|
* @param string $restriction |
|
81
|
|
|
* @return AnnotationInterface[] |
|
82
|
|
|
*/ |
|
83
|
58 |
|
public function getAllAnnotations($restriction = false) |
|
84
|
|
|
{ |
|
85
|
58 |
|
$restriction = Addendum::resolveClassName($restriction); |
|
86
|
58 |
|
$result = []; |
|
87
|
58 |
|
foreach ($this->annotations as $class => $instances) |
|
88
|
|
|
{ |
|
89
|
54 |
|
if (!$restriction || $restriction == $class) |
|
90
|
|
|
{ |
|
91
|
54 |
|
$result = array_merge($result, $instances); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
58 |
|
return $result; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|