AttributeContainer::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\Feature;
6
7
use X501\ASN1\Attribute;
8
use X501\ASN1\AttributeType;
9
10
/**
11
 * Trait for objects containing X.501 attributes.
12
 *
13
 * Implements methods for Countable and IteratorAggregate interfaces.
14
 */
15
trait AttributeContainer
16
{
17
    /**
18
     * Array of attributes.
19
     *
20
     * @var Attribute[] $_attributes
21
     */
22
    protected $_attributes;
23
    
24
    /**
25
     * Find first attribute of given name or OID.
26
     *
27
     * @param string $name
28
     * @return Attribute|null
29
     */
30 25
    protected function _findFirst(string $name)
31
    {
32 25
        $oid = AttributeType::attrNameToOID($name);
33 25
        foreach ($this->_attributes as $attr) {
34 24
            if ($attr->oid() == $oid) {
35 24
                return $attr;
36
            }
37
        }
38 2
        return null;
39
    }
40
    
41
    /**
42
     * Check whether attribute is present.
43
     *
44
     * @param string $name OID or attribute name
45
     * @return boolean
46
     */
47 12
    public function has(string $name): bool
48
    {
49 12
        return null !== $this->_findFirst($name);
50
    }
51
    
52
    /**
53
     * Get first attribute by OID or attribute name.
54
     *
55
     * @param string $name OID or attribute name
56
     * @throws \OutOfBoundsException
57
     * @return Attribute
58
     */
59 16
    public function firstOf(string $name): Attribute
60
    {
61 16
        $attr = $this->_findFirst($name);
62 16
        if (!$attr) {
63 1
            throw new \UnexpectedValueException("No $name attribute.");
64
        }
65 15
        return $attr;
66
    }
67
    
68
    /**
69
     * Get all attributes of given name.
70
     *
71
     * @param string $name OID or attribute name
72
     * @return Attribute[]
73
     */
74 3
    public function allOf(string $name): array
75
    {
76 3
        $oid = AttributeType::attrNameToOID($name);
77 3
        $attrs = array_filter($this->_attributes,
78 3
            function (Attribute $attr) use ($oid) {
79 3
                return $attr->oid() == $oid;
80 3
            });
81 3
        return array_values($attrs);
82
    }
83
    
84
    /**
85
     * Get all attributes.
86
     *
87
     * @return Attribute[]
88
     */
89 1
    public function all(): array
90
    {
91 1
        return $this->_attributes;
92
    }
93
    
94
    /**
95
     * Get self with additional attributes added.
96
     *
97
     * @param Attribute ...$attribs
98
     * @return self
99
     */
100 2
    public function withAdditional(Attribute ...$attribs): self
101
    {
102 2
        $obj = clone $this;
103 2
        foreach ($attribs as $attr) {
104 2
            $obj->_attributes[] = $attr;
105
        }
106 2
        return $obj;
107
    }
108
    
109
    /**
110
     * Get self with single unique attribute added.
111
     *
112
     * All previous attributes of the same type are removed.
113
     *
114
     * @param Attribute $attr
115
     * @return self
116
     */
117 5
    public function withUnique(Attribute $attr): self
118
    {
119 5
        $obj = clone $this;
120 5
        $obj->_attributes = array_filter($obj->_attributes,
121 5
            function (Attribute $a) use ($attr) {
122 3
                return $a->oid() != $attr->oid();
123 5
            });
124 5
        $obj->_attributes[] = $attr;
125 5
        return $obj;
126
    }
127
    
128
    /**
129
     * Get number of attributes.
130
     *
131
     * @see \Countable::count()
132
     * @return int
133
     */
134 6
    public function count(): int
135
    {
136 6
        return count($this->_attributes);
137
    }
138
    
139
    /**
140
     * Get iterator for attributes.
141
     *
142
     * @see \IteratorAggregate::getIterator()
143
     * @return \ArrayIterator
144
     */
145 3
    public function getIterator(): \ArrayIterator
146
    {
147 3
        return new \ArrayIterator($this->_attributes);
148
    }
149
}
150