Allow choice type in parameter config

This commit is contained in:
Tim 2023-02-21 01:03:37 +01:00
parent 7040fd5ea7
commit 61b8a68705
3 changed files with 25 additions and 14 deletions

View File

@ -14,6 +14,7 @@ class ArdentParameterBundle extends AbstractBundle
public const TYPE_TEXT = 'text'; public const TYPE_TEXT = 'text';
public const TYPE_NUMBER = 'number'; public const TYPE_NUMBER = 'number';
public const TYPE_BOOL = 'bool'; public const TYPE_BOOL = 'bool';
public const TYPE_CHOICE = 'choice';
public function configure(DefinitionConfigurator $definition): void public function configure(DefinitionConfigurator $definition): void
{ {
@ -21,19 +22,22 @@ class ArdentParameterBundle extends AbstractBundle
$definition->rootNode() $definition->rootNode()
->children() ->children()
->arrayNode('parameters') ->arrayNode('parameters')
->arrayPrototype() ->arrayPrototype()
->arrayPrototype() ->arrayPrototype()
->children() ->children()
->scalarNode('name')->end() ->scalarNode('name')->end()
->enumNode('type') ->enumNode('type')
->values([self::TYPE_TEXT, self::TYPE_NUMBER, self::TYPE_BOOL]) ->values([self::TYPE_TEXT, self::TYPE_NUMBER, self::TYPE_BOOL, self::TYPE_CHOICE])
->defaultValue(self::TYPE_TEXT) ->defaultValue(self::TYPE_TEXT)
->end() ->end()
->end() ->arrayNode('choices')
->end() ->scalarPrototype()->end()
->end() ->end()
->end() ->end()
->end()
->end()
->end()
->end(); ->end();
} }

View File

@ -61,7 +61,7 @@ abstract class BaseController extends AbstractController
$options = []; $options = [];
switch ($type) { switch ($type) {
case ChoiceType::class: case ChoiceType::class:
$choices = $config['choices']; $choices = array_combine($config['choices'], $config['choices']);
if (is_callable($choices)) { if (is_callable($choices)) {
$options['choices'] = $choices(); $options['choices'] = $choices();

View File

@ -7,6 +7,7 @@ use Ardent\ParameterBundle\Entity\Parameter;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
@ -23,6 +24,9 @@ class ParameterService
$this->config = $this->supplementConfig($config); $this->config = $this->supplementConfig($config);
} }
/**
* @param string $name The name of the parameter in the format <group>_<parameter>
*/
public function get(string $name, bool $parse = true): mixed public function get(string $name, bool $parse = true): mixed
{ {
$split = explode('_', $name); $split = explode('_', $name);
@ -88,6 +92,9 @@ class ParameterService
case ArdentParameterBundle::TYPE_BOOL: case ArdentParameterBundle::TYPE_BOOL:
$class = CheckboxType::class; $class = CheckboxType::class;
break; break;
case ArdentParameterBundle::TYPE_CHOICE:
$class = ChoiceType::class;
break;
} }
// Add the group name in front of the parameter name // Add the group name in front of the parameter name