Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Finder] refactored size and date comparators
- Loading branch information
Showing
12 changed files
with
351 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Symfony\Components\Finder\Comparator; | ||
|
||
/* | ||
* This file is part of the Symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* Comparator. | ||
* | ||
* @package Symfony | ||
* @subpackage Components_Finder | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> PHP port | ||
*/ | ||
class Comparator | ||
{ | ||
protected $target; | ||
protected $operator; | ||
|
||
public function setTarget($target) | ||
{ | ||
$this->target = $target; | ||
} | ||
|
||
public function setOperator($operator) | ||
{ | ||
if (!$operator) { | ||
$operator = '=='; | ||
} | ||
|
||
if (!in_array($operator, array('>', '<', '>=', '<=', '=='))) { | ||
throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator)); | ||
} | ||
|
||
$this->operator = $operator; | ||
} | ||
|
||
/** | ||
* Tests against the target. | ||
* | ||
* @param mixed $test A test value | ||
*/ | ||
public function test($test) | ||
{ | ||
switch ($this->operator) { | ||
case '>': | ||
return $test > $this->target; | ||
case '>=': | ||
return $test >= $this->target; | ||
case '<': | ||
return $test < $this->target; | ||
case '<=': | ||
return $test <= $this->target; | ||
} | ||
|
||
return $test == $this->target; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Symfony/Components/Finder/Comparator/DateComparator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Symfony\Components\Finder\Comparator; | ||
|
||
/* | ||
* This file is part of the Symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* DateCompare compiles date comparisons. | ||
* | ||
* @package Symfony | ||
* @subpackage Components_Finder | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> PHP port | ||
*/ | ||
class DateComparator extends Comparator | ||
{ | ||
protected $target; | ||
protected $comparison; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $test A comparison string | ||
* | ||
* @throws \InvalidArgumentException If the test is not understood | ||
*/ | ||
public function __construct($test) | ||
{ | ||
if (!preg_match('#^\s*([<>=]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) { | ||
throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test)); | ||
} | ||
|
||
if (false === $target = @strtotime($matches[2])) { | ||
throw new \InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2])); | ||
} | ||
|
||
$operator = isset($matches[1]) ? $matches[1] : '=='; | ||
if ('since' === $operator || 'after' === $operator) | ||
{ | ||
$operator = '>'; | ||
} | ||
|
||
if ('until' === $operator || 'before' === $operator) | ||
{ | ||
$operator = '<'; | ||
} | ||
|
||
$this->setOperator($operator); | ||
$this->setTarget($target); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.