<commit_before><?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

namespace Larium\FormBuilder\Elements;

use Larium\FormBuilder\Element;

class Select extends Element
{
    protected $file = 'select';

    protected $options = array();

    public function __construct(
        $name,
        $options = array(),
        $selected = null,
        $attrs = array(),
        $add_empty = true,
        $empty_label = null
    ) {
        $this->name = $name;

        $this->options = $options;

        $this->attrs = $attrs;

        $html = array();

        if (true == $add_empty) {
            $html[] = $this->option($empty_label, null, $selected);
        }

        foreach ($options as $value => $label) {

            if (is_array($label)) {
                $html[] = $this->optgroup($value, $label, $selected);
            } else {
                $html[] = $this->option($label, $value, $selected);
            }
        }

        $this->value = implode("    ", $html);
    }

    public function getOptions()
    {
        return $this->options;
    }

    protected function option($label, $value, $selected)
    {
        $value = is_bool($value) ? (int) $value : $value;

        if (is_array($selected)) {
            foreach($selected as $select) {
                if (((string) $value == (string) $select)) {
                    $selected = 'selected';
                    break;
                } else {
                    $selected = null;
                }
            }
        } else {
            $selected = ((string) $value == (string) $selected)
                ? 'selected'
                : null;
        }


        $attrs = array('value' => $this->encode($value), 'selected'=>$selected);

        return '<option'.$this->attributes($attrs).'>'
            .$this->encode($label).'</option>'.PHP_EOL;
    }

    private function resolve_selected($value, $selected)
    {

    }

    protected function optgroup($label, $options, $selected)
    {
        $html = array();

        foreach ($options as $value=>$tag){
            $html[] = $this->option($tag, $value, $selected);
        }

        return '<optgroup label="'.$this->encode($label).'">'.implode("    ",$html).'</optgroup>'.PHP_EOL;<commit_msg>rename to selection<commit_after><?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

namespace Larium\FormBuilder\Elements;

use Larium\FormBuilder\Element;

class Select extends Element
{
    protected $file = 'select';

    protected $options = array();

    public function __construct(
        $name,
        $options = array(),
        $selected = null,
        $attrs = array(),
        $add_empty = true,
        $empty_label = null
    ) {
        $this->name = $name;

        $this->options = $options;

        $this->attrs = $attrs;

        $html = array();

        if (true == $add_empty) {
            $html[] = $this->option($empty_label, null, $selected);
        }

        foreach ($options as $value => $label) {

            if (is_array($label)) {
                $html[] = $this->optgroup($value, $label, $selected);
            } else {
                $html[] = $this->option($label, $value, $selected);
            }
        }

        $this->value = implode("    ", $html);
    }

    public function getOptions()
    {
        return $this->options;
    }

    protected function option($label, $value, $selected)
    {
        $value = is_bool($value) ? (int) $value : $value;

        if (is_array($selected)) {
            $selection = null;
            foreach($selected as $select) {
                if (((string) $value == (string) $select)) {
                    $selection = 'selected';
                    break;
                } else {
                    $selection = null;
                }
            }
        } else {
            $selection = ((string) $value == (string) $selected)
                ? 'selected'
                : null;
        }

        $attrs = array('value' => $this->encode($value), 'selected'=>$selection);

        return '<option'.$this->attributes($attrs).'>'
            .$this->encode($label).'</option>'.PHP_EOL;
    }

    private function resolve_selected($value, $selected)
    {

    }

    protected function optgroup($label, $options, $selected)
    {
        $html = array();

        foreach ($options as $value=>$tag){
            $html[] = $this->option($tag, $value, $selected);
        }

        return '<optgroup label="'.$this->encode($label).'">'.implode("    ",$html).'</optgroup>'.PHP_EOL;
{'fraction_non_alphanumeric': 0.16293859649122808, 'fraction_numerical': 0.0013157894736842105, 'mean_word_length': 1.772644376899696, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5617713', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1281, 'n_words': 279}