Completed
Push — dev ( c5a23f...f84598 )
by James Ekow Abaka
09:41
created

Container   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 84
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
renderHead() 0 1 ?
renderFoot() 0 1 ?
A setRenderMode() 0 4 1
A addElement() 0 5 1
A add() 0 8 2
A setData() 0 9 3
A render() 0 13 3
A getElements() 0 4 1
A __toString() 0 4 1
A isContainer() 0 4 1
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
    abstract protected function renderHead();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
52
53
    abstract protected function renderFoot();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
54
    
55
    public function setRenderMode($renderMode)
56
    {
57
        $this->renderMode = $renderMode;
58
    }
59
60
    private function addElement($element)
61
    {
62
        $this->elements[] = $element;
63
        $element->parent = $this;
64
    }
65
66
    /**
67
     * Method for adding an element to the form container.
68
     * @return Container
69
     */
70
    public function add()
71
    {
72
        $arguments = func_get_args();
73
        foreach ($arguments as $element) {
74
            $this->addElement($element);
75
        }
76
        return $this;
77
    }
78
79
    /**
80
     * This method sets the data for the fields in this container. The parameter
81
     * passed to this method is a structured array which has field names as keys
82
     * and the values as value.
83
     */
84
    public function setData($data)
85
    {
86
        if (is_array($data)) {
87
            foreach ($this->elements as $element) {
88
                $element->setData($data);
89
            }
90
        }
91
        return $this;
92
    }
93
94
    public function render()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
95
    {
96
        switch ($this->renderMode) {
97
            case self::RENDER_MODE_HEAD;
98
                return $this->renderHead();
99
            case self::RENDER_MODE_FOOT:
100
                return $this->renderFoot();
101
            default:
102
                return $this->renderHead() 
103
                    . TemplateEngine::render('elements.tpl.php', array('elements' => $this->getElements()))
104
                    . $this->renderFoot();
105
        }
106
    }
107
108
    //! Returns an array of all the Elements found in this container.
109
    public function getElements()
110
    {
111
        return $this->elements;
112
    }
113
114
    public function __toString()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
115
    {
116
        return $this->render();
117
    }
118
119
    public function isContainer()
120
    {
121
        return true;
122
    }
123
124
}
125