Completed
Push — master ( acb0bf...7018dc )
by Joschi
02:52
created

Thing::addChild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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