Skip to content

Commit

Permalink
Fix Symfony validator code
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Dec 15, 2023
1 parent fe96e50 commit e75c4e1
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,29 @@ final class After extends AbstractComparison
{
public const BEFORE_OR_EQUAL_ERROR = '99f63b74-a275-4a01-8678-63124971bff8';

/**
* @var array<string, string>
*/
protected const ERROR_NAMES = [
self::BEFORE_OR_EQUAL_ERROR => 'BEFORE_OR_EQUAL_ERROR',
];

/**
* @var array<string, string>
*/
protected static $errorNames = [
self::BEFORE_OR_EQUAL_ERROR => 'BEFORE_OR_EQUAL_ERROR',
];

public $message = 'This value should be after {{ compared_value }}.';
public function __construct(mixed $value = null, string $propertyPath = null, string $message = 'This value should be after {{ compared_value }}.', array $groups = null, mixed $payload = null, array $options = [])
{
parent::__construct(
$value,
$propertyPath,
$message,
$groups,
$payload,
$options
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,29 @@ final class AfterOrEqual extends AbstractComparison
{
public const BEFORE_ERROR = '1c6d2666-52d7-4131-bd11-3f90e2120c2d';

/**
* @var array<string, string>
*/
protected const ERROR_NAMES = [
self::BEFORE_ERROR => 'BEFORE_ERROR',
];

/**
* @var array<string, string>
*/
protected static $errorNames = [
self::BEFORE_ERROR => 'BEFORE_ERROR',
];

public $message = 'This value should be after or equal {{ compared_value }}.';
public function __construct(mixed $value = null, string $propertyPath = null, string $message = 'This value should be after or equal {{ compared_value }}.', array $groups = null, mixed $payload = null, array $options = [])
{
parent::__construct(
$value,
$propertyPath,
$message,
$groups,
$payload,
$options
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class AfterOrEqualValidator extends AbstractComparisonValidator
{
/**
* @param DateTime|Day|Month|Year $value1
* @param ?DateTime|?Day|?Month|?Year $value2
* @param null|DateTime|Day|Month|Year $value2
*
* @return bool
*/
Expand All @@ -28,7 +28,7 @@ protected function compareValues($value1, $value2) : bool
return false;
}

return $value1->isAfterOrEqual($value2);
return $value1->isAfterOrEqualTo($value2);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class AfterValidator extends AbstractComparisonValidator
{
/**
* @param DateTime|Day|Month|Year $value1
* @param ?DateTime|?Day|?Month|?Year $value2
* @param null|DateTime|Day|Month|Year $value2
*
* @return bool
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,29 @@ final class Before extends AbstractComparison
{
public const BEFORE_OR_EQUAL_ERROR = 'c561f511-0fee-4fed-8505-6e67e21aa903';

/**
* @var array<string, string>
*/
protected const ERROR_NAMES = [
self::BEFORE_OR_EQUAL_ERROR => 'BEFORE_OR_EQUAL_ERROR',
];

/**
* @var array<string, string>
*/
protected static $errorNames = [
self::BEFORE_OR_EQUAL_ERROR => 'BEFORE_OR_EQUAL_ERROR',
];

public $message = 'This value should be before {{ compared_value }}.';
public function __construct(mixed $value = null, string $propertyPath = null, string $message = 'This value should be before {{ compared_value }}.', array $groups = null, mixed $payload = null, array $options = [])
{
parent::__construct(
$value,
$propertyPath,
$message,
$groups,
$payload,
$options
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,29 @@ final class BeforeOrEqual extends AbstractComparison
{
public const AFTER_ERROR = 'c411b575-c9fd-4e22-af8a-2e23a565d9a4';

/**
* @var array<string, string>
*/
protected const ERROR_NAMES = [
self::AFTER_ERROR => 'AFTER_ERROR',
];

/**
* @var array<string, string>
*/
protected static $errorNames = [
self::AFTER_ERROR => 'AFTER_ERROR',
];

public $message = 'This value should be before or equal {{ compared_value }}.';
public function __construct(mixed $value = null, string $propertyPath = null, string $message = 'This value should be before or equal {{ compared_value }}.', array $groups = null, mixed $payload = null, array $options = [])
{
parent::__construct(
$value,
$propertyPath,
$message,
$groups,
$payload,
$options
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class BeforeOrEqualValidator extends AbstractComparisonValidator
{
/**
* @param DateTime|Day|Month|Year $value1
* @param ?DateTime|?Day|?Month|?Year $value2
* @param null|DateTime|Day|Month|Year $value2
*
* @return bool
*/
Expand All @@ -28,7 +28,7 @@ protected function compareValues($value1, $value2) : bool
return false;
}

return $value1->isBeforeOrEqual($value2);
return $value1->isBeforeOrEqualTo($value2);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class BeforeValidator extends AbstractComparisonValidator
{
/**
* @param DateTime|Day|Month|Year $value1
* @param ?DateTime|?Day|?Month|?Year $value2
* @param null|DateTime|Day|Month|Year $value2
*
* @return bool
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,29 @@ final class Equal extends AbstractComparison
{
public const NOT_EQUAL_ERROR = 'c561f511-0fee-4fed-8505-6e67e21aa903';

/**
* @var array<string, string>
*/
protected const ERROR_NAMES = [
self::NOT_EQUAL_ERROR => 'NOT_EQUAL_ERROR',
];

/**
* @var array<string, string>
*/
protected static $errorNames = [
self::NOT_EQUAL_ERROR=> 'NOT_EQUAL_ERROR',
self::NOT_EQUAL_ERROR => 'NOT_EQUAL_ERROR',
];

public $message = 'This value should be equal {{ compared_value }}.';
public function __construct(mixed $value = null, string $propertyPath = null, string $message = 'This value should be equal {{ compared_value }}.', array $groups = null, mixed $payload = null, array $options = [])
{
parent::__construct(
$value,
$propertyPath,
$message,
$groups,
$payload,
$options
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ final class EqualValidator extends AbstractComparisonValidator
{
/**
* @param DateTime|Day|Month|Year $value1
* @param ?DateTime|?Day|?Month|?Year $value2
* @param null|DateTime|Day|Month|Year $value2
*
* @return bool
*/
protected function compareValues($value1, $value2) : bool
{
if (!$value2 instanceof DateTime && !$value2 instanceof Day && !$value2 instanceof Month && !$value2 instanceof Year) {
if (!$value2 instanceof DateTime && !$value2 instanceof Day && !$value2 instanceof Month && !$value2 instanceof Year) {
return false;
}

if (\get_class($value1) !== \get_class($value2)) {
return false;
}

return $value1->isEqual($value2);
return $value1->isEqualTo($value2);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,35 @@ final class Holiday extends Constraint
{
public const NOT_HOLIDAY_DAY = 'a4a2fb95-c359-4683-8fbc-307967dd28a4';

/**
* @var array<string, string>
*/
protected const ERROR_NAMES = [
self::NOT_HOLIDAY_DAY => 'NOT_HOLIDAY_DAY',
];

/**
* @var array<string, string>
*/
protected static $errorNames = [
self::NOT_HOLIDAY_DAY => 'NOT_HOLIDAY_DAY',
];

public $message = 'Day {{ day }} is not a holiday.';
public string $message = 'Day {{ day }} is not a holiday.';

public $countryCode;
public string $countryCode;

public function __construct($options = null)
{
parent::__construct($options);
}

public function getRequiredOptions()
public function getRequiredOptions() : array
{
return ['countryCode'];
}

public function validatedBy()
public function validatedBy() : string
{
return 'calendar.holidays.validator.holiday';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(HolidaysFactory $factory)
}

/**
* {@inheritdoc}
* @param mixed $value
*/
public function validate($value, Constraint $constraint) : void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,35 @@ final class NotHoliday extends Constraint
{
public const HOLIDAY_DAY = 'a4a2fb95-c359-4683-8fbc-307967dd28a4';

/**
* @var array<string, string>
*/
protected const ERROR_NAMES = [
self::HOLIDAY_DAY => 'HOLIDAY_DAY',
];

/**
* @var array<string, string>
*/
protected static $errorNames = [
self::HOLIDAY_DAY => 'HOLIDAY_DAY',
];

public $message = 'Day {{ day }} is a holiday.';
public string $message = 'Day {{ day }} is a holiday.';

public $countryCode;
public string $countryCode;

public function __construct($options = null)
{
parent::__construct($options);
}

public function getRequiredOptions()
public function getRequiredOptions() : array
{
return ['countryCode'];
}

public function validatedBy()
public function validatedBy() : string
{
return 'calendar.holidays.validator.not_holiday';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(HolidaysFactory $factory)
}

/**
* {@inheritdoc}
* @param mixed $value
*/
public function validate($value, Constraint $constraint) : void
{
Expand All @@ -34,8 +34,6 @@ public function validate($value, Constraint $constraint) : void
}

if (!\is_string($value) && !$value instanceof Day && !$value instanceof \DateTimeInterface) {
\var_dump($value);

throw new UnexpectedValueException($value, 'string or ' . Day::class);
}

Expand Down

0 comments on commit e75c4e1

Please sign in to comment.