Completed
Push — master ( 2009ba...6871c4 )
by Joschi
03:48
created

Thing::getVocabulary()   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-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\Type\TypeInterface;
44
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
45
46
/**
47
 * Thing
48
 *
49
 * @package Jkphl\RdfaLiteMicrodata
50
 * @subpackage Jkphl\RdfaLiteMicrodata\Domain
51
 */
52
class Thing implements ThingInterface
53
{
54
    /**
55
     * Resource types
56
     *
57
     * @var TypeInterface[]
58
     */
59
    protected $types;
60
    /**
61
     * Resource ID
62
     *
63
     * @var string|null
64
     */
65
    protected $resourceId = null;
66
    /**
67
     * Child things
68
     *
69
     * @var ThingInterface[]
70
     */
71
    protected $children = [];
72
    /**
73
     * Property
74
     *
75
     * @var array[]
76
     */
77
    protected $properties = [];
78
79
    /**
80
     * Thing constructor
81
     *
82
     * @param TypeInterface|TypeInterface[] $types Type(s)
83
     * @param null|string $resourceId Resource id
84
     */
85 28
    public function __construct($types, $resourceId = null)
86
    {
87 28
        if (!is_array($types)) {
88 28
            $types = [$types];
89 28
        }
90
91
        // Run through all given types
92 28
        foreach ($types as $type) {
93 28
            if (!($type instanceof TypeInterface)) {
94 1
                throw new RuntimeException(
95 1
                    sprintf(RuntimeException::INVALID_TYPE_STR, gettype($type)),
96
                    RuntimeException::INVALID_TYPE
97 1
                );
98
            }
99 27
        }
100
101 27
        $this->types = $types;
102 27
        $this->resourceId = $resourceId;
103 27
    }
104
105
    /**
106
     * Return the resource types
107
     *
108
     * @return TypeInterface[] Resource types
109
     */
110 7
    public function getTypes()
111
    {
112 7
        return $this->types;
113
    }
114
115
    /**
116
     * Return the resource ID
117
     *
118
     * @return null|string Resource ID
119
     */
120 8
    public function getResourceId()
121
    {
122 8
        return $this->resourceId;
123
    }
124
125
    /**
126
     * Add a property value
127
     *
128
     * @param PropertyInterface $property Property
129
     * @return Thing Self reference
130
     */
131 7
    public function addProperty(PropertyInterface $property)
132
    {
133 7
        $name = $property->getVocabulary()->expand($property->getName());
134
135
        // Create the property values list if necessary
136 7
        if (!array_key_exists($name, $this->properties)) {
137 7
            $this->properties[$name] = [];
138 7
        }
139
140
        // Register the property value
141 7
        $this->properties[$name][] = $property;
142
143 7
        return $this;
144
    }
145
146
    /**
147
     * Return all properties
148
     *
149
     * @return array[] Properties
150
     */
151 8
    public function getProperties()
152
    {
153 8
        return $this->properties;
154
    }
155
156
    /**
157
     * Return the values of a single property
158
     *
159
     * @param string $name Property name
160
     * @param VocabularyInterface $vocabulary Vocabulary
161
     * @return array Property values
162
     */
163 4
    public function getProperty($name, VocabularyInterface $vocabulary)
164
    {
165 4
        $name = (new PropertyService())->validatePropertyName($name);
166 4
        $name = $vocabulary->expand($name);
167
168
        // If the property name is unknown
169 4
        if (!array_key_exists($name, $this->properties)) {
170 1
            throw new OutOfBoundsException(
171 1
                sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $name),
172
                OutOfBoundsException::UNKNOWN_PROPERTY_NAME
173 1
            );
174
        }
175
176 3
        return $this->properties[$name];
177
    }
178
179
    /**
180
     * Add a child
181
     *
182
     * @param ThingInterface $child Child
183
     * @return Thing Self reference
184
     */
185 9
    public function addChild(ThingInterface $child)
186
    {
187 9
        $this->children[spl_object_hash($child)] = $child;
188 9
        return $this;
189
    }
190
191
    /**
192
     * Return all children
193
     *
194
     * @return ThingInterface[] Children
195
     */
196 9
    public function getChildren()
197
    {
198 9
        return array_values($this->children);
199
    }
200
}
201