Completed
Push — master ( c0ba30...54b444 )
by Joschi
04:59
created

ThingGateway::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
     * Treat types & property names as IRIs
53
     *
54
     * @var boolean
55
     */
56
    protected $iri;
57
58
    /**
59
     * Abstract parser constructor
60
     *
61
     * @param boolean $iri Treat types & property names as IRIs
62
     */
63 9
    public function __construct($iri = false)
64
    {
65 9
        $this->iri = $iri;
66 9
    }
67
68
    /**
69
     * Export things
70
     *
71
     * @param ThingInterface[] $things Things
72
     * @return array Exported things
73
     */
74 9
    public function export(array $things)
75
    {
76 9
        return array_map([$this, 'exportThing'], $things);
77
    }
78
79
    /**
80
     * Export a property
81
     *
82
     * @param PropertyInterface $property Property
83
     * @return ThingInterface|string Exported property
84
     */
85 9
    protected function exportProperty(PropertyInterface $property)
86
    {
87 9
        $value = $property->getValue();
88 9
        return ($value instanceof ThingInterface) ? $this->exportThing($value) : $value;
89
    }
90
91
    /**
92
     * Export a single thing
93
     *
94
     * @param ThingInterface $thing Thing
95
     * @return \stdClass Exported thing
96
     */
97 9
    protected function exportThing(ThingInterface $thing)
98
    {
99 9
        $iri = $this->iri;
100
101
        return (object)[
102 9
            'type' => array_map(
103 9
                function (TypeInterface $type) use ($iri) {
104
                    return $iri ?
105 8
                        (object)[
106 2
                            'profile' => $type->getVocabulary()->getUri(),
107 2
                            'name' => $type->getType(),
108 2
                        ] :
109 8
                        $type->getVocabulary()->expand($type->getType());
110 9
                },
111 9
                $thing->getTypes()
112 9
            ),
113 9
            'id' => $thing->getResourceId(),
114 9
            'properties' => $this->exportProperties($thing),
115 9
        ];
116
    }
117
118
    /**
119
     * Export the properties list
120
     *
121
     * @param ThingInterface $thing Thing
122
     * @return array Properties list
123
     */
124 9
    protected function exportProperties(ThingInterface $thing)
125
    {
126 9
        $properties = [];
127 9
        foreach ($thing->getProperties() as $propertyIri => $propertyValues) {
128 9
            if (count($propertyValues)) {
129 9
                $propertyValues = array_map([$this, 'exportProperty'], $propertyValues);
130 9
                $properties[strval($propertyIri)] = $this->iri ?
131 9
                    (object)[
132 2
                        'profile' => $propertyIri->getProfile(),
133 2
                        'name' => $propertyIri->getName(),
134 2
                        'values' => $propertyValues,
135 2
                    ] : $propertyValues;
136 9
            }
137 9
        }
138
139 9
        return $properties;
140
    }
141
}
142