DocumentContainerTrait::documentElementTreeList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of gpupo/pipe2
5
 *
6
 * (c) Gilmar Pupo <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * For more information, see
12
 * <https://opensource.gpupo.com/pipe2/>.
13
 */
14
15
namespace Gpupo\Pipe2\Traits;
16
17
use DOMElement;
18
use Gpupo\Pipe2\DocumentInterface;
19
20
trait DocumentContainerTrait
21
{
22
    protected $document;
23
24
    public function getDocument()
25
    {
26
        return $this->document;
27
    }
28
29
    protected function setDocument(DocumentInterface $document, $formatOutput = false)
30
    {
31
        $this->document = $document;
32
        $this->document->formatOutput = $formatOutput;
0 ignored issues
show
Bug introduced by
Accessing formatOutput on the interface Gpupo\Pipe2\DocumentInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
33
34
        return $this;
35
    }
36
37
    private $_documentElementTree = [];
38
39
    protected function documentElementTreeAppend(DOMElement $tag, $parent)
40
    {
41
        if (!array_key_exists($parent, $this->_documentElementTree)) {
42
            $this->_documentElementTree[$parent] = $this->document->createElement($parent);
43
        }
44
45
        $this->_documentElementTree[$parent]->appendChild($tag);
46
47
        return $this;
48
    }
49
50
    protected function documentElementTreeList()
51
    {
52
        return [
53
            'g:installment' => ['g:months', 'g:amount'],
54
        ];
55
    }
56
57
    protected function documentElementTreeGetParent($key)
58
    {
59
        foreach ($this->documentElementTreeList() as $name => $array) {
60
            if (in_array($key, $array, true)) {
61
                return $name;
62
            }
63
        }
64
65
        return false;
66
    }
67
68
    protected function documentElementTreeFinal(DOMElement $itemElement)
69
    {
70
        foreach ($this->_documentElementTree as $element) {
71
            $itemElement->appendChild($element);
72
        }
73
74
        return $itemElement;
75
    }
76
77
    protected function populateDocument($itemElement, $item)
78
    {
79
        foreach ($item as $key => $value) {
80
            $tag = $this->document->createElement($key);
81
82
            if (is_numeric($value)) {
83
                $tag->appendChild($this->document->createTextNode($value));
84
            } else {
85
                $tag->appendChild($this->document->createCDATASection($value));
86
            }
87
            $parent = $this->documentElementTreeGetParent($key);
88
            if (!empty($parent)) {
89
                $this->documentElementTreeAppend($tag, $parent);
90
            } else {
91
                $itemElement->appendChild($tag);
92
            }
93
        }
94
95
        $this->document->docset->appendChild($this->documentElementTreeFinal($itemElement));
96
97
        return $this;
98
    }
99
}
100