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):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
35
Covered by 4 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
3637
Covered by 4 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
38
Covered by 4 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
39
Covered by 3 test(s):
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
40
Covered by 3 test(s):
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
414243
Covered by 1 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
44
Covered by 1 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
45
Covered by 1 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
46
Covered by 1 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
47
Covered by 1 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
48
Covered by 4 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
4950
Covered by 4 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
51
Covered by 4 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
52
Covered by 4 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
5354
Covered by 4 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
55
Covered by 4 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
56
Covered by 4 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
57
Covered by 4 test(s):
  • MvcLite\FormTest::testRender with data set "test with groups"
  • MvcLite\FormTest::testRender with data set "test with 2 elements"
  • MvcLite\FormTest::testRender with data set "test with 3 elements"
  • MvcLite\FormTest::testRender with data set "test with attributes"
585960616263646566676869
Covered by 2 test(s):
  • MvcLite\FormTest::testGetGroupWrapper with data set "empty string"
  • MvcLite\FormTest::testGetGroupWrapper with data set "simple string"
70717273747576777879
Covered by 4 test(s):
  • MvcLite\FormTest::testElementFactory with data set "primary"
  • MvcLite\FormTest::testElementFactory with data set "not primary"
  • MvcLite\FormTest::testElementFactory with data set "text"
  • MvcLite\FormTest::testElementFactory with data set "enum"
80
Covered by 1 test(s):
  • MvcLite\FormTest::testElementFactory with data set "primary"
818283
Covered by 3 test(s):
  • MvcLite\FormTest::testElementFactory with data set "not primary"
  • MvcLite\FormTest::testElementFactory with data set "text"
  • MvcLite\FormTest::testElementFactory with data set "enum"
84
Covered by 3 test(s):
  • MvcLite\FormTest::testElementFactory with data set "not primary"
  • MvcLite\FormTest::testElementFactory with data set "text"
  • MvcLite\FormTest::testElementFactory with data set "enum"
85
Covered by 3 test(s):
  • MvcLite\FormTest::testElementFactory with data set "not primary"
  • MvcLite\FormTest::testElementFactory with data set "text"
  • MvcLite\FormTest::testElementFactory with data set "enum"
8687
Covered by 3 test(s):
  • MvcLite\FormTest::testElementFactory with data set "not primary"
  • MvcLite\FormTest::testElementFactory with data set "text"
  • MvcLite\FormTest::testElementFactory with data set "enum"
88
Covered by 1 test(s):
  • MvcLite\FormTest::testElementFactory with data set "enum"
89
Covered by 1 test(s):
  • MvcLite\FormTest::testElementFactory with data set "enum"
9091
Covered by 3 test(s):
  • MvcLite\FormTest::testElementFactory with data set "not primary"
  • MvcLite\FormTest::testElementFactory with data set "text"
  • MvcLite\FormTest::testElementFactory with data set "enum"
92
Covered by 3 test(s):
  • MvcLite\FormTest::testElementFactory with data set "not primary"
  • MvcLite\FormTest::testElementFactory with data set "text"
  • MvcLite\FormTest::testElementFactory with data set "enum"
93
Covered by 3 test(s):
  • MvcLite\FormTest::testElementFactory with data set "not primary"
  • MvcLite\FormTest::testElementFactory with data set "text"
  • MvcLite\FormTest::testElementFactory with data set "enum"
949596979899100101102103
Covered by 1 test(s):
  • MvcLite\FormTest::testGetElementTypeMap
104
Covered by 1 test(s):
  • MvcLite\FormTest::testGetElementTypeMap
105
Covered by 1 test(s):
  • MvcLite\FormTest::testGetElementTypeMap
106
Covered by 1 test(s):
  • MvcLite\FormTest::testGetElementTypeMap
107
Covered by 1 test(s):
  • MvcLite\FormTest::testGetElementTypeMap
108
Covered by 1 test(s):
  • MvcLite\FormTest::testGetElementTypeMap
109
Covered by 1 test(s):
  • MvcLite\FormTest::testGetElementTypeMap
110
Covered by 1 test(s):
  • MvcLite\FormTest::testGetElementTypeMap
111112
Covered by 1 test(s):
  • MvcLite\FormTest::testGetElementTypeMap
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;
    }
}