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\Ports\Parser; |
38
|
|
|
|
39
|
|
|
use Jkphl\RdfaLiteMicrodata\Application\Context\ContextInterface; |
40
|
|
|
use Jkphl\RdfaLiteMicrodata\Application\Contract\DocumentFactoryInterface; |
41
|
|
|
use Jkphl\RdfaLiteMicrodata\Application\Contract\ElementProcessorInterface; |
42
|
|
|
use Jkphl\RdfaLiteMicrodata\Application\Parser\Parser; |
43
|
|
|
use Jkphl\RdfaLiteMicrodata\Infrastructure\Parser\ParserInterface; |
44
|
|
|
use Jkphl\RdfaLiteMicrodata\Infrastructure\Service\ThingGateway; |
45
|
|
|
use Jkphl\RdfaLiteMicrodata\Ports\Exceptions\OutOfBoundsException; |
46
|
|
|
use Jkphl\RdfaLiteMicrodata\Ports\Exceptions\RuntimeException; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Abstract parser |
50
|
|
|
* |
51
|
|
|
* @package Jkphl\RdfaLiteMicrodata |
52
|
|
|
* @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure |
53
|
|
|
*/ |
54
|
|
|
abstract class AbstractParser implements ParserInterface |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* Treat types & property names as IRIs |
58
|
|
|
* |
59
|
|
|
* @var boolean |
60
|
|
|
*/ |
61
|
|
|
protected $iri; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Abstract parser constructor |
65
|
|
|
* |
66
|
|
|
* @param boolean $iri Treat types & property names as IRIs |
67
|
|
|
*/ |
68
|
15 |
|
public function __construct($iri = false) |
69
|
|
|
{ |
70
|
15 |
|
$this->iri = $iri; |
71
|
15 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the contents of a file |
75
|
|
|
* |
76
|
|
|
* @param string $file File |
77
|
|
|
* @return array Extracted things |
78
|
|
|
* @throws RuntimeException If the file is not readable |
79
|
|
|
*/ |
80
|
13 |
|
protected function getFileContents($file) |
81
|
|
|
{ |
82
|
|
|
// If the file is not readable |
83
|
13 |
|
if (!is_readable($file)) { |
84
|
2 |
|
throw new RuntimeException( |
85
|
2 |
|
sprintf(RuntimeException::INVALID_FILE_STR, $file), |
86
|
|
|
RuntimeException::INVALID_FILE |
87
|
2 |
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
11 |
|
return file_get_contents($file); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Parse a source |
95
|
|
|
* |
96
|
|
|
* @param mixed $source Source |
97
|
|
|
* @param DocumentFactoryInterface $documentFactory Document factory |
98
|
|
|
* @param ElementProcessorInterface $elementProcessor Element processor |
99
|
|
|
* @param ContextInterface $context Context |
100
|
|
|
* @return \stdClass Extracted things |
101
|
|
|
*/ |
102
|
13 |
|
protected function parseSource( |
103
|
|
|
$source, |
104
|
|
|
DocumentFactoryInterface $documentFactory, |
105
|
|
|
ElementProcessorInterface $elementProcessor, |
106
|
|
|
ContextInterface $context |
107
|
|
|
) { |
108
|
|
|
try { |
109
|
13 |
|
$parser = new Parser($documentFactory, $elementProcessor, $context); |
110
|
13 |
|
$things = $parser->parse($source); |
111
|
8 |
|
$gateway = new ThingGateway($this->iri); |
112
|
8 |
|
return (object)['items' => $gateway->export($things)]; |
113
|
5 |
|
} catch (\OutOfBoundsException $e) { |
114
|
2 |
|
throw new OutOfBoundsException($e->getMessage(), $e->getCode()); |
115
|
3 |
|
} catch (\RuntimeException $e) { |
116
|
3 |
|
throw new RuntimeException($e->getMessage(), $e->getCode()); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|