AbstractList::getClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Ocsp\Asn1\Element;
4
5
use Ocsp\Asn1\Element;
6
use Ocsp\Asn1\Encoder;
7
use Ocsp\Asn1\TaggableElement;
8
use Ocsp\Asn1\TaggableElementTrait;
9
10
/**
11
 * Base handy class for CONSTRUCTED ASN.1 elements.
12
 */
13
abstract class AbstractList implements TaggableElement
14
{
15
    use TaggableElementTrait;
16
17
    /**
18
     * The child elements.
19
     *
20
     * @var \Ocsp\Asn1\Element[]
21
     */
22
    private $elements = [];
23
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * @see \Ocsp\Asn1\Element::getClass()
28
     */
29 3
    public function getClass()
30
    {
31 3
        return static::CLASS_UNIVERSAL;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     *
37
     * @see \Ocsp\Asn1\Element::isConstructed()
38
     */
39 2
    public function isConstructed()
40
    {
41 2
        return true;
42
    }
43
44
    /**
45
     * Get the child elements.
46
     *
47
     * @return \Ocsp\Asn1\Element[]
48
     */
49 3
    public function getElements()
50
    {
51 3
        return $this->elements;
52
    }
53
54
    /**
55
     * Add a new child element.
56
     *
57
     * @param \Ocsp\Asn1\Element $element
58
     *
59
     * @return $this
60
     */
61 3
    public function addElement(Element $element)
62
    {
63 3
        $this->elements[] = $element;
64
65 3
        return $this;
66
    }
67
68
    /**
69
     * Add child elements.
70
     *
71
     * @param \Ocsp\Asn1\Element[] $elements
72
     *
73
     * @return $this
74
     */
75 3
    public function addElements(array $elements)
76
    {
77 3
        foreach ($elements as $element) {
78 3
            $this->addElement($element);
79
        }
80
81 3
        return $this;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     *
87
     * @see \Ocsp\Asn1\Element::getEncodedValue()
88
     */
89 2
    public function getEncodedValue(Encoder $encoder)
90
    {
91 2
        $elementBytes = [];
92 2
        foreach ($this->getElements() as $element) {
93 2
            $elementBytes[] = $encoder->encodeElement($element);
94
        }
95
96 2
        return implode('', $elementBytes);
97
    }
98
99
    /**
100
     * Find the first child of a specific type.
101
     *
102
     * @param int|string|\phpseclib\Math\BigInteger|\phpseclib3\Math\BigInteger $typeID
0 ignored issues
show
Bug introduced by
The type phpseclib\Math\BigInteger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
103
     * @param string $class
104
     * @param string $tagEnvironment
105
     *
106
     * @return \Ocsp\Asn1\Element|null
107
     */
108 3
    public function getFirstChildOfType($typeID, $class = Element::CLASS_UNIVERSAL, $tagEnvironment = '')
109
    {
110 3
        return $this->getNthChildOfType(1, $typeID, $class, $tagEnvironment);
111
    }
112
113
    /**
114
     * Find the Nth child of a specific type.
115
     *
116
     * @param int $position
117
     * @param int|string|\phpseclib\Math\BigInteger|\phpseclib3\Math\BigInteger $typeID
118
     * @param string $class
119
     * @param string $tagEnvironment
120
     *
121
     * @return \Ocsp\Asn1\Element|null
122
     */
123 3
    public function getNthChildOfType($position, $typeID, $class = Element::CLASS_UNIVERSAL, $tagEnvironment = '')
124
    {
125 3
        $typeIDString = (string) $typeID;
126 3
        $found = 0;
127 3
        foreach ($this->getElements() as $element) {
128 3
            $tag = $element instanceof TaggableElement ? $element->getTag() : null;
129 3
            $actualTypeIDString = (string) ($tag === null ? $element->getTypeID() : $tag->getTagID());
130 3
            if ($actualTypeIDString !== $typeIDString) {
131 3
                continue;
132
            }
133 3
            $actualClass = $tag === null ? $element->getClass() : $tag->getClass();
134 3
            if ($actualClass !== $class) {
135 3
                continue;
136
            }
137 3
            if ($tagEnvironment === '') {
138 3
                if ($tag !== null) {
139 3
                    continue;
140
                }
141
            } else {
142 3
                if ($tag === null || $tag->getEnvironment() !== $tagEnvironment) {
143 1
                    continue;
144
                }
145
            }
146 3
            $found++;
147 3
            if ($found === $position) {
148 3
                return $element;
149
            }
150
        }
151
152
        return null;
153
    }
154
155
    /**
156
     * Find the Nth child of an untagged element with a specific class.
157
     *
158
     * @param int $position
159
     * @param string $class
160
     *
161
     * @return \Ocsp\Asn1\Element|null
162
     */
163
    public function getNthUntaggedChild($position, $class)
164
    {
165
        $found = 0;
166
        foreach ($this->getElements() as $element) {
167
            if ($element instanceof TaggableElement) {
168
                if ($element->getTag() !== null) {
169
                    continue;
170
                }
171
            }
172
            if ($element->getClass() !== $class) {
173
                continue;
174
            }
175
            $found++;
176
            if ($found === $position) {
177
                return $element;
178
            }
179
        }
180
181
        return null;
182
    }
183
}
184