LogicUtils.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-2007 by Konstantin V. Arkhipov                     *
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 LogicUtils extends StaticFactory
00016     {
00021         public static function getOpenRange(
00022             $left, $right, $min = null, $max = null
00023         )
00024         {
00025             Assert::isFalse(
00026                 ($min === null) && ($max === null),
00027                 'how can i build logic from emptyness?'
00028             );
00029             
00030             if ($min !== null)
00031                 $min = new DBValue($min);
00032             
00033             if ($max !== null)
00034                 $max = new DBValue($max);
00035             
00036             $chain = new LogicalChain();
00037             
00038             if ($min !== null && $max !== null) {
00039                 $chain->expOr(
00040                     Expression::orBlock(
00041                         Expression::andBlock(
00042                             Expression::notNull($left),
00043                             Expression::notNull($right),
00044                             Expression::expOr(
00045                                 Expression::between($min, $left, $right),
00046                                 Expression::between($left, $min, $max)
00047                             )
00048                         ),
00049                         Expression::andBlock(
00050                             Expression::isNull($left),
00051                             Expression::ltEq($min, $right)
00052                         ),
00053                         Expression::andBlock(
00054                             Expression::isNull($right),
00055                             Expression::ltEq($left, $max)
00056                         ),
00057                         Expression::andBlock(
00058                             Expression::isNull($left),
00059                             Expression::isNull($right)
00060                         )
00061                     )
00062                 );
00063             } elseif ($min !== null && $max === null) {
00064                 $chain->expOr(
00065                     Expression::orBlock(
00066                         Expression::andBlock(
00067                             Expression::notNull($right),
00068                             Expression::ltEq($min, $right)
00069                         ),
00070                         Expression::isNull($right)
00071                     )
00072                 );
00073             } elseif ($max !== null && $min === null) {
00074                 $chain->expOr(
00075                     Expression::orBlock(
00076                         Expression::andBlock(
00077                             Expression::notNull($left),
00078                             Expression::ltEq($left, $max)
00079                         ),
00080                         Expression::isNull($left)
00081                     )
00082                 );
00083             }
00084             
00085             return $chain;
00086         }
00087         
00088         
00093         public static function getOpenPoint(
00094             $left, $right, $point
00095         )
00096         {
00097             Assert::isFalse(
00098                 ($point === null),
00099                 'how can i build logic from emptyness?'
00100             );
00101             
00102             $point = new DBValue($point);
00103             
00104             $chain = new LogicalChain();
00105             
00106             $chain->expOr(
00107                 Expression::orBlock(
00108                     Expression::andBlock(
00109                         Expression::notNull($left),
00110                         Expression::notNull($right),
00111                         Expression::between($point, $left, $right)
00112                     ),
00113                     Expression::andBlock(
00114                         Expression::isNull($left),
00115                         Expression::ltEq($point, $right)
00116                     ),
00117                     Expression::andBlock(
00118                         Expression::isNull($right),
00119                         Expression::ltEq($left, $point)
00120                     ),
00121                     Expression::andBlock(
00122                         Expression::isNull($left),
00123                         Expression::isNull($right)
00124                     )
00125                 )
00126             );
00127             
00128             return $chain;
00129         }
00130     }
00131 ?>