Classes

File builder/form/field/enum/AbstractFormFieldEnumOption.class.php

File builder/form/field/enum/AbstractFormFieldEnumOption.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 
<?php
/**
 * @package     Builder
 * @subpackage  Form\field\enum
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Benoit SAUTEL <ben.popeye@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 10 28
 * @since       PHPBoost 3.0 - 2010 01 10
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

abstract class AbstractFormFieldEnumOption implements FormFieldEnumOption
{
    private $label = '';

    private $raw_value = '';
    private $active;
    private $disable = false;

    /**
     * @var FormField
     */
    private $field;

    public function __construct($label, $raw_value, $field_choice_options = array())
    {
        $this->set_label($label);
        $this->set_raw_value($raw_value);
        $this->compute_options($field_choice_options);
    }

    /**
     * {@inheritdoc}
     */
    public function get_label()
    {
        return $this->label;
    }

    /**
     * {@inheritdoc}
     */
    public function set_label($label)
    {
        $this->label = $label;
    }

    /**
     * {@inheritdoc}
     */
    public function get_raw_value()
    {
        return $this->raw_value;
    }

    /**
     * {@inheritdoc}
     */
    public function set_raw_value($value)
    {
        $this->raw_value = $value;
    }

    /**
     * @return FormField
     */
    public function get_field()
    {
        return $this->field;
    }

    public function set_field(FormField $field)
    {
        $this->field = $field;
    }

    public function set_active($value = true)
    {
        $this->active = $value;
    }

    public function is_active()
    {
        if (isset($this->active))
        {
            return $this->active;
        }
        else
        {
            return $this->get_field()->get_value() === $this;
        }
    }

    public function set_disable($value = false)
    {
        $this->disable = $value;
    }

    protected function is_disable()
    {
        return $this->disable;
    }

    protected function get_field_id()
    {
        return $this->get_field()->get_html_id();
    }

    protected function get_option_id()
    {
        return $this->get_field()->get_html_id() . $this->get_field()->get_option_id($this->raw_value);
    }

    /**
     * {@inheritdoc}
     */
    public function get_option($raw_value)
    {
        if ($this->get_raw_value() == $raw_value)
        {
            return $this;
        }
        else
        {
            return null;
        }
    }

    protected function compute_options(array &$field_choice_options)
    {
        foreach($field_choice_options as $attribute => $value)
        {
            $attribute = TextHelper::strtolower($attribute);
            switch ($attribute)
            {
                case 'disable':
                    $this->set_disable($value);
                    unset($field_choice_options['disable']);
                    break;
                default :
                    throw new FormBuilderException('The class ' . get_class($this) . ' hasn\'t the ' . $attribute . ' attribute');
            }
        }
    }
}

?>