From a2fe3584be0ae1fc5e19019672f260b736730068 Mon Sep 17 00:00:00 2001 From: tim Date: Tue, 1 Feb 2022 21:46:27 +0100 Subject: [PATCH] Improve parsing of parameters when giving them back Update some things to php 8 --- Controller/BaseController.php | 12 +------- Entity/Parameter.php | 45 ++++++++++++--------------- Service/ParameterService.php | 57 +++++++++++++---------------------- composer.json | 1 + 4 files changed, 43 insertions(+), 72 deletions(-) diff --git a/Controller/BaseController.php b/Controller/BaseController.php index 4b65f51..83c5c61 100644 --- a/Controller/BaseController.php +++ b/Controller/BaseController.php @@ -23,7 +23,7 @@ abstract class BaseController extends AbstractController $data = []; foreach ($configuration as $config) { $name = $config['name']; - $data[$name] = $this->valueParser($config['type'], $param->get($name, false)); + $data[$name] = $param->get($name, false); } // build the form @@ -55,16 +55,6 @@ abstract class BaseController extends AbstractController ]); } - private function valueParser($type, $value) - { - switch ($type) { - case CheckboxType::class: - return boolval($value); - default: - return $value; - } - } - /** * @param $config * @param FormBuilderInterface $formBuilder diff --git a/Entity/Parameter.php b/Entity/Parameter.php index 3e76ae0..92d43b9 100644 --- a/Entity/Parameter.php +++ b/Entity/Parameter.php @@ -2,7 +2,10 @@ namespace Ardent\ParameterBundle\Entity; +use Doctrine\DBAL\Types\TextType; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; +use Symfony\Component\Form\Extension\Core\Type\NumberType; /** * @ORM\Entity @@ -15,57 +18,49 @@ class Parameter * @ORM\GeneratedValue(strategy="AUTO") * @ORM\Column(type="integer") */ - private $id; + private ?int $id; /** * @ORM\Column(type="string") */ - private $name; + private ?string $name; /** * @ORM\Column(type="string") */ - private $value; + private mixed $value; - /** - * @return mixed - */ - public function getId() + public function getId(): int { return $this->id; } - /** - * @return mixed - */ - public function getName() + public function getName(): string { return $this->name; } - /** - * @param mixed $name - * @return Parameter - */ - public function setName($name) + public function setName(string $name): self { $this->name = $name; return $this; } - /** - * @return mixed - */ - public function getValue() + public function getValue(): mixed { return $this->value; } - /** - * @param mixed $value - * @return Parameter - */ - public function setValue($value) + static function parseValue(mixed $value, string $type): mixed + { + return match ($type) { + NumberType::class => intval($value), + CheckboxType::class => boolval($value), + default => $value, + }; + } + + public function setValue(mixed $value): self { $this->value = $value; return $this; diff --git a/Service/ParameterService.php b/Service/ParameterService.php index 50d02b9..99a9b4c 100644 --- a/Service/ParameterService.php +++ b/Service/ParameterService.php @@ -12,36 +12,24 @@ use Symfony\Component\Form\Extension\Core\Type\TextType; class ParameterService { - /** @var EntityManagerInterface */ - private $em; + private array $config; - /** @var ParameterBagInterface */ - private $parameter; - private $config; - - /** - * ParameterService constructor. - * - * @param EntityManagerInterface $em - * @param ParameterBagInterface $parameter - * @param $config - */ - public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameter, $config) + public function __construct( + private EntityManagerInterface $em, + private ParameterBagInterface $parameter, + array $config + ) { - $this->em = $em; - $this->parameter = $parameter; $this->config = $this->supplementConfig($config); } - /** - * @param string $name The name of the parameter in the format _ - * @param bool $parse Whether to parse embedded %parameter%'s - * - * @return mixed - */ - public function get($name, $parse = true) + public function get(string $name, bool $parse = true): mixed { + $split = explode('_', $name); + $category = array_shift($split); + $paramRepo = $this->em->getRepository(Parameter::class); + /** @var Parameter $parameter */ $parameter = $paramRepo->findOneBy(['name' => $name]); if ($parameter) { if ($parse) { @@ -61,7 +49,7 @@ class ParameterService $value = $parameter->getValue(); } - return $value; + return Parameter::parseValue($value, $this->config[$category][$name]['type']); } else { return null; } @@ -69,9 +57,8 @@ class ParameterService /** * @param string $name The name of the parameter in the format _ - * @param mixed $value */ - public function set($name, $value) + public function set(string $name, mixed $value): void { $paramRepo = $this->em->getRepository(Parameter::class); /* @var Parameter $parameter */ @@ -84,16 +71,12 @@ class ParameterService $this->em->flush(); } - /** - * Replaces the types in the config by their classes - * - * @param $config - * @return array - */ - private function supplementConfig($config) + private function supplementConfig(array $rawConfig): array { - foreach ($config as $groupName => &$group) { - foreach ($group as &$value) { + $config = []; + foreach ($rawConfig as $groupName => $rawGroup) { + $group = []; + foreach ($rawGroup as &$value) { switch ($value['type']) { default: case Configuration::TYPE_TEXT: @@ -110,9 +93,11 @@ class ParameterService // Add the group name in front of the parameter name $value['name'] = sprintf('%s_%s', $groupName, $value['name']); $value['type'] = $class; + + $group[$value['name']] = $value; unset($value); } - unset($group); + $config[$groupName] = $group; } return $config; } diff --git a/composer.json b/composer.json index a48a032..84edeb8 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,7 @@ "description": "Simple bundle for storing parameters in a database", "type": "symfony-bundle", "require": { + "php": "^8.0", "doctrine/orm": "^2.0", "symfony/form": "^5.0|^6.0", "symfony/twig-bundle": "^5.0|^6.0"