Passed
Pull Request — develop (#262)
by Felipe
04:12
created

XHtmlElement::fetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// declare(strict_types=1);
4
5
/**
6
 * PHPPgAdmin vv6.0.0-RC8-16-g13de173f
7
 *
8
 */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
9
10
namespace PHPPgAdmin\XHtml;
11
12
/**
13
 *  XHtmlElement.
14
 *
15
 *  Used to generate Xhtml-Code for xhtml elements
16
 *  that can contain child elements
17
 */
18
class XHtmlElement extends XHtmlSimpleElement
19
{
20
    public $_text;
21
22
    public $_htmlcode = '';
23
24
    public $_siblings = [];
25
26
    public function __construct($text = null)
27
    {
28
        parent::__construct();
29
30
        if ($text) {
31
            $this->set_text($text);
32
        }
33
    }
34
35
    /**
36
     * Adds an xhtml child to element.
37
     *
38
     * @param XHtmlElement $object The element to become a child of element
39
     */
40
    public function add(XHtmlOption &$object): void
41
    {
42
        \array_push($this->_siblings, $object);
43
    }
44
45
    /**
46
     * The CDATA section of Element.
47
     *
48
     * @param string $text Text content of the element
49
     */
50
    public function set_text($text): void
51
    {
52
        if ($text) {
53
            $this->_text = \htmlspecialchars($text);
54
        }
55
    }
56
57
    public function fetch()
58
    {
59
        return $this->_html();
60
    }
61
62
    public function _html()
63
    {
64
        $this->_htmlcode = "<{$this->_element}";
65
66
        foreach ($this->_attributes as $attribute => $value) {
67
            if (!empty($value)) {
68
                $this->_htmlcode .= \sprintf(' %s="%s" ', $attribute, $value);
69
            }
70
        }
71
        $this->_htmlcode .= '>';
72
73
        if ($this->_text) {
74
            $this->_htmlcode .= $this->_text;
75
        }
76
77
        foreach ($this->_siblings as $obj) {
78
            $this->_htmlcode .= $obj->fetch();
79
        }
80
81
        $this->_htmlcode .= "</{$this->_element}>";
82
83
        return $this->_htmlcode;
84
    }
85
86
    // Returns siblings of Element
87
    public function get_siblings()
88
    {
89
        return $this->_siblings;
90
    }
91
92
    public function has_siblings()
93
    {
94
        return 0 !== \count($this->_siblings);
95
    }
96
}
97