# InExpression.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /****************************************************************************
    00003  *   Copyright (C) 2004-2009 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
    00017     final class InExpression implements LogicalObject, MappableObject
    00018     {
    00019         const IN        = 'IN';
    00020         const NOT_IN    = 'NOT IN';
    00021
    00022         private $left   = null;
    00023         private $right  = null;
    00024         private $logic  = null;
    00025
    00026         public function __construct($left, $right, $logic)
    00027         {
    00028             Assert::isTrue(
    00029                 ($right instanceof Query)
    00030                 || ($right instanceof Criteria)
    00031                 || ($right instanceof MappableObject)
    00032                 || is_array($right)
    00033             );
    00034
    00035             Assert::isTrue(
    00036                 ($logic == self::IN)
    00037                 || ($logic == self::NOT_IN)
    00038             );
    00039
    00040             $this->left     = $left;
    00041             $this->right    = $right;
    00042             $this->logic    = $logic;
    00043         }
    00044
    00048         public function toMapped(ProtoDAO $dao, JoinCapableQuery $query)
    00049         {
    00050             if (is_array($this->right)) {
    00051                 $right = array();
    00052                 foreach ($this->right as $atom) {
    00053                     $right[] = $dao->guessAtom($atom, $query);
    00054                 }
    00055             } elseif ($this->right instanceof MappableObject)
    00056                 $right = $this->right->toMapped($dao, $query);
    00057             else
    00058                 $right = $this->right; // untransformable
    00059
    00060             return new self(
    00061                 $dao->guessAtom($this->left, $query),
    00062                 $right,
    00063                 $this->logic
    00064             );
    00065         }
    00066
    00067         public function toDialectString(Dialect $dialect)
    00068         {
    00069             $string =
    00070                 '('
    00071                 .$dialect->toFieldString($this->left)
    00072                 .' '.$this->logic
    00073                 .' ';
    00074
    00075             $right = $this->right;
    00076
    00077             if ($right instanceof DialectString) {
    00078
    00079                 $string .= '('.$right->toDialectString($dialect).')';
    00080
    00081             } elseif (is_array($right)) {
    00082
    00083                 $string .= SQLArray::create($right)->
    00084                     toDialectString($dialect);
    00085
    00086             } else
    00087                 throw new WrongArgumentException(
    00088                     'sql select or array accepted by '.$this->logic
    00089                 );
    00090
    00091             $string .= ')';
    00092
    00093             return $string;
    00094         }
    00095
    00096         public function toBoolean(Form $form)
    00097         {
    00098             $left   = $form->toFormValue($this->left);
    00099             $right  = $this->right;
    00100
    00101             $both =
    00102                 (null !== $left)
    00103                 && (null !== $right);
    00104
    00105             switch ($this->logic) {
    00106
    00107                 case self::IN:
    00108                     return $both && (in_array($left, $right));
    00109
    00110                 case self::NOT_IN:
    00111                     return $both && (!in_array($left, $right));
    00112
    00113                 default:
    00114
    00115                     throw new UnsupportedMethodException(
    00116                         "'{$this->logic}' doesn't supported"
    00117                     );
    00118             }
    00119         }
    00120     }
    00121 ?>