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; |
|
|
|
|
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
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: