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

View File

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

View File

@ -7,6 +7,7 @@ use Ardent\ParameterBundle\Entity\Parameter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
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\TextType;
@ -23,6 +24,9 @@ class ParameterService
$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
{
$split = explode('_', $name);
@ -88,6 +92,9 @@ class ParameterService
case ArdentParameterBundle::TYPE_BOOL:
$class = CheckboxType::class;
break;
case ArdentParameterBundle::TYPE_CHOICE:
$class = ChoiceType::class;
break;
}
// Add the group name in front of the parameter name