Completed
Push — master ( 65ee7b...5d2ba3 )
by Joschi
02:37
created

PropertyList::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * rdfa-lite
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\RdfaLiteMicrodata\Domain\Property
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\RdfaLiteMicrodata\Domain\Property;
38
39
use Jkphl\RdfaLiteMicrodata\Domain\Exceptions\ErrorException;
40
use Jkphl\RdfaLiteMicrodata\Domain\Exceptions\OutOfBoundsException;
41
use Jkphl\RdfaLiteMicrodata\Domain\Iri\IriInterface;
42
43
/**
44
 * Property list
45
 *
46
 * @package Jkphl\Micrometa
47
 * @subpackage Jkphl\RdfaLiteMicrodata\Domain
48
 */
49
class PropertyList implements PropertyListInterface
50
{
51
    /**
52
     * Use iterator methods
53
     */
54
    use PropertyListIteratorTrait;
55
56
    /**
57
     * Property values
58
     *
59
     * @var array[]
60
     */
61
    protected $values = [];
62
    /**
63
     * Property names
64
     *
65
     * @var IriInterface[]
66
     */
67
    protected $names = [];
68
    /**
69
     * Name cursor mapping
70
     *
71
     * @var int[]
72
     */
73
    protected $nameToCursor = [];
74
    /**
75
     * Internal cursor
76
     *
77
     * @var int
78
     */
79
    protected $cursor = 0;
80
81
    /**
82
     * Unset a property
83
     *
84
     * @param IriInterface|string $iri IRI
85
     * @throws ErrorException
86
     */
87 1
    public function offsetUnset($iri)
88
    {
89 1
        throw new ErrorException(
90 1
            sprintf(ErrorException::CANNOT_UNSET_PROPERTY_STR, $iri),
91 1
            ErrorException::CANNOT_UNSET_PROPERTY,
92 1
            E_WARNING
93
        );
94
    }
95
96
    /**
97
     * Return the number of properties
98
     *
99
     * @return int Number of properties
100
     */
101 5
    public function count()
102
    {
103 5
        return count($this->nameToCursor);
104
    }
105
106
    /**
107
     * Return an array form
108
     *
109
     * @return array Array form
110
     */
111 1
    public function toArray()
112
    {
113 1
        $values = $this->values;
114 1
        return array_map(
115 1
            function ($cursor) use ($values) {
116 1
                return $values[$cursor];
117 1
            },
118 1
            $this->nameToCursor
119
        );
120
    }
121
122
    /**
123
     * Add a property
124
     *
125
     * @param PropertyInterface $property Property
126
     */
127 11
    public function add(PropertyInterface $property)
128
    {
129 11
        $iri = $property->toIri();
130
131
        // Create the property values list if necessary
132 11
        if (!$this->offsetExists($iri)) {
133 11
            $this->offsetSet($iri, [$property]);
134 11
            return;
135
        }
136
137 2
        $this->offsetGet($iri)[] = $property;
138 2
    }
139
140
    /**
141
     * Return whether a property exists
142
     *
143
     * @param IriInterface|string $iri IRI
144
     * @return boolean Property exists
145
     */
146 11
    public function offsetExists($iri)
147
    {
148 11
        return array_key_exists(strval($iri), $this->nameToCursor);
149
    }
150
151
    /**
152
     * Set a particular property
153
     *
154
     * @param IriInterface|string $iri IRI
155
     * @param array $value Property values
156
     */
157 11
    public function offsetSet($iri, $value)
158
    {
159 11
        $iriStr = strval($iri);
160 11
        $cursor = array_key_exists($iriStr, $this->nameToCursor) ?
161 11
            $this->nameToCursor[$iriStr] : ($this->nameToCursor[$iriStr] = count($this->nameToCursor));
162 11
        $this->names[$cursor] = $iri;
163 11
        $this->values[$cursor] = $value;
164 11
    }
165
166
    /**
167
     * Get a particular property
168
     *
169
     * @param IriInterface|string $iri IRI
170
     * @return array Property values
171
     */
172 5
    public function &offsetGet($iri)
173
    {
174 5
        $iriStr = strval($iri);
175
176
        // If the property name is unknown
177 5
        if (!isset($this->nameToCursor[$iriStr])) {
178 1
            throw new OutOfBoundsException(
179 1
                sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $iriStr),
180 1
                OutOfBoundsException::UNKNOWN_PROPERTY_NAME
181
            );
182
        }
183
184 4
        $cursor = $this->nameToCursor[strval($iri)];
185 4
        return $this->values[$cursor];
186
    }
187
}
188