Improve parsing of parameters when giving them back

Update some things to php 8
This commit is contained in:
Tim 2022-02-01 21:46:27 +01:00
parent dacf3d6687
commit a2fe3584be
4 changed files with 43 additions and 72 deletions

View File

@ -23,7 +23,7 @@ abstract class BaseController extends AbstractController
$data = []; $data = [];
foreach ($configuration as $config) { foreach ($configuration as $config) {
$name = $config['name']; $name = $config['name'];
$data[$name] = $this->valueParser($config['type'], $param->get($name, false)); $data[$name] = $param->get($name, false);
} }
// build the form // 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 $config
* @param FormBuilderInterface $formBuilder * @param FormBuilderInterface $formBuilder

View File

@ -2,7 +2,10 @@
namespace Ardent\ParameterBundle\Entity; namespace Ardent\ParameterBundle\Entity;
use Doctrine\DBAL\Types\TextType;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
/** /**
* @ORM\Entity * @ORM\Entity
@ -15,57 +18,49 @@ class Parameter
* @ORM\GeneratedValue(strategy="AUTO") * @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer") * @ORM\Column(type="integer")
*/ */
private $id; private ?int $id;
/** /**
* @ORM\Column(type="string") * @ORM\Column(type="string")
*/ */
private $name; private ?string $name;
/** /**
* @ORM\Column(type="string") * @ORM\Column(type="string")
*/ */
private $value; private mixed $value;
/** public function getId(): int
* @return mixed
*/
public function getId()
{ {
return $this->id; return $this->id;
} }
/** public function getName(): string
* @return mixed
*/
public function getName()
{ {
return $this->name; return $this->name;
} }
/** public function setName(string $name): self
* @param mixed $name
* @return Parameter
*/
public function setName($name)
{ {
$this->name = $name; $this->name = $name;
return $this; return $this;
} }
/** public function getValue(): mixed
* @return mixed
*/
public function getValue()
{ {
return $this->value; return $this->value;
} }
/** static function parseValue(mixed $value, string $type): mixed
* @param mixed $value {
* @return Parameter return match ($type) {
*/ NumberType::class => intval($value),
public function setValue($value) CheckboxType::class => boolval($value),
default => $value,
};
}
public function setValue(mixed $value): self
{ {
$this->value = $value; $this->value = $value;
return $this; return $this;

View File

@ -12,36 +12,24 @@ use Symfony\Component\Form\Extension\Core\Type\TextType;
class ParameterService class ParameterService
{ {
/** @var EntityManagerInterface */ private array $config;
private $em;
/** @var ParameterBagInterface */ public function __construct(
private $parameter; private EntityManagerInterface $em,
private $config; private ParameterBagInterface $parameter,
array $config
/** )
* ParameterService constructor.
*
* @param EntityManagerInterface $em
* @param ParameterBagInterface $parameter
* @param $config
*/
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameter, $config)
{ {
$this->em = $em;
$this->parameter = $parameter;
$this->config = $this->supplementConfig($config); $this->config = $this->supplementConfig($config);
} }
/** public function get(string $name, bool $parse = true): mixed
* @param string $name The name of the parameter in the format <group>_<parameter>
* @param bool $parse Whether to parse embedded %parameter%'s
*
* @return mixed
*/
public function get($name, $parse = true)
{ {
$split = explode('_', $name);
$category = array_shift($split);
$paramRepo = $this->em->getRepository(Parameter::class); $paramRepo = $this->em->getRepository(Parameter::class);
/** @var Parameter $parameter */
$parameter = $paramRepo->findOneBy(['name' => $name]); $parameter = $paramRepo->findOneBy(['name' => $name]);
if ($parameter) { if ($parameter) {
if ($parse) { if ($parse) {
@ -61,7 +49,7 @@ class ParameterService
$value = $parameter->getValue(); $value = $parameter->getValue();
} }
return $value; return Parameter::parseValue($value, $this->config[$category][$name]['type']);
} else { } else {
return null; return null;
} }
@ -69,9 +57,8 @@ class ParameterService
/** /**
* @param string $name The name of the parameter in the format <group>_<parameter> * @param string $name The name of the parameter in the format <group>_<parameter>
* @param mixed $value
*/ */
public function set($name, $value) public function set(string $name, mixed $value): void
{ {
$paramRepo = $this->em->getRepository(Parameter::class); $paramRepo = $this->em->getRepository(Parameter::class);
/* @var Parameter $parameter */ /* @var Parameter $parameter */
@ -84,16 +71,12 @@ class ParameterService
$this->em->flush(); $this->em->flush();
} }
/** private function supplementConfig(array $rawConfig): array
* Replaces the types in the config by their classes
*
* @param $config
* @return array
*/
private function supplementConfig($config)
{ {
foreach ($config as $groupName => &$group) { $config = [];
foreach ($group as &$value) { foreach ($rawConfig as $groupName => $rawGroup) {
$group = [];
foreach ($rawGroup as &$value) {
switch ($value['type']) { switch ($value['type']) {
default: default:
case Configuration::TYPE_TEXT: case Configuration::TYPE_TEXT:
@ -110,9 +93,11 @@ class ParameterService
// Add the group name in front of the parameter name // Add the group name in front of the parameter name
$value['name'] = sprintf('%s_%s', $groupName, $value['name']); $value['name'] = sprintf('%s_%s', $groupName, $value['name']);
$value['type'] = $class; $value['type'] = $class;
$group[$value['name']] = $value;
unset($value); unset($value);
} }
unset($group); $config[$groupName] = $group;
} }
return $config; return $config;
} }

View File

@ -3,6 +3,7 @@
"description": "Simple bundle for storing parameters in a database", "description": "Simple bundle for storing parameters in a database",
"type": "symfony-bundle", "type": "symfony-bundle",
"require": { "require": {
"php": "^8.0",
"doctrine/orm": "^2.0", "doctrine/orm": "^2.0",
"symfony/form": "^5.0|^6.0", "symfony/form": "^5.0|^6.0",
"symfony/twig-bundle": "^5.0|^6.0" "symfony/twig-bundle": "^5.0|^6.0"