Source of file InputSelect.php

Size: 2,451 Bytes - Last Modified: 2015-12-22T09:12:14-05:00

../src/View/Helper/InputSelect.php

123456789101112131415161718192021222324252627282930313233343536
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
37
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
3839
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
40
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
4142
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
4344
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
45
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
46
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
47
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
48
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
49
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
5051525354555657585960
Covered by 3 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "empty options"
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "single option"
616263
Covered by 3 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "empty options"
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "single option"
64
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "single option"
65
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "single option"
66
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "single option"
67
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "single option"
68
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "single option"
69
Covered by 3 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "empty options"
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "single option"
707172
Covered by 3 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "empty options"
  • MvcLite\ViewHelperInputSelectTest::testBuildOptions with data set "single option"
7374757677787980818283848586
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
8788
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
89
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
9091
Covered by 1 test(s):
  • MvcLite\ViewHelperInputSelectTest::testRender with data set #0
929394
<?php
/**
 * Select Input View Helper
 *
 * @category    PHP
 * @package     MvcLite
 * @subpackage  View\Helper
 * @since       File available since release 1.1.x
 * @author      Cory Collier <corycollier@corycollier.com>
 */

namespace MvcLite\View\Helper;

use MvcLite\View\Helper\InputElementAbstract as InputElementAbstract;

/**
 * Select Input View Helper class
 *
 * @category    PHP
 * @package     MvcLite
 * @subpackage  View\Helper
 * @since       Class available since release 1.1.x
 * @author      Cory Collier <corycollier@corycollier.com>
 */
class InputSelect extends InputElementAbstract
{
    /**
     * Method to render a select element
     *
     * @param array $options
     * @param array $attribs
     * @return string
     */
    public function render($name, $attribs = [])
    {
        $options = $attribs['options'];
        unset($attribs['options']);

        $defaults = $this->getDefaultAttribs($name, 'select');
        $attribs  = array_merge($defaults, $attribs);
        $template = '<label for="!id">!label</label>'
            . '<select!attribs>!options</select>';

        return strtr($template, [
            '!id'      => $attribs['id'],
            '!label'   => $attribs['label'],
            '!attribs' => $this->getHtmlAttribs($attribs),
            '!options' => $this->buildOptions($options),
        ]);
    }

    /**
     * method to take an array and turn it into a string of li elements
     *
     * @param array $options
     * @return string
     */
    protected function buildOptions($options = [])
    {
        $template = '<option value="!value">!label</option>';

        // iterate through the options, turning them into strings
        foreach ($options as $value => $label) {
            unset($options[$value]);
            $options[$label] = strtr($template, [
                '!value'    => $value,
                '!label'    => $label,
            ]);
        }

        // return the array imploded into a string by newline characters
        return implode(PHP_EOL, $options);
    }

    /**
     * Local override of the isValidAttribute method.
     *
     * @param string $name The name of the attribute.
     *
     * @return boolean True if valid, false if not.
     */
    protected function isValidAttribute($name)
    {
        $no = [
            'label'
        ];

        if (in_array($name, $no)) {
            return false;
        }
        return true;
    }
}