DateComparator.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Symfony\Component\Finder\Comparator;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * DateCompare compiles date comparisons.
  13. *
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com> PHP port
  15. */
  16. class DateComparator extends Comparator
  17. {
  18. protected $target;
  19. protected $comparison;
  20. /**
  21. * Constructor.
  22. *
  23. * @param string $test A comparison string
  24. *
  25. * @throws \InvalidArgumentException If the test is not understood
  26. */
  27. public function __construct($test)
  28. {
  29. if (!preg_match('#^\s*([<>=]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {
  30. throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test));
  31. }
  32. if (false === $target = @strtotime($matches[2])) {
  33. throw new \InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2]));
  34. }
  35. $operator = isset($matches[1]) ? $matches[1] : '==';
  36. if ('since' === $operator || 'after' === $operator)
  37. {
  38. $operator = '>';
  39. }
  40. if ('until' === $operator || 'before' === $operator)
  41. {
  42. $operator = '<';
  43. }
  44. $this->setOperator($operator);
  45. $this->setTarget($target);
  46. }
  47. }