Parse the choices and format them.

@param array $choices

@return array
19600
protected function parse(array $choices)
    {
        $items = [];

        foreach ($choices as $key => $value) {
            if (is_array($value)) {
                $items[$key] = $this->parse($value);
            } else {
                if (is_int($key)) {
                    $label = ucfirst(str_replace(['-', '_'], ' ', $value));
                } else {
                    $label = $key;
                }

                $items[$label] = $value;
            }
        }

        return $items;
    }
1027952
static public function getChoices()
    {
        $choices = [];

        foreach (static::getCodes() as $code) {
            $choices[static::getLabel($code)] = $code;
        }

        return $choices;
    }
1278817
public function setChoices(array $choices)
    {
        // Removes old choices
        $oldChoices = array_filter($this->choices->toArray(), function (CellChoice $choice) use ($choices) {
            return !in_array($choice, $choices);
        });
        array_walk($oldChoices, function (CellChoice $choice) {
            $this->removeChoice($choice);
        });

        // Adds new ones
        array_walk($choices, function (CellChoice $choice) {
            $this->addChoice($choice);
        });
    }
800614
protected function parse_array_rules( array $rules ) {
		$parse_rules = [];

		foreach ( $rules as $key => $value ) {
			if ( is_int( $key ) ) {
				$parse_rules[] = [ $this->normalize( $value ), [] ];
				continue;
			}

			$key = $this->normalize( $key );

			if ( ! is_array( $value ) ) {
				$value = [ $value ];
			}

			$parse_rules[] = [ $key, $this->wrap_parameters( $key, $value ) ];
		}

		return $parse_rules;
	}
559326
public function getFormattedChoices(array $choices, $position, $selected)
    {
        $lines = [];

        foreach ($choices as $index => $choice) {
            $state = in_array($choice, $selected)
                ? self::CHOICE_SELECTED
                : self::CHOICE_NORMAL;

            $cursor = $index === $position
                ? self::CHOICE_HOVERED
                : self::CHOICE_NORMAL;

            // select which style to use
            $line = $this->style[$state][$cursor];

            // add the fancy fish eye symbol to make things prettier
            $choice = '  ' . unicode_to_string('\u25C9') . '  ' . $choice;

            $lines[] = sprintf($line, $this->fitText($this->window->getWidth(), $choice));
        }

        return $lines;
    }
515175
protected function parseOptions(array $options): array
    {
        $this->setTransformer(new NumberToLocalizedStringTransformer($this->getLocale(), $this));

        return parent::parseOptions($options);
    }
713966
public function choices()
    {
        $choices = [];

        $proto = $this->proto();
        if (!$proto->source()->tableExists()) {
            return $choices;
        }

        $loader = $this->collectionModelLoader();

        $collection = new HierarchicalCollection($loader->load(), false);
        $collection->setPage($loader->page())
                   ->setNumPerPage($loader->numPerPage())
                   ->sortTree();

        return $this->parseChoices($collection);
    }
1065921
public function choose($string, array $choices, $useChoiceKeysAsSelector = true) {
        $this->output->writeLine("<question>$string</question>");

        $choicesMap = [];

        foreach ($choices as $subject => $message) {
            if ($useChoiceKeysAsSelector) {
                $index = $subject;
            } else {
                $index = count($choicesMap) + 1;
            }

            $choicesMap[$index] = [
                'subject' => $subject,
                'message' => $message,
            ];
        }

        foreach ($choicesMap as $index => $choice) {
            $message = array_get($choice, 'message');

            $this->output->writeLineIndented("[<yellow>$index</yellow>] $message");
        }

        $choice = null;

        while (true) {
            $this->output->writeIndented('Choice: ');
            $input = $this->input->readLine();

            if (array_has($choicesMap, $input)) {
                $choice = array_get(array_get($choicesMap, $input), 'subject');
                break;
            }

            if ( ! empty($input)) {
                $this->output->writeLineIndented('<yellow>Invalid choice</yellow>');
            }
        }

        return $choice;
    }
516126
private function validate_slots($questiontext, $choices) {
        $error = 'Please check the Question text: ';
        if (!$questiontext) {
            return get_string('errorquestiontextblank', 'qtype_gapselect');
        }

        $matches = array();
        preg_match_all($this->squarebracketsregex, $questiontext, $matches);
        $slots = $matches[0];

        if (!$slots) {
            return get_string('errornoslots', 'qtype_gapselect');
        }

        $cleanedslots = array();
        foreach ($slots as $slot) {
            // The 2 is for'[[' and 4 is for '[[]]'.
            $cleanedslots[] = substr($slot, 2, (strlen($slot) - 4));
        }
        $slots = $cleanedslots;

        $found = false;
        foreach ($slots as $slot) {
            $found = false;
            foreach ($choices as $key => $choice) {
                if ($slot == $key + 1) {
                    if ($choice['answer'] === '') {
                        return get_string('errorblankchoice', 'qtype_gapselect',
                                html_writer::tag('b', $slot));
                    }
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                return get_string('errormissingchoice', 'qtype_gapselect',
                        html_writer::tag('b', $slot));
            }
        }
        return false;
    }
825489
protected function formatValues(array $labels)
    {
        $values = Collection::make($this->values);

        if (in_array(strtolower($this->type), $this->specialDatasets)) {
            $colors = $this->getColors($labels);

            return $values->map(function ($value, $key) use ($colors, $labels) {
                $val = [
                    'label' => $labels[$key],
                    'value' => $value,
                ];

                if ($colors->count() > 0) {
                    $val['color'] = $colors->get($key);
                }

                return $val;
            })->toArray();
        }

        return $values->map(function ($value) {
            return [
                'value' => $value,
            ];
        })->toArray();
    }
713183
public function allowedValues(array $choices): Validator
    {
        return $this->assertCallback(
            function ($value) use ($choices) {
                return in_array($value, $choices);
            },
            'is not an allowed value'
        );
    }
16959
public function parse($rules)
    {
        $this->rules = $rules;
        $parsed = [];
        foreach ($this->getRules() as $rulesArray) {
            $column = $this->getColumn($rulesArray);
            $attributes = $this->getAttributes($column, $rulesArray);
            $parsed[$column] = $attributes;
        }

        return $parsed;
    }
194286
protected function selector(&$choices = null)
    {
        $choices = $choices ? $choices : $this->choices;
        $choices = $this->userEditor->getChoicesAsEmailUsername($choices);
        $question = new ChoiceQuestion(static::PLEASE_SELECT_A_USER, $choices);
        $selectedUser = $this->ask($question);
        $user = $this->getChoiceBySelection($selectedUser);
        if ($user) {
            $this->editor($user);
        }
    }
326188
protected static function paginateGroupChoices(array $choices, $startTo, $endTo)
    {
        $paginatedChoices = [];
        $index = 0;

        foreach ($choices as $groupName => $groupChoices) {
            foreach ($groupChoices as $key => $choice) {
                if ($index >= $startTo && $index < $endTo) {
                    $paginatedChoices[$groupName][$key] = $choice;
                }

                ++$index;
            }
        }

        return $paginatedChoices;
    }
1031749
public function addChoices(array $choices)
    {
        foreach ($choices as $choiceIdent => $choice) {
            $this->addChoice($choiceIdent, $choice);
        }

        return $this;
    }
262208
protected function parseOptions(array $options): array
    {
        $this->setTransformer(new ChoiceToValueTransformer());

        $options = parent::parseOptions($options);

        if (is_null($options['choices'])) {
            $options['choices'] = [];
        }

        if (is_array($options['choices'])) {
            $options['choices'] = new ChoiceList($options['choices']);
        }

        // Set field layout based on field options.
        $this->setLayout($options['expanded'], $options['multiple']);

        // Set the "multiple" attribute for <select> tag.
        if ('select' === $this->getLayout() && $options['multiple']) {
            // We're using a <select> tag with the multiple option set to true.
            // So we're going to directly inject the multiple attribute.
            $options['attributes'][] = 'multiple';
        }

        return $options;
    }
468077
public function getCheckboxes(array $options=[]) {
		if(isset($options['choices']))
			$choices = $options['choices'];
		else
			$choices = $this->getChoices();

		$checkboxes = [];
		foreach($choices as $k=>$v) {
			$checkbox_options = $options;
			$checkbox_options['value'] = $k;
			$checkbox_options['widget_name'] = $v;
			$checkboxes[$k] = $this->getCheckbox($v, $checkbox_options);
		}
		return $checkboxes;
	}