Passed
Push — develop ( 4127cf...94aa98 )
by Mikaël
02:13
created

Wsdl::getElementByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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