Completed
Push — location_references ( 47d67d )
by
unknown
13:55
created

NamedReferencesCollection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getReference() 0 8 2
A hasReference() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace eZ\Publish\Core\LocationReference\NamedReferences;
6
7
use ArrayIterator;
8
use Countable;
9
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
10
use IteratorAggregate;
11
use Traversable;
12
13
final class NamedReferencesCollection implements IteratorAggregate, Countable
14
{
15
    /** @var string[] */
16
    private $references;
17
18
    public function __construct(array $references)
19
    {
20
        $this->references = $references;
21
    }
22
23
    public function getReference(string $name): string
24
    {
25
        if (!$this->hasReference($name)) {
26
            throw new NotFoundException('named reference', $name);
27
        }
28
29
        return $this->references[$name];
30
    }
31
32
    public function hasReference(string $name): bool
33
    {
34
        return isset($this->references[$name]);
35
    }
36
37
    public function getIterator(): Traversable
38
    {
39
        return new ArrayIterator($this->references);
40
    }
41
42
    public function count(): int
43
    {
44
        return count($this->references);
45
    }
46
}
47