LinkType::parseAttributeValue()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.7333
cc 4
nc 4
nop 3
crap 4
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 © 2018 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 © 2018 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 LinkType extends AbstractParser
49
{
50
    /**
51
     * Format
52
     *
53
     * @var int
54
     */
55
    const FORMAT = Format::LINK_TYPE;
56
57
    /**
58
     * Parse a DOM document
59
     *
60
     * @param \DOMDocument $dom DOM Document
61
     *
62
     * @return ParsingResultInterface Micro information items
63
     */
64 2
    public function parseDom(\DOMDocument $dom)
65
    {
66 2
        $this->logger->info('Running parser: '.(new \ReflectionClass(__CLASS__))->getShortName());
67 2
        $items = [];
68
69 2
        $xpath = new \DOMXPath($dom);
70 2
        $xpath->registerNamespace('html', self::HTML_PROFILE_URI);
71
72
        // Run through all <link> elements with a `rel` attribute
73
        /** @var \DOMElement $linkType */
74 2
        foreach ($xpath->query('//*[local-name(.) = "link" or local-name(.) = "a"][@rel]') as $linkType) {
75
            $item    = [
76 2
                'type'       => $this->parseLinkType($linkType->getAttribute('rel')),
77 2
                'id'         => $linkType->getAttribute('id') ?: null,
78 2
                'properties' => $this->parseProperties($linkType),
79
            ];
80 2
            $items[] = (object)$item;
81
        }
82
83 2
        return new ParsingResult(self::FORMAT, $items);
84
    }
85
86
    /**
87
     * Process the item types
88
     *
89
     * @param string $relAttr rel attribute value
90
     *
91
     * @return array Item types
92
     */
93 2
    protected function parseLinkType($relAttr)
94
    {
95 2
        $type = [];
96 2
        foreach (preg_split('/\040+/', $relAttr) as $rel) {
97 2
            $type[] = (object)['name' => $rel, 'profile' => self::HTML_PROFILE_URI];
98
        }
99
100 2
        return $type;
101
    }
102
103
    /**
104
     * Parse the LinkType attributes
105
     *
106
     * @param \DOMElement $linkType LinkType element
107
     *
108
     * @return array Properties
109
     */
110 2
    protected function parseProperties(\DOMElement $linkType)
111
    {
112 2
        $properties = [];
113
        /**
114
         * @var string $attributeName Attribute name
115
         * @var \DOMAttr $attribute   Attribute
116
         */
117 2
        foreach ($linkType->attributes as $attributeName => $attribute) {
118 2
            if (!in_array($attributeName, ['rel', 'id'])) {
119 2
                $profile      = $attribute->lookupNamespaceUri($attribute->prefix ?: null);
120
                $property     = (object)[
121 2
                    'name'    => $attributeName,
122 2
                    'profile' => $profile,
123 2
                    'values'  => $this->parseAttributeValue($profile, $attributeName, $attribute->value),
124
                ];
125 2
                $properties[] = $property;
126
            }
127
        }
128
129 2
        return $properties;
130
    }
131
132
    /**
133
     * Parse an attribute value
134
     *
135
     * @param string $profile   Profile
136
     * @param string $attribute Attribute name
137
     * @param string $value     Attribute value
138
     *
139
     * @return array Attribute values
140
     */
141 2
    protected function parseAttributeValue($profile, $attribute, $value)
142
    {
143
        // If it's a HTML attribute
144 2
        if ($profile == LinkType::HTML_PROFILE_URI) {
145 2
            switch ($attribute) {
146
                // Space delimited lists
147 2
                case 'sizes':
148 2
                    return array_filter(preg_split('/\040+/', $value));
149
                // Space or comma delimited lists
150 2
                case 'charset':
151 2
                    return array_filter(preg_split('/[,\040]+/', $value));
152
            }
153
        }
154
155 2
        return [$value];
156
    }
157
}
158