PropertyList   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 140
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetUnset() 0 8 1
A count() 0 4 1
A toArray() 0 10 1
A add() 0 13 2
A offsetExists() 0 4 1
A offsetSet() 0 8 2
A offsetGet() 0 15 2
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
            E_WARNING
93 1
        );
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 1
        );
120
    }
121
122
    /**
123
     * Add a property
124
     *
125
     * @param PropertyInterface $property Property
126
     */
127 14
    public function add(PropertyInterface $property)
128
    {
129 14
        $iri = $property->toIri();
130
131
        // Create the property values list if necessary
132 14
        if (!$this->offsetExists($iri)) {
133 14
            $this->offsetSet($iri, [$property]);
134 14
            return;
135
        }
136
137 3
        $propertyStore =& $this->offsetGet($iri);
138 3
        $propertyStore[] = $property;
139 4
    }
140
141
    /**
142
     * Return whether a property exists
143
     *
144
     * @param IriInterface|string $iri IRI
145
     * @return boolean Property exists
146
     */
147 14
    public function offsetExists($iri)
148
    {
149 14
        return array_key_exists(strval($iri), $this->nameToCursor);
150
    }
151
152
    /**
153
     * Set a particular property
154
     *
155
     * @param IriInterface|string $iri IRI
156
     * @param array $value Property values
157
     */
158 14
    public function offsetSet($iri, $value)
159
    {
160 14
        $iriStr = strval($iri);
161 14
        $cursor = array_key_exists($iriStr, $this->nameToCursor) ?
162 14
            $this->nameToCursor[$iriStr] : ($this->nameToCursor[$iriStr] = count($this->nameToCursor));
163 14
        $this->names[$cursor] = $iri;
164 14
        $this->values[$cursor] = $value;
165 14
    }
166
167
    /**
168
     * Get a particular property
169
     *
170
     * @param IriInterface|string $iri IRI
171
     * @return array Property values
172
     */
173 6
    public function &offsetGet($iri)
174
    {
175 6
        $iriStr = strval($iri);
176
177
        // If the property name is unknown
178 6
        if (!isset($this->nameToCursor[$iriStr])) {
179 1
            throw new OutOfBoundsException(
180 1
                sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $iriStr),
181
                OutOfBoundsException::UNKNOWN_PROPERTY_NAME
182 1
            );
183
        }
184
185 5
        $cursor = $this->nameToCursor[strval($iri)];
186 5
        return $this->values[$cursor];
187
    }
188
}
189