Source of file Form.php
Size: 3,003 Bytes - Last Modified: 2015-12-22T09:12:14-05:00
../src/View/Helper/Form.php
12345678910111213141516171819202122232425262728293031323334
Covered by 4 test(s):
35
Covered by 4 test(s):
3637
Covered by 4 test(s):
38
Covered by 4 test(s):
39
Covered by 3 test(s):
40
Covered by 3 test(s):
414243
Covered by 1 test(s):
44
Covered by 1 test(s):
45
Covered by 1 test(s):
46
Covered by 1 test(s):
47
Covered by 1 test(s):
48
Covered by 4 test(s):
4950
Covered by 4 test(s):
51
Covered by 4 test(s):
52
Covered by 4 test(s):
5354
Covered by 4 test(s):
55
Covered by 4 test(s):
56
Covered by 4 test(s):
57
Covered by 4 test(s):
585960616263646566676869
Covered by 2 test(s):
70717273747576777879
Covered by 4 test(s):
80
Covered by 1 test(s):
818283
Covered by 3 test(s):
84
Covered by 3 test(s):
85
Covered by 3 test(s):
8687
Covered by 3 test(s):
88
Covered by 1 test(s):
89
Covered by 1 test(s):
9091
Covered by 3 test(s):
92
Covered by 3 test(s):
93
Covered by 3 test(s):
949596979899100101102103
Covered by 1 test(s):
104
Covered by 1 test(s):
105
Covered by 1 test(s):
106
Covered by 1 test(s):
107
Covered by 1 test(s):
108
Covered by 1 test(s):
109
Covered by 1 test(s):
110
Covered by 1 test(s):
111112
Covered by 1 test(s):
113114115
| <?php /** * HTML Form View Helper * * @category MvcLite * @package Lib * @subpackage View_Helper * @since File available since release 1.1.x * @author Cory Collier <corycollier@corycollier.com> */ namespace MvcLite\View\Helper; use MvcLite\View\HelperAbstract as HelperAbstract; /** * HTML Form View Helper class * * @category MvcLite * @package Lib * @subpackage View_Helper * @since Class available since release 1.1.x * @author Cory Collier <corycollier@corycollier.com> */ class Form extends HelperAbstract { /** * renders a form from a model * * @param \MvcLite\ModelAbstract $model */ public function render($fields, $attribs = []) { $template = '<form!attribs>!elements</form>'; $elements = ''; foreach ($fields as $column => $field) { if (!is_int($column)) { $elements .= $this->getGroupWrapper($this->elementFactory($column, $field)); continue; } $group = ''; foreach ($field as $name => $attribs) { $group .= $this->elementFactory($name, $attribs); } $elements .= $this->getGroupWrapper($group); } $elements .= $this->getView() ->getHelper('InputSubmit') ->render(); return strtr($template, [ '!attribs' => $this->getHtmlAttribs($attribs), '!elements' => $elements, ]); } /** * Gets a wrapping string around an element(s) string. * * @param string $string The element markup. * * @return string The wrapped markup. */ public function getGroupWrapper($string) { return sprintf('<div class="form-group">%s</div>', $string); } /** * method to return an input element from a given parameter array * * @param array $params */ public function elementFactory($column, $params = []) { if (isset($params['primary'])) { return ''; } $map = $this->getElementTypeMap(); $helper = $map[$params['type']]; $params['placeholder'] = $params['description']; if ($params['type'] == 'enum') { $params['options'] = array_combine($params['options'], $params['options']); } return $this->getView() ->getHelper($helper) ->render($column, $params); } /** * Gets the known element type map * @return array An associative array mapping field types, to form element types. */ protected function getElementTypeMap() { $map = [ 'enum' => 'InputSelect', 'password' => 'InputPassword', 'int' => 'InputText', 'text' => 'InputTextarea', 'varchar' => 'InputText', 'submit' => 'InputSubmit', 'checkbox' => 'InputCheckbox', ]; return $map; } } |