Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Finder\Comparator; Chris@0: Chris@0: /** Chris@0: * DateCompare compiles date comparisons. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class DateComparator extends Comparator Chris@0: { Chris@0: /** Chris@0: * @param string $test A comparison string Chris@0: * Chris@0: * @throws \InvalidArgumentException If the test is not understood Chris@0: */ Chris@0: public function __construct($test) Chris@0: { Chris@0: if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) { Chris@0: throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test)); Chris@0: } Chris@0: Chris@0: try { Chris@0: $date = new \DateTime($matches[2]); Chris@0: $target = $date->format('U'); Chris@0: } catch (\Exception $e) { Chris@0: throw new \InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2])); Chris@0: } Chris@0: Chris@0: $operator = isset($matches[1]) ? $matches[1] : '=='; Chris@0: if ('since' === $operator || 'after' === $operator) { Chris@0: $operator = '>'; Chris@0: } Chris@0: Chris@0: if ('until' === $operator || 'before' === $operator) { Chris@0: $operator = '<'; Chris@0: } Chris@0: Chris@0: $this->setOperator($operator); Chris@0: $this->setTarget($target); Chris@0: } Chris@0: }