# LogicalChain.class.php

Go to the documentation of this file.

    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
    00017     final class LogicalChain extends SQLChain
    00018     {
    00022         public static function block($args, $logic)
    00023         {
    00024             Assert::isTrue(
    00025                 ($logic == BinaryExpression::EXPRESSION_AND)
    00026                 || ($logic == BinaryExpression::EXPRESSION_OR),
    00027
    00028                 "unknown logic '{$logic}'"
    00029             );
    00030
    00031             $logicalChain = new self;
    00032
    00033             foreach ($args as $arg) {
    00034                 if (
    00035                     !$arg instanceof LogicalObject
    00036                     && !$arg instanceof SelectQuery
    00037                 )
    00038                     throw new WrongArgumentException(
    00039                         'unsupported object type: '.get_class($arg)
    00040                     );
    00041
    00042                 $logicalChain->exp($arg, $logic);
    00043             }
    00044
    00045             return $logicalChain;
    00046         }
    00047
    00051         public function expAnd(LogicalObject $exp)
    00052         {
    00053             return $this->exp($exp, BinaryExpression::EXPRESSION_AND);
    00054         }
    00055
    00059         public function expOr(LogicalObject $exp)
    00060         {
    00061             return $this->exp($exp, BinaryExpression::EXPRESSION_OR);
    00062         }
    00063
    00064         public function toBoolean(Form $form)
    00065         {
    00066             $chain = &$this->chain;
    00067
    00068             $size = count($chain);
    00069
    00070             if (!$size)
    00071                 throw new WrongArgumentException(
    00072                     'empty chain can not be calculated'
    00073                 );
    00074             elseif ($size == 1)
    00075                 return $chain[0]->toBoolean($form);
    00076             else { // size > 1
    00077                 $out = $chain[0]->toBoolean($form);
    00078
    00079                 for ($i = 1; $i < $size; ++$i) {
    00080                     $out =
    00081                         self::calculateBoolean(
    00082                             $this->logic[$i],
    00083                             $out,
    00084                             $chain[$i]->toBoolean($form)
    00085                         );
    00086                 }
    00087
    00088                 return $out;
    00089             }
    00090
    00091             Assert::isUnreachable();
    00092         }
    00093
    00094         private static function calculateBoolean($logic, $left, $right)
    00095         {
    00096             switch ($logic) {
    00097                 case BinaryExpression::EXPRESSION_AND:
    00098                     return $left && $right;
    00099
    00100                 case BinaryExpression::EXPRESSION_OR:
    00101                     return $left || $right;
    00102
    00103                 default:
    00104                     throw new WrongArgumentException(
    00105                         "unknown logic - '{$logic}'"
    00106                     );
    00107             }
    00108
    00109             Assert::isUnreachable();
    00110         }
    00111     }
    00112 ?>