Completed
Push — master ( a738a2...ae9dae )
by Joschi
02:37
created

AbstractObject::getAbstract()   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 1
Bugs 0 Features 0
Metric Value
c 1
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
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 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 © 2016 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 Apparat\Object\Domain\Model\Object;
38
39
use Apparat\Object\Domain\Model\Author\AuthorInterface;
40
use Apparat\Object\Domain\Model\Path\RepositoryPathInterface;
41
use Apparat\Object\Domain\Model\Properties\AbstractDomainProperties;
42
use Apparat\Object\Domain\Model\Properties\InvalidArgumentException as PropertyInvalidArgumentException;
43
use Apparat\Object\Domain\Model\Properties\MetaProperties;
44
use Apparat\Object\Domain\Model\Properties\ProcessingInstructions;
45
use Apparat\Object\Domain\Model\Properties\Relations;
46
use Apparat\Object\Domain\Model\Properties\SystemProperties;
47
48
/**
49
 * Abstract object
50
 *
51
 * @package Apparat\Object
52
 * @subpackage Apparat\Object\Domain
53
 */
54
abstract class AbstractObject implements ObjectInterface
55
{
56
    /**
57
     * System properties
58
     *
59
     * @var SystemProperties
60
     */
61
    protected $systemProperties;
62
    /**
63
     * Meta properties
64
     *
65
     * @var MetaProperties
66
     */
67
    protected $metaProperties;
68
    /**
69
     * Domain properties
70
     *
71
     * @var AbstractDomainProperties
72
     */
73
    protected $domainProperties;
74
    /**
75
     * Object payload
76
     *
77
     * @var string
78
     */
79
    protected $payload;
80
    /**
81
     * Repository path
82
     *
83
     * @var RepositoryPathInterface
84
     */
85
    protected $path;
86
    /**
87
     * Domain property collection class
88
     *
89
     * @var string
90
     */
91
    protected $domainPropertyCollectionClass = null;
92
    /**
93
     * Object relations
94
     *
95
     * @var Relations
96
     */
97
    private $relations;
98
    /**
99
     * Processing instructions
100
     *
101
     * @var ProcessingInstructions
102
     */
103
    private $processingInstructions;
104
105
    /**
106
     * Object constructor
107
     *
108
     * @param RepositoryPathInterface $path Object repository path
109
     * @param array $propertyData Property data
110
     * @param string $payload Object payload
111
     * @throws PropertyInvalidArgumentException If the domain property collection class is invalid
112
     */
113 5
    public function __construct(RepositoryPathInterface $path, array $propertyData = [], $payload = '')
114
    {
115
        // If the domain property collection class is invalid
116 5
        if (!is_subclass_of($this->domainPropertyCollectionClass, AbstractDomainProperties::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Apparat\Object\Domain\M...DomainProperties::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
117 1
            throw new PropertyInvalidArgumentException(
118 1
                sprintf(
119 1
                    'Invalid domain property collection class "%s"',
120 1
                    $this->domainPropertyCollectionClass
121 1
                ),
122
                PropertyInvalidArgumentException::INVALID_DOMAIN_PROPERTY_COLLECTION_CLASS
123 1
            );
124
        }
125
126 4
        $this->payload = $payload;
127 4
        $this->path = $path;
128
129
        // Instantiate the system properties
130 4
        $systemPropertyData = (empty($propertyData[SystemProperties::COLLECTION]) || !is_array(
131 4
                $propertyData[SystemProperties::COLLECTION]
132 4
            )) ? [] : $propertyData[SystemProperties::COLLECTION];
133 4
        $this->systemProperties = new SystemProperties($systemPropertyData, $this);
134
135
        // Instantiate the meta properties
136 3
        $metaPropertyData = (empty($propertyData[MetaProperties::COLLECTION]) || !is_array(
137 3
                $propertyData[MetaProperties::COLLECTION]
138 3
            )) ? [] : $propertyData[MetaProperties::COLLECTION];
139 3
        $this->metaProperties = new MetaProperties($metaPropertyData, $this);
140
141
        // Instantiate the domain properties
142 2
        $domainPropertyData = (empty($propertyData[AbstractDomainProperties::COLLECTION]) || !is_array(
143 2
                $propertyData[AbstractDomainProperties::COLLECTION]
144 2
            )) ? [] : $propertyData[AbstractDomainProperties::COLLECTION];
145 2
        $this->domainProperties = new $this->domainPropertyCollectionClass($domainPropertyData, $this);
146
147
        // Instantiate the processing instructions
148 2
        $processingInstructionData = (empty($propertyData[ProcessingInstructions::COLLECTION]) || !is_array(
149 2
                $propertyData[ProcessingInstructions::COLLECTION]
150 2
            )) ? [] : $propertyData[ProcessingInstructions::COLLECTION];
151 2
        $this->processingInstructions = new ProcessingInstructions($processingInstructionData, $this);
152
153
        // Instantiate the object relations
154 2
        $relationData = (empty($propertyData[Relations::COLLECTION]) || !is_array(
155 2
                $propertyData[Relations::COLLECTION]
156 2
            )) ? [] : $propertyData[Relations::COLLECTION];
157 2
        $this->relations = new Relations($relationData, $this);
158 2
    }
159
160
    /**
161
     * Return the object ID
162
     *
163
     * @return Id Object ID
164
     */
165 5
    public function getId()
166
    {
167 5
        return $this->systemProperties->getId();
168
    }
169
170
    /**
171
     * Return the object type
172
     *
173
     * @return Type Object type
174
     */
175 1
    public function getType()
176
    {
177 1
        return $this->systemProperties->getType();
178
    }
179
180
    /**
181
     * Return the object revision
182
     *
183
     * @return Revision Object revision
184
     */
185 1
    public function getRevision()
186
    {
187 1
        return $this->systemProperties->getRevision();
188
    }
189
190
    /**
191
     * Return the creation date & time
192
     *
193
     * @return \DateTimeImmutable Creation date & time
194
     */
195 1
    public function getCreated()
196
    {
197 1
        return $this->systemProperties->getCreated();
198
    }
199
200
    /**
201
     * Return the publication date & time
202
     *
203
     * @return \DateTimeImmutable Publication date & time
204
     */
205 1
    public function getPublished()
206
    {
207 1
        return $this->systemProperties->getPublished();
208
    }
209
210
    /**
211
     * Return the object hash
212
     *
213
     * @return string Object hash
214
     */
215 1
    public function getHash()
216
    {
217 1
        return $this->systemProperties->getHash();
218
    }
219
220
    /**
221
     * Return the object description
222
     *
223
     * @return string Object description
224
     */
225 1
    public function getDescription()
226
    {
227 1
        return $this->metaProperties->getDescription();
228
    }
229
230
    /**
231
     * Return the object abstract
232
     *
233
     * @return string Object abstract
234
     */
235 1
    public function getAbstract()
236
    {
237 1
        return $this->metaProperties->getAbstract();
238
    }
239
240
241
    /**
242
     * Return all object keywords
243
     *
244
     * @return array Object keywords
245
     */
246 1
    public function getKeywords()
247
    {
248 1
        return $this->metaProperties->getKeywords();
249
    }
250
251
    /**
252
     * Return all object categories
253
     *
254
     * @return array Object categories
255
     */
256 1
    public function getCategories()
257
    {
258 1
        return $this->metaProperties->getCategories();
259
    }
260
261
    /**
262
     * Return all object authors
263
     *
264
     * @return AuthorInterface[] Authors
265
     */
266 2
    public function getAuthors()
267
    {
268 2
        return $this->metaProperties->getAuthors();
269
    }
270
271
    /**
272
     * Add an object author
273
     *
274
     * @param AuthorInterface $author Author
275
     * @return ObjectInterface Self reference
276
     */
277 1
    public function addAuthor(AuthorInterface $author)
278
    {
279 1
        $authors = $this->metaProperties->getAuthors();
280 1
        $authors[] = $author;
281 1
        $this->metaProperties->setAuthors($authors);
282 1
        return $this;
283
    }
284
285
    /**
286
     * Return the object repository path
287
     *
288
     * @return RepositoryPathInterface Object repository path
289
     */
290 2
    public function getRepositoryPath()
291
    {
292 2
        return $this->path;
293
    }
294
295
    /**
296
     * Return the object property data
297
     *
298
     * @return array Object property data
299
     */
300 1
    public function getPropertyData()
301
    {
302
        $propertyData = [
303 1
            SystemProperties::COLLECTION => $this->systemProperties->toArray(),
304 1
            MetaProperties::COLLECTION => $this->metaProperties->toArray(),
305 1
            AbstractDomainProperties::COLLECTION => $this->domainProperties->toArray(),
306 1
            ProcessingInstructions::COLLECTION => $this->processingInstructions->toArray(),
307 1
            Relations::COLLECTION => $this->relations->toArray(),
308 1
        ];
309
310 1
        return $propertyData;
311
    }
312
313
    /**
314
     * Return the object payload
315
     *
316
     * @return string Object payload
317
     */
318 1
    public function getPayload()
319
    {
320 1
        return $this->payload;
321
    }
322
323
    /**
324
     * Return the absolute object URL
325
     *
326
     * @return string
327
     */
328 1
    public function getAbsoluteUrl()
329
    {
330 1
        return getenv('APPARAT_BASE_URL') . ltrim($this->path->getRepository()->getUrl(), '/') . strval($this->path);
331
    }
332
333
    /**
334
     * Get a particular property value
335
     *
336
     * Multi-level properties might be traversed by property name paths separated with colons (":").
337
     *
338
     * @param string $property Property name
339
     * @return mixed Property value
340
     */
341 2
    public function getDomainProperty($property)
342
    {
343 2
        return $this->domainProperties->getProperty($property);
344
    }
345
}
346