Thing::getTypes()   A
last analyzed

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
c 0
b 0
f 0
dl 0
loc 4
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\RuntimeException;
40
use Jkphl\RdfaLiteMicrodata\Domain\Property\PropertyInterface;
41
use Jkphl\RdfaLiteMicrodata\Domain\Property\PropertyList;
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 PropertyList
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 33
    public function __construct($types, $resourceId = null)
80
    {
81 33
        if (!is_array($types)) {
82 31
            $types = [$types];
83 31
        }
84
85
        // Run through all given types
86 33
        foreach ($types as $type) {
87 32
            if (!($type instanceof TypeInterface)) {
88 1
                throw new RuntimeException(
89 1
                    sprintf(RuntimeException::INVALID_TYPE_STR, gettype($type)),
90
                    RuntimeException::INVALID_TYPE
91 1
                );
92
            }
93 32
        }
94
95 32
        $this->types = $types;
96 32
        $this->resourceId = $resourceId;
97 32
        $this->properties = new PropertyList();
98 32
    }
99
100
    /**
101
     * Return the resource types
102
     *
103
     * @return TypeInterface[] Resource types
104
     */
105 13
    public function getTypes()
106
    {
107 13
        return $this->types;
108
    }
109
110
    /**
111
     * Return the resource ID
112
     *
113
     * @return null|string Resource ID
114
     */
115 13
    public function getResourceId()
116
    {
117 13
        return $this->resourceId;
118
    }
119
120
    /**
121
     * Add a property value
122
     *
123
     * @param PropertyInterface $property Property
124
     * @return Thing Self reference
125
     */
126 12
    public function addProperty(PropertyInterface $property)
127
    {
128 12
        $this->properties->add($property);
129 12
        return $this;
130
    }
131
132
    /**
133
     * Return all properties
134
     *
135
     * @return PropertyList Properties
136
     */
137 13
    public function getProperties()
138
    {
139 13
        return $this->properties;
140
    }
141
142
    /**
143
     * Return the values of a single property
144
     *
145
     * @param string $name Property name
146
     * @param VocabularyInterface $vocabulary Vocabulary
147
     * @return array Property values
148
     */
149 4
    public function getProperty($name, VocabularyInterface $vocabulary)
150
    {
151 4
        $name = (new PropertyService())->validatePropertyName($name);
152 4
        return $this->properties[$vocabulary->expand($name)];
153
    }
154
}
155