Exception
3 months ago
SpecificationFactory
3 months ago
AggregateSpecification.php
3 months ago
AndSpecification.php
3 months ago
CollectionOperators.php
3 months ago
CollectionSpecification.php
3 months ago
ComparisonOperators.php
3 months ago
ComparisonSpecification.php
3 months ago
CompositeSpecification.php
3 months ago
ContainsSpecification.php
3 months ago
Context.php
3 months ago
ContextAwareSpecification.php
3 months ago
DateComparisonSpecification.php
3 months ago
DateOperators.php
3 months ago
DateRangeSpecification.php
3 months ago
DateRelativeDaysSpecification.php
3 months ago
DateRelativeDeductedSpecification.php
3 months ago
DateSpecification.php
3 months ago
DateTimeFactory.php
3 months ago
DateTrait.php
3 months ago
EndsWithSpecification.php
3 months ago
FactSpecification.php
3 months ago
FactTrait.php
3 months ago
FloatComparisonSpecification.php
3 months ago
FloatRangeSpecification.php
3 months ago
IntegerComparisonSpecification.php
3 months ago
IntegerRangeSpecification.php
3 months ago
NotSpecification.php
3 months ago
NullSpecification.php
3 months ago
OperatorExpression.php
3 months ago
OrSpecification.php
3 months ago
RangeOperators.php
3 months ago
RangeSpecification.php
3 months ago
Specification.php
3 months ago
SpecificationFactory.php
3 months ago
StartsWithSpecification.php
3 months ago
StringComparisonSpecification.php
3 months ago
StringMatchSpecification.php
3 months ago
StringOperators.php
3 months ago
TypeSpecification.php
3 months ago
TypeTrait.php
3 months ago
Types.php
3 months ago
DateRelativeDaysSpecification.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Expression; |
| 6 | |
| 7 | use AC\DateFormats; |
| 8 | use AC\Expression\Exception\InvalidDateFormatException; |
| 9 | use AC\Expression\Exception\OperatorNotFoundException; |
| 10 | use DateTimeZone; |
| 11 | |
| 12 | final class DateRelativeDaysSpecification extends OperatorExpression implements FactSpecification |
| 13 | { |
| 14 | |
| 15 | use DateTrait; |
| 16 | use FactTrait; |
| 17 | |
| 18 | public function __construct(string $operator, int $fact, ?string $format = null, ?DateTimeZone $timezone = null) |
| 19 | { |
| 20 | parent::__construct($operator); |
| 21 | |
| 22 | $this->fact = DateTimeFactory::create($timezone)->modify($this->get_modifier_string($fact, $operator)); |
| 23 | $this->format = $format; |
| 24 | $this->timezone = $timezone; |
| 25 | } |
| 26 | |
| 27 | private function get_modifier_string(int $fact, $operator): string |
| 28 | { |
| 29 | switch ($operator) { |
| 30 | case DateOperators::WITHIN_DAYS: |
| 31 | return sprintf('+%d days', $fact); |
| 32 | case DateOperators::GT_DAYS_AGO: |
| 33 | case DateOperators::LT_DAYS_AGO: |
| 34 | return sprintf('-%d days', $fact); |
| 35 | } |
| 36 | |
| 37 | throw new OperatorNotFoundException($operator); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @throws InvalidDateFormatException |
| 42 | */ |
| 43 | public function is_satisfied_by($value): bool |
| 44 | { |
| 45 | // Format that discards time |
| 46 | $format = DateFormats::DATE_MYSQL; |
| 47 | |
| 48 | $today = DateTimeFactory::create($this->timezone)->format($format); |
| 49 | $date = DateTimeFactory::create_from_format($value, $this->format, $this->timezone)->format($format); |
| 50 | $fact = $this->fact->format($format); |
| 51 | |
| 52 | switch ($this->operator) { |
| 53 | case DateOperators::WITHIN_DAYS: |
| 54 | return $date <= $fact && $date >= $today; |
| 55 | case DateOperators::LT_DAYS_AGO: |
| 56 | return $date <= $today && $date >= $fact; |
| 57 | case DateOperators::GT_DAYS_AGO: |
| 58 | return $date <= $fact; |
| 59 | } |
| 60 | |
| 61 | throw new OperatorNotFoundException($this->operator); |
| 62 | } |
| 63 | |
| 64 | public function export(): array |
| 65 | { |
| 66 | return array_merge( |
| 67 | parent::export(), |
| 68 | $this->export_date(), |
| 69 | $this->export_fact() |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | } |