Passed
Pull Request — master (#267)
by Felipe
07:20
created

XHtmlElement::add()   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 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-RC9-3-gd93ec300
5
 */
6
7
namespace PHPPgAdmin\XHtml;
8
9
/**
10
 *  XHtmlElement.
11
 *
12
 *  Used to generate Xhtml-Code for xhtml elements
13
 *  that can contain child elements
14
 */
15
class XHtmlElement extends XHtmlSimpleElement
16
{
17
    public $_text;
18
19
    public $_htmlcode = '';
20
21
    public $_siblings = [];
22
23
    public function __construct($text = null)
24
    {
25
        parent::__construct();
26
27
        if ($text) {
28
            $this->set_text($text);
29
        }
30
    }
31
32
    /**
33
     * Adds an xhtml child to element.
34
     *
35
     * @param XHtmlOption $object
36
     */
37
    public function add(XHtmlOption &$object): void
38
    {
39
        \array_push($this->_siblings, $object);
40
    }
41
42
    /**
43
     * The CDATA section of Element.
44
     *
45
     * @param string $text Text content of the element
46
     */
47
    public function set_text($text): void
48
    {
49
        if ($text) {
50
            $this->_text = \htmlspecialchars($text);
51
        }
52
    }
53
54
    public function fetch()
55
    {
56
        return $this->_html();
57
    }
58
59
    public function _html()
60
    {
61
        $this->_htmlcode = "<{$this->_element}";
62
63
        foreach ($this->_attributes as $attribute => $value) {
64
            if (!empty($value)) {
65
                $this->_htmlcode .= \sprintf(' %s="%s" ', $attribute, $value);
66
            }
67
        }
68
        $this->_htmlcode .= '>';
69
70
        if ($this->_text) {
71
            $this->_htmlcode .= $this->_text;
72
        }
73
74
        foreach ($this->_siblings as $obj) {
75
            $this->_htmlcode .= $obj->fetch();
76
        }
77
78
        $this->_htmlcode .= "</{$this->_element}>";
79
80
        return $this->_htmlcode;
81
    }
82
83
    // Returns siblings of Element
84
    public function get_siblings()
85
    {
86
        return $this->_siblings;
87
    }
88
89
    public function has_siblings()
90
    {
91
        return 0 !== \count($this->_siblings);
92
    }
93
}
94