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 = [];
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

View File

@ -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;

View File

@ -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 <group>_<parameter>
* @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 <group>_<parameter>
* @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;
}

View File

@ -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"