Completed
Push — dev ( a486ee...7113e4 )
by James Ekow Abaka
03:55
created

Container::render()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
/**
4
 * Containers for containing form elements
5
 * 
6
 * Ntentan Framework
7
 * Copyright (c) 2008-2012 James Ekow Abaka Ainooson
8
 * 
9
 * Permission is hereby granted, free of charge, to any person obtaining
10
 * a copy of this software and associated documentation files (the
11
 * "Software"), to deal in the Software without restriction, including
12
 * without limitation the rights to use, copy, modify, merge, publish,
13
 * distribute, sublicense, and/or sell copies of the Software, and to
14
 * permit persons to whom the Software is furnished to do so, subject to
15
 * the following conditions:
16
 * 
17
 * The above copyright notice and this permission notice shall be
18
 * included in all copies or substantial portions of the Software.
19
 * 
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
27
 * 
28
 * @author James Ainooson <[email protected]>
29
 * @copyright Copyright 2010 James Ekow Abaka Ainooson
30
 * @license MIT
31
 */
32
33
namespace ntentan\honam\engines\php\helpers\form;
34
35
36
/**
37
 * The container class. This abstract class provides the necessary
38
 * basis for implementing form element containers. The container
39
 * is a special element which contains other form elements.
40
 */
41
abstract class Container extends Element
42
{
43
44
    const RENDER_MODE_ALL = 'all';
45
    const RENDER_MODE_HEAD = 'head';
46
    const RENDER_MODE_FOOT = 'foot';
47
48
    protected $renderMode = self::RENDER_MODE_ALL;
49
    protected $elements = array();
50
51
    /**
52
     * @return string
53
     */
54
    abstract protected function renderHead();
55
56
    /**
57
     * @return string
58
     */
59
    abstract protected function renderFoot();
60
    
61 7
    public function setRenderMode($renderMode)
62
    {
63 7
        $this->renderMode = $renderMode;
64 7
    }
65
66 3
    private function addElement($element)
67
    {
68 3
        $this->elements[] = $element;
69 3
        $element->parent = $this;
70 3
    }
71
72
    /**
73
     * Method for adding an element to the form container.
74
     * @return Container
75
     */
76 3
    public function add()
77
    {
78 3
        $arguments = func_get_args();
79 3
        foreach ($arguments as $element) {
80 3
            $this->addElement($element);
81
        }
82 3
        return $this;
83
    }
84
85
    /**
86
     * This method sets the data for the fields in this container. The parameter
87
     * passed to this method is a structured array which has field names as keys
88
     * and the values as value.
89
     */
90 3
    public function setData($data)
91
    {
92 3
        if (is_array($data)) {
93 3
            foreach ($this->elements as $element) {
94 3
                $element->setData($data);
95
            }
96
        }
97 3
        return $this;
98
    }
99
100 10
    public function render()
101
    {
102 10
        switch ($this->renderMode) {
103 10
            case self::RENDER_MODE_HEAD;
104 7
                return $this->renderHead();
105 10
            case self::RENDER_MODE_FOOT:
106 7
                return $this->renderFoot();
107
            default:
108 3
                return $this->renderHead() 
109 3
                    . $this->templateRenderer->render('elements.tpl.php', array('elements' => $this->getElements()))
110 3
                    . $this->renderFoot();
111
        }
112
    }
113
114
    //! Returns an array of all the Elements found in this container.
115 3
    public function getElements()
116
    {
117 3
        return $this->elements;
118
    }
119
120 10
    public function __toString()
121
    {
122 10
        return $this->render();
123
    }
124
125
    public function isContainer()
126
    {
127
        return true;
128
    }
129
130
}
131