| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace WindPressDeps\Symfony\Component\Finder\Comparator; |
| 12 |
|
| 13 |
/** |
| 14 |
* DateCompare compiles date comparisons. |
| 15 |
* |
| 16 |
* @author Fabien Potencier <fabien@symfony.com> |
| 17 |
*/ |
| 18 |
class DateComparator extends Comparator |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @param string $test A comparison string |
| 22 |
* |
| 23 |
* @throws \InvalidArgumentException If the test is not understood |
| 24 |
*/ |
| 25 |
public function __construct(string $test) |
| 26 |
{ |
| 27 |
if (!\preg_match('#^\\s*(==|!=|[<>]=?|after|since|before|until)?\\s*(.+?)\\s*$#i', $test, $matches)) { |
| 28 |
throw new \InvalidArgumentException(\sprintf('Don\'t understand "%s" as a date test.', $test)); |
| 29 |
} |
| 30 |
try { |
| 31 |
$date = new \DateTime($matches[2]); |
| 32 |
$target = $date->format('U'); |
| 33 |
} catch (\Exception $e) { |
| 34 |
throw new \InvalidArgumentException(\sprintf('"%s" is not a valid date.', $matches[2])); |
| 35 |
} |
| 36 |
$operator = $matches[1] ?? '=='; |
| 37 |
if ('since' === $operator || 'after' === $operator) { |
| 38 |
$operator = '>'; |
| 39 |
} |
| 40 |
if ('until' === $operator || 'before' === $operator) { |
| 41 |
$operator = '<'; |
| 42 |
} |
| 43 |
parent::__construct($target, $operator); |
| 44 |
} |
| 45 |
} |
| 46 |
|