-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAttributeCollection.php
59 lines (48 loc) · 1.63 KB
/
AttributeCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace Undabot\JsonApi\Implementation\Model\Resource\Attribute;
use Undabot\JsonApi\Definition\Model\Resource\Attribute\AttributeCollectionInterface;
use Undabot\JsonApi\Definition\Model\Resource\Attribute\AttributeInterface;
/** @psalm-suppress UnusedClass */
class AttributeCollection implements AttributeCollectionInterface
{
/** @var AttributeInterface[] */
private $attributes;
/** @param AttributeInterface[] $attributes */
public function __construct(array $attributes)
{
$this->makeSureAllAttributesAreValid($attributes);
$this->attributes = $attributes;
}
/** @return AttributeInterface[] */
public function getAttributes(): array
{
return $this->attributes;
}
/**
* @return \ArrayIterator<int,AttributeInterface>
*/
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->getAttributes());
}
public function getAttributeByName(string $name): ?AttributeInterface
{
foreach ($this->attributes as $attribute) {
if ($attribute->getName() === $name) {
return $attribute;
}
}
return null;
}
/** @param AttributeInterface[] $attributes */
private function makeSureAllAttributesAreValid(array $attributes): void
{
foreach ($attributes as $attribute) {
if (false === ($attribute instanceof Attribute)) {
$message = sprintf('Attribute expected, %s given', \get_class($attribute));
throw new \InvalidArgumentException($message);
}
}
}
}