From 385fdee0a4c090a96c8f4de71abfaca55fc56d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Thu, 2 Jan 2025 11:07:17 +0100 Subject: [PATCH] Simplify the date range validator and make it more aligned with SF standards --- src/Resources/config/services/validator.xml | 8 ++------ src/Resources/config/validation/Promotion.xml | 1 - src/Resources/translations/validators.en.yaml | 2 +- src/Validator/Constraints/PromotionDateRange.php | 5 ----- .../Constraints/PromotionDateRangeValidator.php | 14 ++++++++++---- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/Resources/config/services/validator.xml b/src/Resources/config/services/validator.xml index e58c614..e972608 100644 --- a/src/Resources/config/services/validator.xml +++ b/src/Resources/config/services/validator.xml @@ -4,12 +4,8 @@ xmlns="http://symfony.com/schema/dic/services" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - - - + + diff --git a/src/Resources/config/validation/Promotion.xml b/src/Resources/config/validation/Promotion.xml index 96cb164..8dccb8f 100644 --- a/src/Resources/config/validation/Promotion.xml +++ b/src/Resources/config/validation/Promotion.xml @@ -11,7 +11,6 @@ - diff --git a/src/Resources/translations/validators.en.yaml b/src/Resources/translations/validators.en.yaml index 5853a16..0e7cd58 100644 --- a/src/Resources/translations/validators.en.yaml +++ b/src/Resources/translations/validators.en.yaml @@ -5,4 +5,4 @@ setono_sylius_catalog_promotion: regex: Promotion code can only be comprised of letters (a-z), numbers (0-9), dashes (-) and underscores (_). discount: range: Please enter value between 0% and 100%. - end_date_cannot_be_set_prior_start_date: End date cannot be set prior start date. + end_date_cannot_be_set_prior_start_date: End date must be set after start date. diff --git a/src/Validator/Constraints/PromotionDateRange.php b/src/Validator/Constraints/PromotionDateRange.php index 7503844..4016bad 100644 --- a/src/Validator/Constraints/PromotionDateRange.php +++ b/src/Validator/Constraints/PromotionDateRange.php @@ -14,9 +14,4 @@ public function getTargets(): string { return self::CLASS_CONSTRAINT; } - - public function validatedBy(): string - { - return 'setono_sylius_catalog_promotion_promotion_date_range_validator'; - } } diff --git a/src/Validator/Constraints/PromotionDateRangeValidator.php b/src/Validator/Constraints/PromotionDateRangeValidator.php index f9b2a55..61ed8f1 100644 --- a/src/Validator/Constraints/PromotionDateRangeValidator.php +++ b/src/Validator/Constraints/PromotionDateRangeValidator.php @@ -7,7 +7,8 @@ use Setono\SyliusCatalogPromotionPlugin\Model\PromotionInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; -use Webmozart\Assert\Assert; +use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; final class PromotionDateRangeValidator extends ConstraintValidator { @@ -21,8 +22,13 @@ public function validate($value, Constraint $constraint): void return; } - Assert::isInstanceOf($value, PromotionInterface::class); - Assert::isInstanceOf($constraint, PromotionDateRange::class); + if (!$constraint instanceof PromotionDateRange) { + throw new UnexpectedTypeException($constraint, PromotionDateRange::class); + } + + if (!$value instanceof PromotionInterface) { + throw new UnexpectedValueException($value, PromotionInterface::class); + } $startsAt = $value->getStartsAt(); $endsAt = $value->getEndsAt(); @@ -31,7 +37,7 @@ public function validate($value, Constraint $constraint): void return; } - if ($startsAt->getTimestamp() > $endsAt->getTimestamp()) { + if ($startsAt > $endsAt) { $this->context ->buildViolation($constraint->message) ->atPath('endsAt')