EqualsCondition.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /*
  3. * $Id: EqualsCondition.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/tasks/system/condition/Condition.php';
  22. /**
  23. * A simple string comparator. Compares two strings for eqiality in a
  24. * binary safe manner. Implements the condition interface specification.
  25. *
  26. * @author Andreas Aderhold <andi@binarycloud.com>
  27. * @copyright © 2001,2002 THYRELL. All rights reserved
  28. * @version $Revision: 905 $ $Date: 2010-10-05 18:28:03 +0200 (Tue, 05 Oct 2010) $
  29. * @access public
  30. * @package phing.tasks.system.condition
  31. */
  32. class EqualsCondition implements Condition {
  33. private $arg1;
  34. private $arg2;
  35. private $trim = false;
  36. private $caseSensitive = true;
  37. public function setArg1($a1) {
  38. $this->arg1 = $a1;
  39. }
  40. public function setArg2($a2) {
  41. $this->arg2 = $a2;
  42. }
  43. /**
  44. * Should we want to trim the arguments before comparing them?
  45. * @param boolean $b
  46. */
  47. public function setTrim($b) {
  48. $this->trim = (boolean) $b;
  49. }
  50. /**
  51. * Should the comparison be case sensitive?
  52. * @param boolean $b
  53. */
  54. public function setCaseSensitive($b) {
  55. $this->caseSensitive = (boolean) $b;
  56. }
  57. public function evaluate() {
  58. if ($this->arg1 === null || $this->arg2 === null) {
  59. throw new BuildException("Both arg1 and arg2 are required in equals.");
  60. }
  61. if ($this->trim) {
  62. $this->arg1 = trim($this->arg1);
  63. $this->arg2 = trim($this->arg2);
  64. }
  65. //print("[comparison] Comparing '".$this->arg1."' and '".$this->arg2."'\n");
  66. return $this->caseSensitive ? $this->arg1 === $this->arg2 : strtolower($this->arg1) === strtolower($this->arg2);
  67. }
  68. }