Completed
Push — v2 ( e198fb...470733 )
by Joschi
04:46
created

LinkRel::parseAttributeValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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
     * HTML namespace
58
     *
59
     * @var string
60
     */
61
    const HTML_PROFILE_URI = 'http://www.w3.org/1999/xhtml';
62
63
    /**
64
     * Parse a DOM document
65
     *
66
     * @param \DOMDocument $dom DOM Document
67
     * @return ParsingResultInterface Micro information items
68
     */
69 1
    public function parseDom(\DOMDocument $dom)
70
    {
71 1
        $items = [];
72
73
        // Resave to proper XML to get full namespace support
74 1
        $dom2 = $dom->saveXML();
75 1
        $dom = new \DOMDocument();
76 1
        $dom->loadXML($dom2);
77
78 1
        $xpath = new \DOMXPath($dom);
79 1
        $xpath->registerNamespace('html', self::HTML_PROFILE_URI);
80
81
        // Run through all <link> elements with a `rel` attribute
82
        /** @var \DOMElement $linkRel */
83 1
        foreach ($xpath->query('//html:link[@rel]') as $linkRel) {
84 1
            $item = new \stdClass();
85 1
            $item->type = (object)['name' => $linkRel->getAttribute('rel'), 'profile' => self::HTML_PROFILE_URI];
86
87
            // Get the item ID (if any)
88 1
            if ($linkRel->hasAttribute('id')) {
89 1
                $item->id = $linkRel->getAttribute('id');
90
            }
91
92
            // Run through all item attributes
93 1
            $item->properties = [];
94
            /**
95
             * @var string $attributeName Attribute name
96
             * @var \DOMAttr $attribute Attribute
97
             */
98 1
            foreach ($linkRel->attributes as $attributeName => $attribute) {
99 1
                if (!in_array($attributeName, ['rel', 'id'])) {
100 1
                    $profile = $attribute->lookupNamespaceUri($attribute->prefix ?: null);
101 1
                    $item->properties[] = (object)[
102 1
                        'name' => $attributeName,
103 1
                        'profile' => $profile,
104 1
                        'values' => $this->parseAttributeValue($profile, $attributeName, $attribute->value),
105
                    ];
106
                }
107
            }
108
109 1
            $items[] = $item;
110
        }
111
112 1
        return new ParsingResult(self::FORMAT, $items);
113
    }
114
115
    /**
116
     * Parse an attribute value
117
     *
118
     * @param string $profile Profile
119
     * @param string $attribute Attribute name
120
     * @param string $value Attribute value
121
     * @return array Attribute values
122
     */
123 1
    protected function parseAttributeValue($profile, $attribute, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $profile is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124
    {
125 1
        return [$value];
126
    }
127
}
128