ResourceObjectCollection   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 6
dl 0
loc 135
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 2
A __construct() 0 4 1
A getIterator() 0 4 1
A has() 0 11 3
A get() 0 12 3
A getAll() 0 4 1
A getIdentifiers() 0 11 2
A isEmpty() 0 4 1
A count() 0 4 1
A add() 0 6 2
A addMany() 0 10 3
1
<?php
2
/**
3
 * ResourceObjectCollection.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:JsonAPIClient!
9
 * @subpackage     Objects
10
 * @since          1.0.0
11
 *
12
 * @date           05.05.18
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\JsonAPIClient\Objects;
18
19
use IPub\JsonAPIClient\Exceptions;
20
21
/**
22
 * Resource objects collection
23
 *
24
 * @package        iPublikuj:JsonAPIClient!
25
 * @subpackage     Objects
26
 *
27
 * @author         Adam Kadlec <[email protected]>
28
 */
29
class ResourceObjectCollection implements IResourceObjectCollection
30
{
31
	/**
32
	 * @var IResourceObject[]
33
	 */
34
	private $stack = [];
35
36
	/**
37
	 * @param array $resources
38
	 *
39
	 * @return ResourceObjectCollection
40
	 */
41
	public static function create(array $resources)
42
	{
43
		$resources = array_map(function ($resource) {
44
			return ($resource instanceof IResourceObject) ? $resource : new ResourceObject($resource);
45
		}, $resources);
46
47
		return new self($resources);
48
	}
49
50
	/**
51
	 * @param array $resources
52
	 */
53
	public function __construct(array $resources = [])
54
	{
55
		$this->addMany($resources);
56
	}
57
58
	/**
59
	 * {@inheritdoc}
60
	 */
61
	public function getIterator() : \ArrayIterator
62
	{
63
		return new \ArrayIterator($this->stack);
64
	}
65
66
	/**
67
	 * {@inheritdoc}
68
	 */
69
	public function has(IResourceIdentifier $identifier) : bool
70
	{
71
		/** @var IResourceObject $resource */
72
		foreach ($this as $resource) {
73
			if ($identifier->isSame($resource->getIdentifier())) {
74
				return true;
75
			}
76
		}
77
78
		return false;
79
	}
80
81
	/**
82
	 * {@inheritdoc}
83
	 */
84
	public function get(IResourceIdentifier $identifier) : IResourceObject
85
	{
86
		/** @var IResourceObject $resource */
87
		foreach ($this as $resource) {
88
89
			if ($identifier->isSame($resource->getIdentifier())) {
90
				return $resource;
91
			}
92
		}
93
94
		throw new Exceptions\RuntimeException('No matching resource in collection: ' . $identifier->toString());
95
	}
96
97
	/**
98
	 * {@inheritdoc}
99
	 */
100
	public function getAll() : array
101
	{
102
		return $this->stack;
103
	}
104
105
	/**
106
	 * {@inheritdoc}
107
	 */
108
	public function getIdentifiers() : IResourceIdentifierCollection
109
	{
110
		$collection = new ResourceIdentifierCollection;
111
112
		/** @var IResourceObject $resource */
113
		foreach ($this as $resource) {
114
			$collection->add($resource->getIdentifier());
115
		}
116
117
		return $collection;
118
	}
119
120
	/**
121
	 * {@inheritdoc}
122
	 */
123
	public function isEmpty() : bool
124
	{
125
		return empty($this->stack);
126
	}
127
128
	/**
129
	 * {@inheritdoc}
130
	 */
131
	public function count() : int
132
	{
133
		return count($this->stack);
134
	}
135
136
	/**
137
	 * @param IResourceObject $resource
138
	 *
139
	 * @return void
140
	 */
141
	public function add(IResourceObject $resource) : void
142
	{
143
		if (!$this->has($resource->getIdentifier())) {
144
			$this->stack[] = $resource;
145
		}
146
	}
147
148
	/**
149
	 * @param array $resources
150
	 *
151
	 * @return void
152
	 */
153
	public function addMany(array $resources) : void
154
	{
155
		foreach ($resources as $resource) {
156
			if (!$resource instanceof IResourceObject) {
157
				throw new Exceptions\InvalidArgumentException('Expecting only resource objects.');
158
			}
159
160
			$this->add($resource);
161
		}
162
	}
163
}
164