BinaryExpression.class.php

Go to the documentation of this file.
00001 <?php
00002 /****************************************************************************
00003  *   Copyright (C) 2004-2008 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 
00015     final class BinaryExpression implements LogicalObject, MappableObject
00016     {
00017         const EQUALS            = '=';
00018         const NOT_EQUALS        = '!=';
00019         
00020         const EXPRESSION_AND    = 'AND';
00021         const EXPRESSION_OR     = 'OR';
00022 
00023         const GREATER_THAN      = '>';
00024         const GREATER_OR_EQUALS = '>=';
00025 
00026         const LOWER_THAN        = '<';
00027         const LOWER_OR_EQUALS   = '<=';
00028 
00029         const LIKE              = 'LIKE';
00030         const NOT_LIKE          = 'NOT LIKE';
00031         const ILIKE             = 'ILIKE';
00032         const NOT_ILIKE         = 'NOT ILIKE';
00033 
00034         const SIMILAR_TO        = 'SIMILAR TO';
00035         const NOT_SIMILAR_TO    = 'NOT SIMILAR TO';
00036         
00037         const ADD               = '+';
00038         const SUBSTRACT         = '-';
00039         const MULTIPLY          = '*';
00040         const DIVIDE            = '/';
00041         const MOD               = '%';
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 getLeft()
00055         {
00056             return $this->left;
00057         }
00058         
00059         public function getRight()
00060         {
00061             return $this->right;
00062         }
00063         
00064         public function getLogic()
00065         {
00066             return $this->logic;
00067         }
00068         
00069         public function toDialectString(Dialect $dialect)
00070         {
00071             return
00072                 '('
00073                 .$dialect->toFieldString($this->left)
00074                 ." {$this->logic} "
00075                 .$dialect->toValueString($this->right)
00076                 .')';
00077         }
00078         
00082         public function toMapped(ProtoDAO $dao, JoinCapableQuery $query)
00083         {
00084             return new self(
00085                 $dao->guessAtom($this->left, $query),
00086                 $dao->guessAtom($this->right, $query),
00087                 $this->logic
00088             );
00089         }
00090         
00091         public function toBoolean(Form $form)
00092         {
00093             $left   = $form->toFormValue($this->left);
00094             $right  = $form->toFormValue($this->right);
00095             
00096             $both =
00097                 (null !== $left)
00098                 && (null !== $right);
00099                 
00100             switch ($this->logic) {
00101                 case self::EQUALS:
00102                     return $both && ($left == $right);
00103 
00104                 case self::NOT_EQUALS:
00105                     return $both && ($left != $right);
00106 
00107                 case self::GREATER_THAN:
00108                     return $both && ($left > $right);
00109 
00110                 case self::GREATER_OR_EQUALS:
00111                     return $both && ($left >= $right);
00112 
00113                 case self::LOWER_THAN:
00114                     return $both && ($left < $right);
00115 
00116                 case self::LOWER_OR_EQUALS:
00117                     return $both && ($left <= $right);
00118 
00119                 case self::EXPRESSION_AND:
00120                     return $both && ($left && $right);
00121                 
00122                 case self::EXPRESSION_OR:
00123                     return $both && ($left || $right);
00124                 
00125                 case self::ADD:
00126                     return $both && ($left + $right);
00127                 
00128                 case self::SUBSTRACT:
00129                     return $both && ($left - $right);
00130                 
00131                 case self::MULTIPLY:
00132                     return $both && ($left * $right);
00133                 
00134                 case self::DIVIDE:
00135                     return $both && $right && ($left / $right);
00136                     
00137                 case self::MOD:
00138                     return $both && $right && ($left % $right);
00139                 
00140                 default:
00141                     throw new UnsupportedMethodException(
00142                         "'{$this->logic}' doesn't supported yet"
00143                     );
00144             }
00145         }
00146     }
00147 ?>