<?php

/**
 *
 * Cube Framework $Id$ CukK5Jfbrfv+Ux6Er/1HkfFj6DvYlJ23gmD8eg9Dtzw=
 *
 * @link        http://codecu.be/framework
 * @copyright   Copyright (c) 2017 CodeCube SRL
 * @license     http://codecu.be/framework/license Commercial License
 *
 * @version     1.10 [rev.1.10.01]
 */
/**
 * radio buttons form element generator class
 */

namespace Cube\Form\Element;

use Cube\Form\Element;

class Radio extends Element
{

    /**
     *
     * type of element - override the variable from the parent class
     *
     * @var string
     */
    protected $_element = 'radio';

    /**
     *
     * class constructor
     *
     * @param string $name
     */
    public function __construct($name)
    {
        parent::__construct($this->_element, $name);
    }

    /**
     *
     * render the form element
     *
     * @return string
     */
    public function render()
    {
        $output = null;
        $value = $this->getValue();

        $translate = $this->getTranslate();

        foreach ((array)$this->_multiOptions as $key => $option) {
            $checked = ($value == $key) ? 'checked="checked" ' : '';

            if (is_array($option)) {
                $title = isset($option[0]) ? $option[0] : null;
                $description = isset($option[1]) ? $option[1] : null;

                if (isset($option[2])) {
                    $this->addMultiOptionAttributes($key, $option[2]);
                }
            }
            else {
                $title = $option;
                $description = null;
            }

            $attributes = array(
                'type="' . $this->_element . '"',
                'name="' . $this->_name . '"',
                'value="' . $key . '"',
                $this->renderAttributes(),
                $this->renderOptionAttributes($key),
                $checked
            );

            $output .= '<label class="radio">'
                . '<input ' . implode(' ', array_filter($attributes)) . '>'
                . ' ' . $translate->_($title)
                . (($description !== null) ? '<span class="help-block">' . $translate->_($description) . '</span>' : '')
                . '</label>'
                . "\n";
        }

        return $output;
    }

}

