4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Comparator;
15 * DateCompare compiles date comparisons.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class DateComparator extends Comparator
22 * @param string $test A comparison string
24 * @throws \InvalidArgumentException If the test is not understood
26 public function __construct($test)
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));
33 $date = new \DateTime($matches[2]);
34 $target = $date->format('U');
35 } catch (\Exception $e) {
36 throw new \InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2]));
39 $operator = isset($matches[1]) ? $matches[1] : '==';
40 if ('since' === $operator || 'after' === $operator) {
44 if ('until' === $operator || 'before' === $operator) {
48 $this->setOperator($operator);
49 $this->setTarget($target);