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

ThingGateway::exportProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

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 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * rdfa-lite-microdata
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\RdfaLiteMicrodata
8
 * @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure
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\Infrastructure\Service;
38
39
use Jkphl\RdfaLiteMicrodata\Domain\Property\PropertyInterface;
40
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface;
41
use Jkphl\RdfaLiteMicrodata\Domain\Type\TypeInterface;
42
43
/**
44
 * Thing gateway
45
 *
46
 * @package Jkphl\RdfaLiteMicrodata
47
 * @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure
48
 */
49
class ThingGateway
50
{
51
    /**
52
     * Export things
53
     *
54
     * @param ThingInterface[] $things Things
55
     * @return array Exported things
56
     */
57 4
    public function export(array $things)
58
    {
59 4
        return array_map([$this, 'exportThing'], $things);
60
    }
61
62
    /**
63
     * Export a property
64
     *
65
     * @param PropertyInterface $property Property
66
     * @return ThingInterface|string Exported property
67
     */
68 4
    protected function exportProperty(PropertyInterface $property)
69
    {
70 4
        $value = $property->getValue();
71 4
        return ($value instanceof ThingInterface) ? $this->exportThing($value) : $value;
72
    }
73
74
    /**
75
     * Export a single thing
76
     *
77
     * @param ThingInterface $thing Thing
78
     * @return \stdClass Exported thing
79
     */
80 4
    protected function exportThing(ThingInterface $thing)
81
    {
82 4
        $properties = [];
83 4
        foreach ($thing->getProperties() as $values) {
84 4
            if (count($values)) {
85
                /** @var PropertyInterface $firstProperty */
86 4
                $firstProperty = $values[0];
87 4
                $properties[$firstProperty->getName()] = (object)[
88 4
                    'context' => $firstProperty->getVocabulary()->getUri(),
89 4
                    'values' => array_map([$this, 'exportProperty'], $values)
90 4
                ];
91 4
            }
92 4
        }
93
94
        return (object)[
95 4
            'type' => array_map(
96 4
                function (TypeInterface $type) {
97 4
                    return $type->getVocabulary()->expand($type->getType());
98 4
                },
99 4
                $thing->getTypes()
100 4
            ),
101 4
            'id' => $thing->getResourceId(),
102 4
            'properties' => $properties,
103 4
            'children' => array_map([$this, 'exportThing'], $thing->getChildren()),
104 4
        ];
105
    }
106
}
107