Completed
Branch master (95b3ba)
by Ryan
28:02 queued 13:03
created

AbstractXmlMiddleware::generateQuery()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 3
1
<?php
2
/**
3
 * Copyright (c) 2017 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Middleware\Xml;
12
13
use SimplePie\Middleware\AbstractMiddleware;
14
use SimplePie\Type\Node;
15
16
abstract class AbstractXmlMiddleware extends AbstractMiddleware
17
{
18
    /**
19
     * Replace all instances of `%s` with the `$namespaceAlias` parameter.
20
     *
21
     * This is similar to `sprintf()`, but the `$namespaceAlias` is applied to _all_ instances of `%s`.
22
     *
23
     * @param string $query          An XPath query where `%s` is used in-place of the XML namespace alias.
24
     * @param string $namespaceAlias The XML namespace alias to apply.
25
     *
26
     * @return string
27
     */
28
    public function applyNsToQuery(string $query, string $namespaceAlias): string
29
    {
30
        return \str_replace('%s', $namespaceAlias, $query);
31
    }
32
33
    /**
34
     * Produce an XPath 1.0 expression which is used to query XML document nodes.
35
     *
36
     * @param string $namespaceAlias   The XML namespace alias to apply.
37
     * @param bool   $supportMixedCase Whether or not to generate an XPath query which supports
38
     *                                 mixed-case/case-insensitive XML element names.
39
     * @param string ...$path          A variadic parameter which accepts the names of the XML
40
     *                                 tree nodes in sequence.
41
     *
42
     * @return string An XPath 1.0 expression.
43
     *
44
     * @see https://wiki.php.net/rfc/variadics
45
     * @see http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list
46
     */
0 ignored issues
show
Documentation introduced by
Should the type for parameter $path not be string[]?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
47
    public function generateQuery(string $namespaceAlias, bool $supportMixedCase = false, string ...$path): string
48
    {
49
        $query = '';
50
51
        foreach ($path as $p) {
52
            if ($supportMixedCase) {
53
                $query .= \sprintf(
54
                    '/%s:*[translate(name(), \'%s\', \'%s\') = \'%s\']',
55
                    $namespaceAlias,
56
                    'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
57
                    'abcdefghijklmnopqrstuvwxyz',
58
                    $p
59
                );
60
            } else {
61
                $query .= \sprintf(
62
                    '/%s:%s',
63
                    $namespaceAlias,
64
                    $p
65
                );
66
            }
67
        }
68
69
        return $query;
70
    }
71
72
    /**
73
     * Some elements in the feed should only have one result. This handles those cases.
74
     *
75
     * @param callable $fn A callable which returns a `DOMElementList`.
76
     *
77
     * @return array Returns an array with keys of `text` and `html`.
0 ignored issues
show
Documentation introduced by
Should the return type not be Node?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
78
     */
79
    public function handleSingleNode(callable $fn): Node
80
    {
81
        $nodes = $fn();
82
83
        if ($nodes->length > 0) {
84
            return new Node($nodes[0]);
85
        }
86
87
        return new Node();
88
    }
89
90
    public function handleMultipleNodes(callable $fn): Node
91
    {
92
        $nodes = $fn();
93
94
        \print_r($nodes);
95
    }
96
}
97