Completed
Push — v2 ( 546f8a...9de304 )
by Joschi
06:09
created

JsonLD::parseNodeProperties()   D

Complexity

Conditions 10
Paths 14

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10.4632

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 15
cts 18
cp 0.8333
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 19
nc 14
nop 1
crap 10.4632

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Infrastructure\Parser
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\Micrometa\Infrastructure\Parser;
38
39
use Jkphl\Micrometa\Application\Contract\ParsingResultInterface;
40
use Jkphl\Micrometa\Infrastructure\Parser\JsonLD\CachingContextLoader;
41
use Jkphl\Micrometa\Infrastructure\Parser\JsonLD\VocabularyCache;
42
use Jkphl\Micrometa\Ports\Format;
43
use ML\JsonLD\Exception\JsonLdException;
44
use ML\JsonLD\JsonLD as JsonLDParser;
45
use ML\JsonLD\LanguageTaggedString;
46
use ML\JsonLD\Node;
47
use ML\JsonLD\NodeInterface;
48
use ML\JsonLD\TypedValue;
49
use Psr\Http\Message\UriInterface;
50
use Psr\Log\LoggerInterface;
51
52
/**
53
 * JsonLD parser
54
 *
55
 * @package Jkphl\Micrometa
56
 * @subpackage Jkphl\Micrometa\Infrastructure
57
 * @see https://jsonld-examples.com/
58
 * @see http://www.dr-chuck.com/csev-blog/2016/04/json-ld-performance-sucks-for-api-specs/
59
 */
60
class JsonLD extends AbstractParser
61
{
62
    /**
63
     * Vocabulary cache
64
     *
65
     * @var VocabularyCache
66
     */
67
    protected $vocabularyCache;
68
    /**
69
     * Context loader
70
     *
71
     * @var CachingContextLoader
72
     */
73
    protected $contextLoader;
74
    /**
75
     * Format
76
     *
77
     * @var int
78
     */
79
    const FORMAT = Format::JSON_LD;
80
    /**
81
     * Regex pattern for matching leading comments in a JSON string
82
     *
83
     * @var string
84
     */
85
    const JSON_COMMENT_PATTERN = '#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#';
86
87
    /**
88
     * JSON-LD parser constructor
89
     *
90
     * @param UriInterface $uri Base URI
91
     * @param LoggerInterface $logger Logger
92
     */
93 3
    public function __construct(UriInterface $uri, LoggerInterface $logger)
94
    {
95 3
        parent::__construct($uri, $logger);
96 3
        $this->vocabularyCache = new VocabularyCache();
97 3
        $this->contextLoader = new CachingContextLoader($this->vocabularyCache);
98 3
    }
99
100
    /**
101
     * Parse a DOM document
102
     *
103
     * @param \DOMDocument $dom DOM Document
104
     * @return ParsingResultInterface Micro information items
105
     */
106 2
    public function parseDom(\DOMDocument $dom)
107
    {
108 2
        $this->logger->info('Running parser: '.(new \ReflectionClass(__CLASS__))->getShortName());
109 2
        $items = [];
110
111
        // Find and process all JSON-LD documents
112 2
        $xpath = new \DOMXPath($dom);
113 2
        $jsonLDDocs = $xpath->query('//*[local-name(.) = "script"][@type = "application/ld+json"]');
114 2
        $this->logger->debug('Processing '.$jsonLDDocs->length.' JSON-LD documents');
115
116
        // Run through all JSON-LD documents
117 2
        foreach ($jsonLDDocs as $jsonLDDoc) {
118 2
            $jsonLDDocSource = preg_replace(self::JSON_COMMENT_PATTERN, '', $jsonLDDoc->textContent);
119 2
            $i = $this->parseDocument($jsonLDDocSource);
120 2
            $items = array_merge($items, $i);
121
        }
122
123 2
        return new ParsingResult(self::FORMAT, $items);
124
    }
125
126
    /**
127
     * Parse a JSON-LD document
128
     *
129
     * @param string $jsonLDDocSource JSON-LD document
130
     * @return array Items
131
     */
132 2
    protected function parseDocument($jsonLDDocSource)
133
    {
134
        // Unserialize the JSON-LD document
135 2
        $jsonLDDoc = @json_decode($jsonLDDocSource);
136
137
        // If this is not a valid JSON document: Return
138 2
        if (!is_object($jsonLDDoc) && !is_array($jsonLDDoc)) {
139
            $this->logger->error('Skipping invalid JSON-LD document');
140
            return [];
141
        }
142
143
        // Parse the document
144 2
        return array_filter(
145 2
            is_array($jsonLDDoc) ?
146 2
                array_map([$this, 'parseRootNode'], $jsonLDDoc) : [$this->parseRootNode($jsonLDDoc)]
147
        );
148
    }
149
150
    /**
151
     * Parse a JSON-LD root node
152
     *
153
     * @param \stdClass $jsonLDRoot JSON-LD root node
154
     */
155 2
    protected function parseRootNode($jsonLDRoot)
156
    {
157 2
        $item = null;
158
159
        try {
160 2
            $jsonDLDocument = JsonLDParser::getDocument($jsonLDRoot, ['documentLoader' => $this->contextLoader]);
161
162
            // Run through all nodes to parse the first one
163
            /** @var Node $node */
164 2
            foreach ($jsonDLDocument->getGraph()->getNodes() as $node) {
165 2
                $item = $this->parseNode($node);
166 2
                break;
167
            }
168
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
169
        } catch (JsonLdException $e) {
170
            $this->logger->error($e->getMessage(), ['exception' => $e]);
171
        }
172
173 2
        return $item;
174
    }
175
176
    /**
177
     * Parse a JSON-LD fragment
178
     *
179
     * @param Node|TypedValue|array $jsonLD JSON-LD fragment
180
     * @return mixed Parsed fragment
181
     */
182 2
    protected function parse($jsonLD)
183
    {
184
        // If it's a node object
185 2
        if ($jsonLD instanceof NodeInterface) {
186
            return $this->parseNode($jsonLD);
187
188
            // Else if it's a language tagged string
189 2
        } elseif ($jsonLD instanceof LanguageTaggedString) {
190 2
            return $this->parseLanguageTaggedString($jsonLD);
191
192
            // Else if it's a typed value
193 2
        } elseif ($jsonLD instanceof TypedValue) {
194 2
            return $this->parseTypedValue($jsonLD);
195
196
            // Else if it's a list of items
197 2
        } elseif (is_array($jsonLD)) {
198 2
            return array_map([$this, 'parse'], $jsonLD);
199
        }
200
201
        $this->logger->warning('Unknown JSON-LD item: '.gettype($jsonLD));
202
        return null;
203
    }
204
205
    /**
206
     * Parse a JSON-LD node
207
     *
208
     * @param Node $node Node
209
     * @return \stdClass Item
210
     */
211 2
    protected function parseNode(Node $node)
212
    {
213
        return (object)[
214 2
            'type' => $this->parseNodeType($node),
215 2
            'id' => $node->getId() ?: null,
216 2
            'properties' => $this->parseNodeProperties($node),
217
        ];
218
    }
219
220
    /**
221
     * Parse the type of a JSON-LD node
222
     *
223
     * @param Node $node Node
224
     * @return array Item type
225
     */
226 2
    protected function parseNodeType(Node $node)
227
    {
228
        /** @var Node $itemType */
229 2
        return ($itemType = $node->getType()) ? [$this->vocabularyCache->expandIRI($itemType->getId())] : [];
230
    }
231
232
    /**
233
     * Parse the properties of a JSON-LD node
234
     *
235
     * @param Node $node Node
236
     * @return array Item properties
237
     */
238 2
    protected function parseNodeProperties(Node $node)
239
    {
240 2
        $properties = [];
241
242
        // Run through all node properties
243 2
        foreach ($node->getProperties() as $name => $property) {
244
            // Skip the node type
245 2
            if ($name === Node::TYPE) {
246 2
                continue;
247
            }
248
249
            // Initialize the property
250 2
            if (empty($properties[$name])) {
251 2
                $properties[$name] = $this->vocabularyCache->expandIRI($name);
252 2
                $properties[$name]->values = [];
253
            }
254
255
            // Parse the property value
256 2
            $value = $this->parse($property);
257
258
            // If this is a nested item
259 2
            if (is_object($value)) {
260
                if (isset($value->type) || isset($value->lang)) {
261
                    $properties[$name]->values[] = $value;
262
263
                    // @type = @id
264
                } elseif (isset($value->id)) {
265
                    $properties[$name]->values[] = $value->id;
266
                }
267
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
268 2
            } elseif (is_array($value)) {
269 2
                $properties[$name]->values = array_merge($properties[$name]->values, $value);
270
271
                // Else
272 2
            } elseif ($value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
273 2
                $properties[$name]->values[] = $value;
274
            }
275
        }
276
277 2
        return $properties;
278
    }
279
280
    /**
281
     * Parse a language tagged string
282
     *
283
     * @param LanguageTaggedString $value Language tagged string
284
     * @return string Value
285
     */
286 2
    protected function parseLanguageTaggedString(LanguageTaggedString $value)
287
    {
288
        return (object)['value' => $value->getValue(), 'lang' => $value->getLanguage()];
289 2
    }
290
291
    /**
292
     * Parse a typed value
293
     *
294
     * @param TypedValue $value Typed value
295
     * @return string Value
296
     */
297
    protected function parseTypedValue(TypedValue $value)
298 2
    {
299
        return $value->getValue();
300 2
    }
301
302
    /**
303
     * Filter empty values
304
     *
305
     * @param array|string $value Value
306
     * @return bool Value is not empty
307
     */
308
    protected function filter($value)
309
    {
310
        return is_array($value) ? !!count($value) : strlen($value);
311
    }
312
}
313