AnnotationsCollection   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 78
ccs 16
cts 21
cp 0.7619
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hasAnnotation() 0 5 1
A getAnnotation() 0 5 2
A getAnnotations() 0 9 2
A getAllAnnotations() 0 13 4
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