Completed
Push — master ( c82ee6...a08768 )
by Joschi
03:01
created

Thing::getChildren()   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
     * Property
68
     *
69
     * @var array[]
70
     */
71
    protected $properties = [];
72
73
    /**
74
     * Thing constructor
75
     *
76
     * @param TypeInterface|TypeInterface[] $types Type(s)
77
     * @param null|string $resourceId Resource id
78
     */
79 30
    public function __construct($types, $resourceId = null)
80
    {
81 30
        if (!is_array($types)) {
82 28
            $types = [$types];
83
        }
84
85
        // Run through all given types
86 30
        foreach ($types as $type) {
87 29
            if (!($type instanceof TypeInterface)) {
88 1
                throw new RuntimeException(
89 1
                    sprintf(RuntimeException::INVALID_TYPE_STR, gettype($type)),
90 29
                    RuntimeException::INVALID_TYPE
91
                );
92
            }
93
        }
94
95 29
        $this->types = $types;
96 29
        $this->resourceId = $resourceId;
97 29
    }
98
99
    /**
100
     * Return the resource types
101
     *
102
     * @return TypeInterface[] Resource types
103
     */
104 9
    public function getTypes()
105
    {
106 9
        return $this->types;
107
    }
108
109
    /**
110
     * Return the resource ID
111
     *
112
     * @return null|string Resource ID
113
     */
114 9
    public function getResourceId()
115
    {
116 9
        return $this->resourceId;
117
    }
118
119
    /**
120
     * Add a property value
121
     *
122
     * @param PropertyInterface $property Property
123
     * @return Thing Self reference
124
     */
125 8
    public function addProperty(PropertyInterface $property)
126
    {
127 8
        $name = $property->getVocabulary()->expand($property->getName());
128
129
        // Create the property values list if necessary
130 8
        if (!array_key_exists($name, $this->properties)) {
131 8
            $this->properties[$name] = [];
132
        }
133
134
        // Register the property value
135 8
        $this->properties[$name][] = $property;
136
137 8
        return $this;
138
    }
139
140
    /**
141
     * Return all properties
142
     *
143
     * @return array[] Properties
144
     */
145 9
    public function getProperties()
146
    {
147 9
        return $this->properties;
148
    }
149
150
    /**
151
     * Return the values of a single property
152
     *
153
     * @param string $name Property name
154
     * @param VocabularyInterface $vocabulary Vocabulary
155
     * @return array Property values
156
     */
157 4
    public function getProperty($name, VocabularyInterface $vocabulary)
158
    {
159 4
        $name = (new PropertyService())->validatePropertyName($name);
160 4
        $name = $vocabulary->expand($name);
161
162
        // If the property name is unknown
163 4
        if (!array_key_exists($name, $this->properties)) {
164 1
            throw new OutOfBoundsException(
165 1
                sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $name),
166 1
                OutOfBoundsException::UNKNOWN_PROPERTY_NAME
167
            );
168
        }
169
170 3
        return $this->properties[$name];
171
    }
172
}
173