Skip to content

Commit

Permalink
Simplify the date range validator and make it more aligned with SF st…
Browse files Browse the repository at this point in the history
…andards
  • Loading branch information
loevgaard committed Jan 2, 2025
1 parent f00467f commit 385fdee
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 17 deletions.
8 changes: 2 additions & 6 deletions src/Resources/config/services/validator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<services>
<defaults public="true"/>

<service id="setono_sylius_catalog_promotion.validator.date_range"
class="Setono\SyliusCatalogPromotionPlugin\Validator\Constraints\PromotionDateRangeValidator">
<tag name="validator.constraint_validator"
alias="setono_sylius_catalog_promotion_promotion_date_range_validator"/>
<service id="Setono\SyliusCatalogPromotionPlugin\Validator\Constraints\PromotionDateRangeValidator">
<tag name="validator.constraint_validator"/>
</service>
</services>
</container>
1 change: 0 additions & 1 deletion src/Resources/config/validation/Promotion.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<option name="groups">setono_sylius_catalog_promotion</option>
</constraint>
<constraint name="Setono\SyliusCatalogPromotionPlugin\Validator\Constraints\PromotionDateRange">
<option name="message">setono_sylius_catalog_promotion.promotion.end_date_cannot_be_set_prior_start_date</option>
<option name="groups">setono_sylius_catalog_promotion</option>
</constraint>
<property name="code">
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/translations/validators.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
5 changes: 0 additions & 5 deletions src/Validator/Constraints/PromotionDateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
14 changes: 10 additions & 4 deletions src/Validator/Constraints/PromotionDateRangeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand All @@ -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')
Expand Down

0 comments on commit 385fdee

Please sign in to comment.