Completed
Pull Request — master (#247)
by Michael
02:36
created

TransientMetadataCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 14
dl 0
loc 57
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A add() 0 8 3
A getIterator() 0 3 1
A include() 0 3 1
A count() 0 3 1
A has() 0 3 1
A get() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Metadata;
6
7
use Doctrine\Annotations\Metadata\Exception\MetadataAlreadyExists;
8
use Doctrine\Annotations\Metadata\Exception\MetadataDoesNotExist;
9
use Traversable;
10
use function array_key_exists;
11
use function array_values;
12
use function count;
13
14
/**
15
 * @internal
16
 */
17
final class TransientMetadataCollection implements MetadataCollection
18
{
19
    /** @var array<string, AnnotationMetadata> */
20
    private $metadata = [];
21
22
    public function __construct(AnnotationMetadata ...$metadatas)
23
    {
24
        $this->add(...$metadatas);
25
    }
26
27
    /**
28
     * @throws MetadataAlreadyExists
29
     */
30
    public function add(AnnotationMetadata ...$metadatas) : void
31
    {
32
        foreach ($metadatas as $metadata) {
33
            if (isset($this->metadata[$metadata->getName()])) {
34
                throw MetadataAlreadyExists::new($metadata);
35
            }
36
37
            $this->metadata[$metadata->getName()] = $metadata;
38
        }
39
    }
40
41
    public function include(MetadataCollection $other) : void
42
    {
43
        $this->add(...$other);
44
    }
45
46
    /**
47
     * @throws MetadataDoesNotExist
48
     */
49
    public function get(string $name) : AnnotationMetadata
50
    {
51
        if (! isset($this->metadata[$name])) {
52
            throw MetadataDoesNotExist::new($name);
53
        }
54
55
        return $this->metadata[$name];
56
    }
57
58
    public function has(string $name) : bool
59
    {
60
        return array_key_exists($name, $this->metadata);
61
    }
62
63
    /**
64
     * @return Traversable<AnnotationMetadata>
65
     */
66
    public function getIterator() : Traversable
67
    {
68
        yield from array_values($this->metadata);
69
    }
70
71
    public function count() : int
72
    {
73
        return count($this->metadata);
74
    }
75
}
76