mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
Merge pull request #1403 from goetas/boolean-enums
Add boolean enums support to forms
This commit is contained in:
commit
67ff6bd1e1
@ -147,7 +147,14 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
|
||||
}
|
||||
if (($choices = $config->getOption('choices')) && is_array($choices) && count($choices)) {
|
||||
$enums = array_values($choices);
|
||||
$type = $this->isNumbersArray($enums) ? 'number' : 'string';
|
||||
if ($this->isNumbersArray($enums)) {
|
||||
$type = 'number';
|
||||
} elseif ($this->isBooleansArray($enums)) {
|
||||
$type = 'boolean';
|
||||
} else {
|
||||
$type = 'string';
|
||||
}
|
||||
|
||||
if ($config->getOption('multiple')) {
|
||||
$property->getItems()->setType($type)->setEnum($enums);
|
||||
} else {
|
||||
@ -233,6 +240,22 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
*
|
||||
* @return bool true if $array contains only booleans, false otherwise
|
||||
*/
|
||||
private function isBooleansArray(array $array): bool
|
||||
{
|
||||
foreach ($array as $item) {
|
||||
if (!is_bool($item)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResolvedFormTypeInterface $type
|
||||
*
|
||||
|
@ -26,6 +26,7 @@ class DummyType extends AbstractType
|
||||
{
|
||||
$builder->add('bar', TextType::class, ['required' => false]);
|
||||
$builder->add('foo', ChoiceType::class, ['choices' => ['male', 'female']]);
|
||||
$builder->add('boo', ChoiceType::class, ['choices' => [true, false], 'required' => false]);
|
||||
$builder->add('foz', ChoiceType::class, ['choices' => ['male', 'female'], 'multiple' => true]);
|
||||
$builder->add('baz', CheckboxType::class, ['required' => false]);
|
||||
$builder->add('bey', IntegerType::class, ['required' => false]);
|
||||
|
@ -285,6 +285,10 @@ class FunctionalTest extends WebTestCase
|
||||
'type' => 'string',
|
||||
'enum' => ['male', 'female'],
|
||||
],
|
||||
'boo' => [
|
||||
'type' => 'boolean',
|
||||
'enum' => [true, false],
|
||||
],
|
||||
'foz' => [
|
||||
'type' => 'array',
|
||||
'items' => [
|
||||
|
Loading…
x
Reference in New Issue
Block a user