public function transform($value)
 {
     if (null !== $value && !is_scalar($value)) {
         throw new UnexpectedTypeException($value, 'scalar');
     }
     return FormUtil::toArrayKey($value);
 }
 /**
  * Transforms a single choice to a format appropriate for the nested
  * checkboxes/radio buttons.
  *
  * The result is an array with the options as keys and true/false as values,
  * depending on whether a given option is selected. If this field is rendered
  * as select tag, the value is not modified.
  *
  * @param  mixed $value  An array if "multiple" is set to true, a scalar
  *                       value otherwise.
  *
  * @return mixed         An array
  *
  * @throws UnexpectedTypeException if the given value is not scalar
  * @throws TransformationFailedException if the choices can not be retrieved
  */
 public function transform($value)
 {
     if (!is_scalar($value) && null !== $value) {
         throw new UnexpectedTypeException($value, 'scalar');
     }
     try {
         $choices = $this->choiceList->getChoices();
     } catch (\Exception $e) {
         throw new TransformationFailedException('Can not get the choice list', $e->getCode(), $e);
     }
     $value = FormUtil::toArrayKey($value);
     foreach (array_keys($choices) as $key) {
         $choices[$key] = $key === $value;
     }
     return $choices;
 }
Exemple #3
0
    public function isChoiceSelected($choice)
    {
        $choice = FormUtil::toArrayKey($choice);

        // The value should already have been converted by value transformers,
        // otherwise we had to do the conversion on every call of this method
        if (is_array($this->vars['value'])) {
            return false !== array_search($choice, $this->vars['value'], true);
        }

        return $choice === $this->vars['value'];
    }
Exemple #4
0
 /**
  * @dataProvider toArrayKeyProvider
  */
 public function testToArrayKey($in, $out)
 {
     $this->assertSame($out, FormUtil::toArrayKey($in));
 }
 public function transform($value)
 {
     return FormUtil::toArrayKey($value);
 }