Assert.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-2008 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 
00017     final class Assert extends StaticFactory
00018     {
00019         public static function isTrue($boolean, $message = null)
00020         {
00021             if ($boolean !== true)
00022                 throw new WrongArgumentException(
00023                     $message.', '.self::dumpArgument($boolean)
00024                 );
00025         }
00026         
00027         public static function isFalse($boolean, $message = null)
00028         {
00029             if ($boolean !== false)
00030                 throw new WrongArgumentException(
00031                     $message.', '.self::dumpArgument($boolean)
00032                 );
00033         }
00034         
00035         public static function isNotFalse($boolean, $message = null)
00036         {
00037             if ($boolean === false)
00038                 throw new WrongArgumentException(
00039                     $message.', '.self::dumpArgument($boolean)
00040                 );
00041         }
00042         
00043         public static function isNull($variable, $message = null)
00044         {
00045             if ($variable !== null)
00046                 throw new WrongArgumentException(
00047                     $message.', '.self::dumpArgument($variable)
00048                 );
00049         }
00050         
00051         public static function isEmpty($variable, $message = null)
00052         {
00053             if (!empty($variable))
00054                 throw new WrongArgumentException(
00055                     $message.', '.self::dumpArgument($variable)
00056                 );
00057         }
00058         
00059         public static function isNotEmpty($variable, $message = null)
00060         {
00061             if (empty($variable))
00062                 throw new WrongArgumentException(
00063                     $message.', '.self::dumpArgument($variable)
00064                 );
00065         }
00066         
00067         public static function isIndexExists($array, $key, $message = null)
00068         {
00069             Assert::isArray($array);
00070             
00071             if (!array_key_exists($key, $array))
00072                 throw new WrongArgumentException(
00073                     $message.', '.self::dumpArgument($key)
00074                 );
00075         }
00076         
00077         public static function isNotNull($variable, $message = null)
00078         {
00079             if ($variable === null)
00080                 throw new WrongArgumentException($message);
00081         }
00082         
00083         public static function isScalar($variable, $message = null)
00084         {
00085             if (!is_scalar($variable))
00086                 throw new WrongArgumentException(
00087                     $message.', '.self::dumpArgument($variable)
00088                 );
00089         }
00090         
00091         public static function isArray($variable, $message = null)
00092         {
00093             if (!is_array($variable))
00094                 throw new WrongArgumentException(
00095                     $message.', '.self::dumpArgument($variable)
00096                 );
00097         }
00098         
00099         public static function isNotEmptyArray(&$variable, $message = null)
00100         {
00101             self::isArray($variable, $message);
00102             
00103             if (!$variable)
00104                 throw new WrongArgumentException(
00105                     $message.', '.self::dumpArgument($variable)
00106                 );
00107         }
00108         
00109         public static function isInteger($variable, $message = null)
00110         {
00111             if (
00112                 !(
00113                     is_numeric($variable)
00114                     && $variable == (int) $variable
00115                 )
00116             )
00117                 throw new WrongArgumentException(
00118                     $message.', '.self::dumpArgument($variable)
00119                 );
00120         }
00121         
00122         public static function isPositiveInteger($variable, $message = null)
00123         {
00124             if (
00125                 !self::checkInteger($variable)
00126                 || $variable < 0
00127             )
00128                 throw new WrongArgumentException(
00129                     $message.', '.self::dumpArgument($variable)
00130                 );
00131         }
00132         
00133         public static function isFloat($variable, $message = null)
00134         {
00135             if (!self::checkFloat($variable))
00136                 throw new WrongArgumentException(
00137                     $message.', '.self::dumpArgument($variable)
00138                 );
00139         }
00140         
00141         public static function isString($variable, $message = null)
00142         {
00143             if (!is_string($variable))
00144                 throw new WrongArgumentException(
00145                     $message.', '.self::dumpArgument($variable)
00146                 );
00147         }
00148         
00149         public static function isBoolean($variable, $message = null)
00150         {
00151             if (!($variable === true || $variable === false))
00152                 throw new WrongArgumentException(
00153                     $message.', '.self::dumpArgument($variable)
00154                 );
00155         }
00156         
00157         public static function isTernaryBase($variable, $message = null)
00158         {
00159             if (
00160                 !(
00161                     ($variable === true)
00162                     || ($variable === false)
00163                     || ($variable === null)
00164                 )
00165             )
00166                 throw new WrongArgumentException(
00167                     $message.', '.self::dumpArgument($variable)
00168                 );
00169         }
00170         
00171         public static function brothers($first, $second, $message = null)
00172         {
00173             if (get_class($first) !== get_class($second))
00174                 throw new WrongArgumentException(
00175                     $message.', '.self::dumpOppositeArguments($first, $second)
00176                 );
00177         }
00178         
00179         public static function isEqual($first, $second, $message = null)
00180         {
00181             if ($first != $second)
00182                 throw new WrongArgumentException(
00183                     $message.', '.self::dumpOppositeArguments($first, $second)
00184                 );
00185         }
00186         
00187         public static function isNotEqual($first, $second, $message = null)
00188         {
00189             if ($first == $second)
00190                 throw new WrongArgumentException(
00191                     $message.', '.self::dumpOppositeArguments($first, $second)
00192                 );
00193         }
00194         
00195         public static function isSame($first, $second, $message = null)
00196         {
00197             if ($first !== $second)
00198                 throw new WrongArgumentException(
00199                     $message.', '.self::dumpOppositeArguments($first, $second)
00200                 );
00201         }
00202         
00203         public static function isNotSame($first, $second, $message = null)
00204         {
00205             if ($first === $second)
00206                 throw new WrongArgumentException(
00207                     $message.', '.self::dumpOppositeArguments($first, $second)
00208                 );
00209         }
00210         
00211         public static function isTypelessEqual($first, $second, $message = null)
00212         {
00213             if ($first != $second)
00214                 throw new WrongArgumentException(
00215                     $message.', '.self::dumpOppositeArguments($first, $second)
00216                 );
00217         }
00218         
00219         public static function isLesser($first, $second, $message = null)
00220         {
00221             if (!($first < $second))
00222                 throw new WrongArgumentException(
00223                     $message.', '.self::dumpOppositeArguments($first, $second)
00224                 );
00225         }
00226         
00227         public static function isGreater($first, $second, $message = null)
00228         {
00229             if (!($first > $second))
00230                 throw new WrongArgumentException(
00231                     $message.', '.self::dumpOppositeArguments($first, $second)
00232                 );
00233         }
00234         
00235         public static function isLesserOrEqual($first, $second, $message = null)
00236         {
00237             if (!($first <= $second))
00238                 throw new WrongArgumentException(
00239                     $message.', '.self::dumpOppositeArguments($first, $second)
00240                 );
00241         }
00242         
00243         public static function isGreaterOrEqual($first, $second, $message = null)
00244         {
00245             if (!($first >= $second))
00246                 throw new WrongArgumentException(
00247                     $message.', '.self::dumpOppositeArguments($first, $second)
00248                 );
00249         }
00250         
00251         public static function isInstance($first, $second, $message = null)
00252         {
00253             if (!ClassUtils::isInstanceOf($first, $second))
00254                 throw new WrongArgumentException(
00255                     $message.', '.self::dumpOppositeArguments($first, $second)
00256                 );
00257         }
00258         
00259         public static function classExists($className, $message = null)
00260         {
00261             if (!class_exists($className, true))
00262                 throw new WrongArgumentException(
00263                     $message.', class "'.$className.'" does not exists'
00264                 );
00265         }
00266         
00267         public static function methodExists($object, $method, $message = null)
00268         {
00269             if (!method_exists($object, $method))
00270                 throw new WrongArgumentException(
00271                     $message.', method "'.get_class($object).'::'.$method.'()" does not exists'
00272                 );
00273         }
00274 
00275         public static function isUnreachable($message = 'unreachable code reached')
00276         {
00277             throw new WrongArgumentException($message);
00278         }
00279         
00281 
00282         public static function checkInteger($value)
00283         {
00284             return (
00285                 is_numeric($value)
00286                 && ($value == (int) $value)
00287                 && (strlen($value) == strlen((int) $value))
00288             );
00289         }
00290         
00291         public static function checkFloat($value)
00292         {
00293             return (
00294                 is_numeric($value)
00295                 && ($value == (float) $value)
00296             );
00297         }
00298 
00299         public static function checkScalar($value)
00300         {
00301             return is_scalar($value);
00302         }
00303         
00304         public static function dumpArgument($argument)
00305         {
00306             return 'argument: ['.print_r($argument, true).']';
00307         }
00308         
00309         public static function dumpOppositeArguments($first, $second)
00310         {
00311             return
00312                 'arguments: ['.print_r($first, true).'] '
00313                 .'vs. ['.print_r($second, true).'] ';
00314         }
00316     }
00317 ?>