Passed
Branch main (e68017)
by James Ekow Abaka
10:11
created

Container::addElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace ntentan\honam\engines\php\helpers\form;
3
4
/**
5
 * The container class. This abstract class provides the necessary
6
 * basis for implementing form element containers. The container
7
 * is a special element which contains other form elements.
8
 */
9
abstract class Container extends Element
10
{
11
    protected $elements = array();
12
13
    /**
14
     * @return string
15
     */
16
    abstract protected function renderHead();
17
18
    /**
19
     * @return string
20
     */
21
    abstract protected function renderFoot();
22
23
    /**
24
     * Method for adding an element to the form container.
25
     * @return Container
26
     */
27
    public function add(Element $element)
28
    {
29
        $this->elements[] = $element;
30
    }
31
32
    /**
33
     * This method sets the data for the fields in this container. The parameter passed to this method is a structured 
34
     * array which has field names as keys and the values as value.
35
     */
36
    public function setData($data)
37
    {
38
        if (is_array($data)) {
39
            foreach ($this->elements as $element) {
40
                $element->setData($data);
41
            }
42
        }
43
    }
44
45
    public function render()
46
    {
47
        return $this->renderHead() 
48
            . $this->templateRenderer->render('elements.tpl.php', array('elements' => $this->getElements()))
49
            . $this->renderFoot();
50
    }
51
52
    public function getElements()
53
    {
54
        return $this->elements;
55
    }
56
57
    public function __toString()
58
    {
59
        return $this->render();
60
    }
61
62
    public function isContainer()
63
    {
64
        return true;
65
    }
66
}
67