Improve parsing of parameters when giving them back
Update some things to php 8
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user