-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix After/Before expanders error message when date/time is equal to b…
…oundary
- Loading branch information
Stephane Leveugle
committed
Apr 21, 2024
1 parent
2d82377
commit 1c99056
Showing
8 changed files
with
713 additions
and
805 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
src/Matcher/Pattern/Expander/AbstractDateTimeComparison.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander; | ||
|
||
use Aeon\Calendar\Gregorian\DateTime; | ||
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander; | ||
use Coduo\ToString\StringConverter; | ||
|
||
abstract class AbstractDateTimeComparison implements PatternExpander | ||
{ | ||
use BacktraceBehavior; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public const NAME = 'after'; | ||
|
||
protected readonly DateTime $boundary; | ||
|
||
protected ?string $error = null; | ||
|
||
public function __construct(string $boundary) | ||
{ | ||
if (!\is_string($boundary)) { | ||
throw new \InvalidArgumentException(\sprintf('Before expander require "string", got "%s".', new StringConverter($boundary))); | ||
} | ||
|
||
try { | ||
$this->boundary = DateTime::fromString($boundary); | ||
} catch (\Exception $exception) { | ||
throw new \InvalidArgumentException(\sprintf('Boundary value "%s" is not a valid date, date time or time.', new StringConverter($boundary))); | ||
} | ||
} | ||
|
||
public static function is(string $name) : bool | ||
{ | ||
return static::getName() === $name; | ||
} | ||
|
||
public function match($value) : bool | ||
{ | ||
$this->backtrace->expanderEntrance(static::getName(), $value); | ||
|
||
if (!\is_string($value)) { | ||
$this->error = \sprintf('%s expander require "string", got "%s".', static::getName(), new StringConverter($value)); | ||
$this->backtrace->expanderFailed(static::getName(), $value, $this->error); | ||
|
||
return false; | ||
} | ||
|
||
return $this->compare($value); | ||
} | ||
|
||
public function getError() : ?string | ||
{ | ||
return $this->error; | ||
} | ||
|
||
abstract protected static function getName() : string; | ||
|
||
/** | ||
* @param string $value raw value | ||
* @param DateTime $datetime value converted in DateTime object | ||
*/ | ||
abstract protected function handleComparison(string $value, DateTime $datetime) : bool; | ||
|
||
private function compare(string $value) : bool | ||
{ | ||
try { | ||
$datetime = DateTime::fromString($value); | ||
} catch (\Exception $e) { | ||
$this->error = \sprintf('Value "%s" is not a valid date, date time or time.', new StringConverter($value)); | ||
$this->backtrace->expanderFailed(static::getName(), $value, $this->error); | ||
|
||
return false; | ||
} | ||
|
||
return $this->handleComparison($value, $datetime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.