00001 <?php 00002 /**************************************************************************** 00003 * Copyright (C) 2004-2007 by Konstantin V. Arkhipov, Anton E. Lebedevich * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU Lesser General Public License as * 00007 * published by the Free Software Foundation; either version 3 of the * 00008 * License, or (at your option) any later version. * 00009 * * 00010 ****************************************************************************/ 00011 /* $Id: BinaryExpression.class.php 4687 2007-12-09 18:57:18Z voxus $ */ 00012 00016 final class BinaryExpression implements LogicalObject, MappableObject 00017 { 00018 const EQUALS = '='; 00019 const NOT_EQUALS = '!='; 00020 00021 const EXPRESSION_AND = 'AND'; 00022 const EXPRESSION_OR = 'OR'; 00023 00024 const GREATER_THAN = '>'; 00025 const GREATER_OR_EQUALS = '>='; 00026 00027 const LOWER_THAN = '<'; 00028 const LOWER_OR_EQUALS = '<='; 00029 00030 const LIKE = 'LIKE'; 00031 const NOT_LIKE = 'NOT LIKE'; 00032 const ILIKE = 'ILIKE'; 00033 const NOT_ILIKE = 'NOT ILIKE'; 00034 00035 const SIMILAR_TO = 'SIMILAR TO'; 00036 const NOT_SIMILAR_TO = 'NOT SIMILAR TO'; 00037 00038 const ADD = '+'; 00039 const SUBSTRACT = '-'; 00040 const MULTIPLY = '*'; 00041 const DIVIDE = '/'; 00042 00043 private $left = null; 00044 private $right = null; 00045 private $logic = null; 00046 00047 public function __construct($left, $right, $logic) 00048 { 00049 $this->left = $left; 00050 $this->right = $right; 00051 $this->logic = $logic; 00052 } 00053 00054 public function toDialectString(Dialect $dialect) 00055 { 00056 return 00057 '(' 00058 .$dialect->toFieldString($this->left) 00059 ." {$this->logic} " 00060 .$dialect->toValueString($this->right) 00061 .')'; 00062 } 00063 00067 public function toMapped(StorableDAO $dao, JoinCapableQuery $query) 00068 { 00069 return new self( 00070 $dao->guessAtom($this->left, $query), 00071 $dao->guessAtom($this->right, $query), 00072 $this->logic 00073 ); 00074 } 00075 00076 public function toBoolean(Form $form) 00077 { 00078 $left = $form->toFormValue($this->left); 00079 $right = $form->toFormValue($this->right); 00080 00081 $both = 00082 (null !== $left) 00083 && (null !== $right); 00084 00085 switch ($this->logic) { 00086 case self::EQUALS: 00087 return $both && ($left == $right); 00088 00089 case self::NOT_EQUALS: 00090 return $both && ($left != $right); 00091 00092 case self::GREATER_THAN: 00093 return $both && ($left > $right); 00094 00095 case self::GREATER_OR_EQUALS: 00096 return $both && ($left >= $right); 00097 00098 case self::LOWER_THAN: 00099 return $both && ($left < $right); 00100 00101 case self::LOWER_OR_EQUALS: 00102 return $both && ($left <= $right); 00103 00104 case self::EXPRESSION_AND: 00105 return $both && ($left && $right); 00106 00107 case self::EXPRESSION_OR: 00108 return $both && ($left || $right); 00109 00110 default: 00111 throw new UnsupportedMethodException( 00112 "'{$this->logic}' doesn't supported yet" 00113 ); 00114 } 00115 } 00116 } 00117 ?>