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