Completed
Push — v2 ( 9de304...bf6a94 )
by Joschi
05:08
created

LinkRel::parseDom()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 46
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 7.0022

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 46
ccs 27
cts 28
cp 0.9643
rs 6.7272
c 1
b 0
f 0
cc 7
eloc 22
nc 13
nop 1
crap 7.0022
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\Ports\Format;
41
42
/**
43
 * Link rel parser
44
 *
45
 * @package Jkphl\Micrometa
46
 * @subpackage Jkphl\Micrometa\Infrastructure
47
 */
48
class LinkRel extends AbstractParser
49
{
50
    /**
51
     * Format
52
     *
53
     * @var int
54
     */
55
    const FORMAT = Format::LINK_REL;
56
57
    /**
58
     * Parse a DOM document
59
     *
60
     * @param \DOMDocument $dom DOM Document
61
     * @return ParsingResultInterface Micro information items
62
     */
63 2
    public function parseDom(\DOMDocument $dom)
64
    {
65 2
        $this->logger->info('Running parser: '.(new \ReflectionClass(__CLASS__))->getShortName());
66 2
        $items = [];
67
68 2
        $xpath = new \DOMXPath($dom);
69 2
        $xpath->registerNamespace('html', self::HTML_PROFILE_URI);
70
71
        // Run through all <link> elements with a `rel` attribute
72
        /** @var \DOMElement $linkRel */
73 2
        foreach ($xpath->query('//*[local-name(.) = "link" or local-name(.) = "a"][@rel]') as $linkRel) {
74 2
            $item = new \stdClass();
75
76
            // Collect the item types
77 2
            $item->type = [];
78 2
            foreach (preg_split('/\040+/', $linkRel->getAttribute('rel')) as $rel) {
79 2
                $item->type[] = (object)['name' => $rel, 'profile' => self::HTML_PROFILE_URI];
80 2
            }
81
82
            // Get the item ID (if any)
83 2
            if ($linkRel->hasAttribute('id')) {
84 2
                $item->id = $linkRel->getAttribute('id');
85 2
            }
86
87
            // Run through all item attributes
88 2
            $item->properties = [];
89
            /**
90
             * @var string $attributeName Attribute name
91
             * @var \DOMAttr $attribute Attribute
92
             */
93 2
            foreach ($linkRel->attributes as $attributeName => $attribute) {
94 2
                if (!in_array($attributeName, ['rel', 'id'])) {
95 2
                    $profile = $attribute->lookupNamespaceUri($attribute->prefix ?: null);
96 2
                    $item->properties[] = (object)[
97 2
                        'name' => $attributeName,
98 2
                        'profile' => $profile,
99 2
                        'values' => $this->parseAttributeValue($profile, $attributeName, $attribute->value),
100
                    ];
101 2
                }
102 2
            }
103
104 2
            $items[] = $item;
105 2
        }
106
107 2
        return new ParsingResult(self::FORMAT, $items);
108
    }
109
110
    /**
111
     * Parse an attribute value
112
     *
113
     * @param string $profile Profile
114
     * @param string $attribute Attribute name
115
     * @param string $value Attribute value
116
     * @return array Attribute values
117
     */
118 2
    protected function parseAttributeValue($profile, $attribute, $value)
119
    {
120
        // If it's a HTML attribute
121 2
        if ($profile == LinkRel::HTML_PROFILE_URI) {
122
            switch ($attribute) {
123
                // Space delimited lists
124 2
                case 'sizes':
125 2
                    return array_filter(preg_split('/\040+/', $value));
126
                // Space or comma delimited lists
127 2
                case 'charset':
128 2
                    return array_filter(preg_split('/[,\040]+/', $value));
129
            }
130 2
        }
131
132 2
        return [$value];
133
    }
134
}
135