mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-09-13 13:57:10 +02:00
Optimized ParameterManagement
This commit is contained in:
@@ -22,14 +22,12 @@ class OptionalGetParameterService implements OptionalGetParameterServiceInterfac
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @deprecated
|
||||
*
|
||||
* @throws UnvalidParameterException If the parameter is not valid
|
||||
*/
|
||||
protected function validateParameter(string $key): void
|
||||
{
|
||||
if (in_array($key, self::OPTIONAL_PARAMETERS)) {
|
||||
return;
|
||||
}
|
||||
throw new UnvalidParameterException("Parameter <<$key>> isn't valid.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -9,13 +9,6 @@ namespace Infinito\Domain\ParameterManagement;
|
||||
*/
|
||||
interface OptionalGetParameterServiceInterface
|
||||
{
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const VERSION_PARAMETER = 'version';
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
@@ -57,7 +50,6 @@ interface OptionalGetParameterServiceInterface
|
||||
* @var array|string[]
|
||||
*/
|
||||
const OPTIONAL_PARAMETERS = [
|
||||
self::VERSION_PARAMETER,
|
||||
self::VIEW_PARAMETER,
|
||||
self::CLASS_PARAMETER,
|
||||
self::FRAME_PARAMETER,
|
||||
|
@@ -3,6 +3,8 @@
|
||||
namespace Infinito\Domain\ParameterManagement\Parameter;
|
||||
|
||||
/**
|
||||
* Parameter classes shouldn't throw exceptions!
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
abstract class AbstractParameter implements ParameterInterface
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace Infinito\Domain\ParameterManagement\Parameter;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use Infinito\Exception\SetNotPossibleException;
|
||||
use Infinito\Exception\UnvalidGetParameterException;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
@@ -16,7 +16,7 @@ final class FrameParameter extends AbstractParameter
|
||||
const STANDART_VALUE = true;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
* @var bool
|
||||
* @Assert\Type("bool")
|
||||
*/
|
||||
protected $value;
|
||||
@@ -28,20 +28,26 @@ final class FrameParameter extends AbstractParameter
|
||||
*/
|
||||
public function setValue($value): void
|
||||
{
|
||||
if (is_null($value)) {
|
||||
//Use standart value
|
||||
$this->value = self::STANDART_VALUE;
|
||||
$type = gettype($value);
|
||||
switch ($type) {
|
||||
case 'NULL':
|
||||
// Use standart value
|
||||
$this->value = self::STANDART_VALUE;
|
||||
|
||||
return;
|
||||
return;
|
||||
case 'boolean':
|
||||
$this->value = $value;
|
||||
|
||||
return;
|
||||
}
|
||||
if (is_numeric($value)) {
|
||||
$number = (int) $value;
|
||||
if ($number >= 0 && $number <= 1) {
|
||||
$value = (int) $value;
|
||||
if ($value >= 0 && $value <= 1) {
|
||||
$this->value = (bool) $value;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new SetNotPossibleException("It\'s not possible to set <<$value>> of type <<".gettype($value).'>> for class <<'.get_class().'>>. Just 0 and 1 are allowed!');
|
||||
throw new UnvalidGetParameterException("It\'s not possible to set <<$value>> of type <<".$type.'>> for class <<'.get_class().'>>. Just 0 and 1 are allowed!');
|
||||
}
|
||||
}
|
||||
|
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace Infinito\Domain\ParameterManagement;
|
||||
|
||||
use Infinito\Exception\UnvalidParameterException;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use Infinito\Exception\UnvalidGetParameterException;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
@@ -40,12 +40,11 @@ final class ValidGetParametersService extends OptionalGetParameterService implem
|
||||
*/
|
||||
protected function validateParameter(string $key): void
|
||||
{
|
||||
parent::validateParameter($key);
|
||||
$parameter = $this->parameterFactory->getParameter($key);
|
||||
$parameter->setValue($this->currentRequest->get($key));
|
||||
$errors = $this->validator->validate($parameter);
|
||||
foreach ($errors as $error) {
|
||||
throw new UnvalidParameterException("Parameter <<$key>> didn't pass the validation; Message: <<".$error->getMessage().'>> ,Value: <<'.$parameter->getValue().'>> .');
|
||||
throw new UnvalidGetParameterException("Parameter <<$key>> didn't pass the validation; Message: <<".$error->getMessage().'>> ,Value: <<'.$parameter->getValue().'>> .');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,6 +8,7 @@ use Infinito\Domain\ActionManagement\ActionServiceInterface;
|
||||
use Infinito\Domain\ActionManagement\ActionFactoryServiceInterface;
|
||||
use Infinito\Domain\TemplateManagement\TemplateNameServiceInterface;
|
||||
use Infinito\Domain\ParameterManagement\ValidGetParameterServiceInterface;
|
||||
use Infinito\Domain\ParameterManagement\Parameter\FrameParameter;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
@@ -56,8 +57,8 @@ final class ViewBuilder implements ViewBuilderInterface
|
||||
*/
|
||||
private function checkLoadWithFrame(): bool
|
||||
{
|
||||
if ($this->validGetParameterService->hasParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER)) {
|
||||
return $this->validGetParameterService->getParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER);
|
||||
if ($this->validGetParameterService->hasParameter(FrameParameter::getKey())) {
|
||||
return $this->validGetParameterService->getParameter(FrameParameter::getKey());
|
||||
}
|
||||
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user