Passed
Push — develop ( 3db47d...4127cf )
by Mikaël
02:22
created

Wsdl::getElementByNameAndAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\WsdlHandler;
6
7
use DOMNode;
8
use WsdlToPhp\WsdlHandler\Tag\AbstractTag;
9
10
class Wsdl extends AbstractDocument
11
{
12
    protected array $externalSchemas = [];
13
14
    public function addExternalSchema(Schema $schema): Wsdl
15
    {
16
        $this->externalSchemas[] = $schema;
17
18
        return $this;
19
    }
20
21
    public function getExternalSchemas(): array
22
    {
23
        return $this->externalSchemas;
24
    }
25
26
    public function getElementByName(string $name, bool $includeExternals = false): ?AbstractTag
27
    {
28
        return $this->useParentMethodAndExternals(__FUNCTION__, [
29
            $name,
30
        ], $includeExternals, true);
31
    }
32
33
    public function getElementByNameAndAttributes(string $name, array $attributes, bool $includeExternals = false): ?AbstractTag
34
    {
35
        return $this->useParentMethodAndExternals(__FUNCTION__, [
36
            $name,
37
            $attributes,
38
        ], $includeExternals, true);
39
    }
40
41
    public function getElementsByName(string $name, bool $includeExternals = false): array
42
    {
43
        return $this->useParentMethodAndExternals(__FUNCTION__, [
44
            $name,
45
        ], $includeExternals);
46
    }
47
48
    public function getElementsByNameAndAttributes(string $name, array $attributes, ?DOMNode $node = null, bool $includeExternals = false): array
49
    {
50
        if ($includeExternals) {
51
            $elements = $this->useParentMethodAndExternals(__FUNCTION__, [
52
                $name,
53
                $attributes,
54
                $node,
55
            ]);
56
            /** @var Schema $externalSchema */
57
            foreach ($this->getExternalSchemas() as $index => $externalSchema) {
58
                if (0 < ($nodes = $externalSchema->searchTagsByXpath($name, $attributes, $node))->length) {
59
                    $elements = array_merge($elements, $this->getElementsHandlers($nodes));
60
                }
61
            }
62
            $elements = array_unique($elements, SORT_REGULAR);
63
        } else {
64
            $elements = $this->useParentMethodAndExternals(__FUNCTION__, [
65
                $name,
66
                $attributes,
67
                $node,
68
            ], $includeExternals);
69
        }
70
71
        return $elements;
72
    }
73
74
    /**
75
     * Handles any method that exist within the parent class,
76
     * in addition it handles the case when we want to use the external schemas to search in.
77
     *
78
     * @return mixed
79
     */
80
    protected function useParentMethodAndExternals(string $method, array $parameters, bool $includeExternals = false, bool $returnOne = false)
81
    {
82
        $result = call_user_func_array([
83
            $this,
84
            sprintf('parent::%s', $method),
85
        ], $parameters);
86
87
        if ($includeExternals && (!$returnOne || empty($result))) {
88
            $result = $this->useExternalSchemas($method, $parameters, $result, $returnOne);
89
        }
90
91
        return $result;
92
    }
93
94
    protected function useExternalSchemas(string $method, array $parameters, $parentResult, bool $returnOne = false)
95
    {
96
        $result = $parentResult;
97
98
        foreach ($this->getExternalSchemas() as $externalSchema) {
99
            $externalResult = call_user_func_array([
100
                $externalSchema,
101
                $method,
102
            ], $parameters);
103
104
            if ($returnOne && !is_null($externalResult)) {
105
                $result = $externalResult;
106
107
                break;
108
            }
109
            if (is_array($externalResult) && !empty($externalResult)) {
110
                $result = array_merge($result, $externalResult);
111
            }
112
        }
113
114
        return $result;
115
    }
116
}
117