Skip to content

Commit 385fdee

Browse files
committed
Simplify the date range validator and make it more aligned with SF standards
1 parent f00467f commit 385fdee

File tree

5 files changed

+13
-17
lines changed

5 files changed

+13
-17
lines changed

src/Resources/config/services/validator.xml

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
xmlns="http://symfony.com/schema/dic/services"
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66
<services>
7-
<defaults public="true"/>
8-
9-
<service id="setono_sylius_catalog_promotion.validator.date_range"
10-
class="Setono\SyliusCatalogPromotionPlugin\Validator\Constraints\PromotionDateRangeValidator">
11-
<tag name="validator.constraint_validator"
12-
alias="setono_sylius_catalog_promotion_promotion_date_range_validator"/>
7+
<service id="Setono\SyliusCatalogPromotionPlugin\Validator\Constraints\PromotionDateRangeValidator">
8+
<tag name="validator.constraint_validator"/>
139
</service>
1410
</services>
1511
</container>

src/Resources/config/validation/Promotion.xml

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<option name="groups">setono_sylius_catalog_promotion</option>
1212
</constraint>
1313
<constraint name="Setono\SyliusCatalogPromotionPlugin\Validator\Constraints\PromotionDateRange">
14-
<option name="message">setono_sylius_catalog_promotion.promotion.end_date_cannot_be_set_prior_start_date</option>
1514
<option name="groups">setono_sylius_catalog_promotion</option>
1615
</constraint>
1716
<property name="code">

src/Resources/translations/validators.en.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ setono_sylius_catalog_promotion:
55
regex: Promotion code can only be comprised of letters (a-z), numbers (0-9), dashes (-) and underscores (_).
66
discount:
77
range: Please enter value between 0% and 100%.
8-
end_date_cannot_be_set_prior_start_date: End date cannot be set prior start date.
8+
end_date_cannot_be_set_prior_start_date: End date must be set after start date.

src/Validator/Constraints/PromotionDateRange.php

-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,4 @@ public function getTargets(): string
1414
{
1515
return self::CLASS_CONSTRAINT;
1616
}
17-
18-
public function validatedBy(): string
19-
{
20-
return 'setono_sylius_catalog_promotion_promotion_date_range_validator';
21-
}
2217
}

src/Validator/Constraints/PromotionDateRangeValidator.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
use Setono\SyliusCatalogPromotionPlugin\Model\PromotionInterface;
88
use Symfony\Component\Validator\Constraint;
99
use Symfony\Component\Validator\ConstraintValidator;
10-
use Webmozart\Assert\Assert;
10+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
11+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1112

1213
final class PromotionDateRangeValidator extends ConstraintValidator
1314
{
@@ -21,8 +22,13 @@ public function validate($value, Constraint $constraint): void
2122
return;
2223
}
2324

24-
Assert::isInstanceOf($value, PromotionInterface::class);
25-
Assert::isInstanceOf($constraint, PromotionDateRange::class);
25+
if (!$constraint instanceof PromotionDateRange) {
26+
throw new UnexpectedTypeException($constraint, PromotionDateRange::class);
27+
}
28+
29+
if (!$value instanceof PromotionInterface) {
30+
throw new UnexpectedValueException($value, PromotionInterface::class);
31+
}
2632

2733
$startsAt = $value->getStartsAt();
2834
$endsAt = $value->getEndsAt();
@@ -31,7 +37,7 @@ public function validate($value, Constraint $constraint): void
3137
return;
3238
}
3339

34-
if ($startsAt->getTimestamp() > $endsAt->getTimestamp()) {
40+
if ($startsAt > $endsAt) {
3541
$this->context
3642
->buildViolation($constraint->message)
3743
->atPath('endsAt')

0 commit comments

Comments
 (0)