Debian Codegraph @

vendor/symfony/finder/Comparator/DateComparator.php

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