Completed
Push — master ( 2f5cae...701262 )
by Joschi
03:21
created

Thing::addProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * rdfa-lite-microdata
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\RdfaLiteMicrodata
8
 * @subpackage Jkphl\RdfaLiteMicrodata\Domain
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\Thing;
38
39
use Jkphl\RdfaLiteMicrodata\Domain\Exceptions\OutOfBoundsException;
40
use Jkphl\RdfaLiteMicrodata\Domain\Exceptions\RuntimeException;
41
use Jkphl\RdfaLiteMicrodata\Domain\Property\PropertyInterface;
42
use Jkphl\RdfaLiteMicrodata\Domain\Property\PropertyService;
43
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
44
45
/**
46
 * Thing
47
 *
48
 * @package Jkphl\RdfaLiteMicrodata
49
 * @subpackage Jkphl\RdfaLiteMicrodata\Domain
50
 */
51
class Thing implements ThingInterface
52
{
53
    /**
54
     * Resource type
55
     *
56
     * @var string
57
     */
58
    protected $type;
59
    /**
60
     * Resource vocabulary
61
     *
62
     * @var VocabularyInterface
63
     */
64
    protected $vocabulary;
65
    /**
66
     * Resource ID
67
     *
68
     * @var string|null
69
     */
70
    protected $resourceId = null;
71
    /**
72
     * Child things
73
     *
74
     * @var ThingInterface[]
75
     */
76
    protected $children = [];
77
    /**
78
     * Property
79
     *
80
     * @var array[]
81
     */
82
    protected $properties = [];
83
84
    /**
85
     * Thing constructor
86
     *
87
     * @param string $type Resource type
88
     * @param VocabularyInterface $vocabulary Vocabulary in use
89
     * @param null|string $resourceId Resource id
90
     */
91 23
    public function __construct($type, VocabularyInterface $vocabulary, $resourceId = null)
92
    {
93 23
        $type = trim($type);
94 23
        if (!strlen($type)) {
95 1
            throw new RuntimeException(
96 1
                sprintf(RuntimeException::INVALID_RESOURCE_TYPE_STR, $type, $vocabulary->getUri()),
97
                RuntimeException::INVALID_RESOURCE_TYPE
98 1
            );
99
        }
100
101 22
        $this->vocabulary = $vocabulary;
102 22
        $this->type = $type;
103 22
        $this->resourceId = $resourceId;
104 22
    }
105
106
    /**
107
     * Return the resource type
108
     *
109
     * @return string Resource type
110
     */
111 5
    public function getType()
112
    {
113 5
        return $this->type;
114
    }
115
116
    /**
117
     * Return the vocabulary in use
118
     *
119
     * @return VocabularyInterface Vocabulary
120
     */
121 5
    public function getVocabulary()
122
    {
123 5
        return $this->vocabulary;
124
    }
125
126
    /**
127
     * Return the resource ID
128
     *
129
     * @return null|string Resource ID
130
     */
131 6
    public function getResourceId()
132
    {
133 6
        return $this->resourceId;
134
    }
135
136
    /**
137
     * Add a property value
138
     *
139
     * @param PropertyInterface $property Property
140
     * @return Thing Self reference
141
     */
142 5
    public function addProperty(PropertyInterface $property)
143
    {
144 5
        $name = $property->getVocabulary()->expand($property->getName());
145
146
        // Create the property values list if necessary
147 5
        if (!array_key_exists($name, $this->properties)) {
148 5
            $this->properties[$name] = [];
149 5
        }
150
151
        // Register the property value
152 5
        $this->properties[$name][] = $property;
153
154 5
        return $this;
155
    }
156
157
    /**
158
     * Return all properties
159
     *
160
     * @return array[] Properties
161
     */
162 6
    public function getProperties()
163
    {
164 6
        return $this->properties;
165
    }
166
167
    /**
168
     * Return the values of a single property
169
     *
170
     * @param string $name Property name
171
     * @param VocabularyInterface $vocabulary Vocabulary
172
     * @return array Property values
173
     */
174 4
    public function getProperty($name, VocabularyInterface $vocabulary)
175
    {
176 4
        $name = (new PropertyService())->validatePropertyName($name);
177 4
        $name = $vocabulary->expand($name);
178
179
        // If the property name is unknown
180 4
        if (!array_key_exists($name, $this->properties)) {
181 1
            throw new OutOfBoundsException(
182 1
                sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $name),
183
                OutOfBoundsException::UNKNOWN_PROPERTY_NAME
184 1
            );
185
        }
186
187 3
        return $this->properties[$name];
188
    }
189
190
    /**
191
     * Add a child
192
     *
193
     * @param ThingInterface $child Child
194
     * @return Thing Self reference
195
     */
196 6
    public function addChild(ThingInterface $child)
197
    {
198 6
        $this->children[spl_object_hash($child)] = $child;
199 6
        return $this;
200
    }
201
202
    /**
203
     * Return all children
204
     *
205
     * @return ThingInterface[] Children
206
     */
207 7
    public function getChildren()
208
    {
209 7
        return array_values($this->children);
210
    }
211
}
212