|
@@ -19,14 +19,19 @@ class ScalarToBooleanChoicesTransformer implements DataTransformerInterface
|
|
{
|
|
{
|
|
private $choiceList;
|
|
private $choiceList;
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Constructor.
|
|
|
|
+ *
|
|
|
|
+ * @param ChoiceListInterface $choiceList
|
|
|
|
+ */
|
|
public function __construct(ChoiceListInterface $choiceList)
|
|
public function __construct(ChoiceListInterface $choiceList)
|
|
{
|
|
{
|
|
$this->choiceList = $choiceList;
|
|
$this->choiceList = $choiceList;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Transforms a single choice or an array of choices to a format appropriate
|
|
|
|
- * for the nested checkboxes/radio buttons.
|
|
|
|
|
|
+ * 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,
|
|
* 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
|
|
* depending on whether a given option is selected. If this field is rendered
|
|
@@ -34,32 +39,43 @@ class ScalarToBooleanChoicesTransformer implements DataTransformerInterface
|
|
*
|
|
*
|
|
* @param mixed $value An array if "multiple" is set to true, a scalar
|
|
* @param mixed $value An array if "multiple" is set to true, a scalar
|
|
* value otherwise.
|
|
* value otherwise.
|
|
- * @return mixed An array if "expanded" or "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)
|
|
public function transform($value)
|
|
{
|
|
{
|
|
- $choices = $this->choiceList->getChoices();
|
|
|
|
|
|
+ if (!is_scalar($value) && !is_null($value)) {
|
|
|
|
+ throw new UnexpectedTypeException($value, 'scalar');
|
|
|
|
+ }
|
|
|
|
|
|
- foreach ($choices as $choice => $_) {
|
|
|
|
- $choices[$choice] = $choice === $value;
|
|
|
|
|
|
+ try {
|
|
|
|
+ $choices = $this->choiceList->getChoices();
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ throw new TransformationFailedException('Can not get the choice list', $e->getCode(), $e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ foreach (array_keys($choices) as $key) {
|
|
|
|
+ $choices[$key] = $key === $value;
|
|
}
|
|
}
|
|
|
|
|
|
return $choices;
|
|
return $choices;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Transforms a checkbox/radio button array to a single choice or an array
|
|
|
|
- * of choices.
|
|
|
|
|
|
+ * Transforms a checkbox/radio button array to a single choice.
|
|
*
|
|
*
|
|
* The input value is an array with the choices as keys and true/false as
|
|
* The input value is an array with the choices as keys and true/false as
|
|
* values, depending on whether a given choice is selected. The output
|
|
* values, depending on whether a given choice is selected. The output
|
|
- * is an array with the selected choices or a single selected choice.
|
|
|
|
|
|
+ * is the selected choice.
|
|
*
|
|
*
|
|
- * @param mixed $value An array if "expanded" or "multiple" is set to true,
|
|
|
|
- * a scalar value otherwise.
|
|
|
|
- * @return mixed $value An array if "multiple" is set to true, a scalar
|
|
|
|
- * value otherwise.
|
|
|
|
|
|
+ * @param array $value An array of values
|
|
|
|
+ *
|
|
|
|
+ * @return mixed $value A scalar value
|
|
|
|
+ *
|
|
|
|
+ * @throws new UnexpectedTypeException if the given value is not an array
|
|
*/
|
|
*/
|
|
public function reverseTransform($value)
|
|
public function reverseTransform($value)
|
|
{
|
|
{
|