Orion PHP  0.11.12
The PHP5.3 framework
tools.php
Go to the documentation of this file.
00001 <?php
00002 
00003 namespace Orion\Core;
00004 
00005 
00006 /**
00007  * \Orion\Core\Tools
00008  * 
00009  * Orion tools class.
00010  *
00011  * Various function helpers used internally
00012  *
00013  * @author Thibaut Despoulain
00014  * @license BSD 4-clauses
00015  * @version 0.11.12
00016  *
00017  * @static
00018  */
00019 class Tools
00020 {
00021 
00022     /**
00023      * Regex styntax characters (escaped version)
00024      * @var array
00025      */
00026     private static $_REGEX_ESCAPED = array( '\-', '\#', '\!', '\^', '\$', '\(', '\)', '\[', '\]', '\{', '\}', '\+', '\*', '\.', '\|' );
00027 
00028     /**
00029      * Regex styntax characters (unescaped version)
00030      * @var array
00031      */
00032     private static $_REGEX_UNESCAPED = array( '-', '#', '!', '^', '$', '(', ')', '[', ']', '{', '}', '+', '*', '.', '|' );
00033 
00034     /**
00035      * Easy regex wildcards characters
00036      * @var array
00037      */
00038     private static $_REGEX_WILDCARDS = array( '@', '?', '*' );
00039 
00040     /**
00041      * Easy regex wildcards saves (used to prevent regex escape from escaping the easyregex syntax)
00042      * @var array
00043      */
00044     private static $_REGEX_WSAVE = array( '::number::', '::word::', '::any::' );
00045 
00046     /**
00047      * Easy regex wildacards traduction in PCRE
00048      * @var array
00049      */
00050     private static $_REGEX_WCREPLACE = array( '(\d+)', '([a-zA-Z0-9_-]+)', '(.*?)' );
00051 
00052     /**
00053      * SQL wildards unescaped
00054      * @var array
00055      */
00056     private static $_SQL_UNESCAPED = array( '%', '_' );
00057 
00058     /**
00059      * SQL wildards escaped
00060      * @var array
00061      */
00062     private static $_SQL_ESCAPED = array( '\%', '\_' );
00063 
00064     /**
00065      * <p>Call a class method with $args as arguments.
00066      * This is a more effective version of call_user_func_array() for 5 or less arguments.</p>
00067      * <p>If $args is of size 6 or more, call_user_func_array is called instead</p>
00068      * @param mixed $class The class instance
00069      * @param string $method The method name
00070      * @param array<mixed> $args An array of arguments
00071      */
00072     public static function callClassMethod( &$class, $method, $args=null )
00073     {
00074         //echo 'Calling... ['.count($args).']'.$method;
00075         switch ( count( $args ) )
00076         {
00077             case 0: $class->{$method}();
00078                 break;
00079             case 1: $class->{$method}( $args[ 0 ] );
00080                 break;
00081             case 2: $class->{$method}( $args[ 0 ], $args[ 1 ] );
00082                 break;
00083             case 3: $class->{$method}( $args[ 0 ], $args[ 1 ], $args[ 2 ] );
00084                 break;
00085             case 4: $class->{$method}( $args[ 0 ], $args[ 1 ], $args[ 2 ], $args[ 3 ] );
00086                 break;
00087             case 5: $class->{$method}( $args[ 0 ], $args[ 1 ], $args[ 2 ], $args[ 3 ], $args[ 4 ] );
00088                 break;
00089             default: call_user_func_array( array( $class, $method ), $args );
00090                 break;
00091         }
00092     }
00093 
00094     /**
00095      * Escape special regex chars.
00096      * In fact, this function is only usefull with an easy-regex instance or in very particular cases.
00097      * @param string $regex Regex to escape (without starting and ending delimiters and special chars that must remain special chars) 
00098      * @return string Escaped regex
00099      */
00100     public static function escapeRegex( $regex )
00101     {
00102         return str_replace( self::$_REGEX_UNESCAPED, self::$_REGEX_ESCAPED, str_replace( self::$_REGEX_ESCAPED, self::$_REGEX_UNESCAPED, $regex ) );
00103     }
00104 
00105     /**
00106      * Escapes sql wildcards (% and _) (prevents some SQL injection methods)
00107      * @param string $string SQL string
00108      * @return escaped string
00109      */
00110     public static function escapeSql( $string )
00111     {
00112         return str_replace( self::$_SQL_UNESCAPED, self::$_SQL_ESCAPED, str_replace( self::$_SQL_ESCAPED, self::$_SQL_UNESCAPED, $string ) );
00113     }
00114 
00115     /**
00116      * Extract sub array from base array, keeping only keys starting with $pattern
00117      * @param array<string,mixed> $array
00118      * @param string $pattern
00119      * @return array<key, mixed>
00120      */
00121     public static function extractArrayKeysStartingWith( $array, $pattern )
00122     {
00123         $out = array( );
00124 
00125         foreach ( $array as $key => $value )
00126         {
00127             if ( Tools::startWith( $key, $pattern ) )
00128                 $out[ $key ] = $value;
00129         }
00130 
00131         return $out;
00132     }
00133 
00134     /**
00135      * Get an array of files contained in a directory (recursively)
00136      * @param string $directory The directory to scan recursively.
00137      * @return string[] An array of file paths
00138      */
00139     public static function getFiles( $directory )
00140     {
00141         if ( substr( $directory, -1 ) == DS )
00142         {
00143             $directory = substr( $directory, 0, -1 );
00144         }
00145         
00146         if( !file_exists( $directory ) || !is_dir( $directory ) )
00147             throw new Exception( 'Directory ['.Security::preventInjection ( $directory ).'] does not exist, unable to get files.' );
00148         
00149         $base = $directory;
00150         $directoryHandle = opendir( $directory );
00151         
00152         $arr = array();
00153 
00154         while ( $contents = readdir( $directoryHandle ) )
00155         {
00156             if ( $contents != '.' && $contents != '..' )
00157             {
00158                 $path = $directory . DS . $contents;
00159 
00160                 if ( is_dir( $path ) )
00161                 {
00162                     $files = self::getFiles( $base.DS.$contents );
00163                     foreach($files as $file)
00164                         $arr[] = $file;
00165                 }
00166                 else
00167                 {
00168                     $arr[] = $base.DS.$contents;
00169                 }
00170             }
00171         }
00172 
00173         closedir( $directoryHandle );
00174         return $arr;
00175     }
00176     
00177     /**
00178      * preg_match shortcut.
00179      *
00180      * @param String Input to test
00181      * @param String Regex pattern without start/end tags (ex: "[a-zA-Z]+")
00182      *
00183      * @return TRUE if $string matches $pattern, FALSE otherwise.
00184      */
00185     public static function match( $string, $pattern, $modifiers='' )
00186     {
00187         return preg_match( '#^' . $pattern . '$#' . $modifiers, $string );
00188     }
00189 
00190     /**
00191      * Concats an array with $trail after each item (Useful for path and file array to string)
00192      * @param string $trail
00193      * @param array $array
00194      * @param boolean $tolower Should each item be lower case'd ?
00195      * @return string
00196      */
00197     public static function concatWithTrail( $trail, $array, $tolower=false )
00198     {
00199         if ( $array == null || empty( $array ) )
00200             return '';
00201 
00202         $out = '';
00203         foreach ( $array as $item )
00204         {
00205             $out .= ($tolower ? strtolower( $item ) : $item) . $trail;
00206         }
00207         return $out;
00208     }
00209 
00210     /**
00211      * Removes one or more characters from a string.
00212      * @param String|String[] $needle
00213      * @param String $string
00214      * @return String
00215      */
00216     public static function removeString( $needle, $string )
00217     {
00218         return str_replace( $needle, '', $string );
00219     }
00220 
00221     /**
00222      * Test if provided string starts with provided expression.
00223      *
00224      * @param String Input to test
00225      * @param String Starting expression
00226      *
00227      * @return TRUE if $string starts with $start, FALSE otherwise.
00228      */
00229     public static function startWith( $string, $start )
00230     {
00231         return (substr( $string, 0, strlen( $start ) ) == $start);
00232     }
00233 
00234     /**
00235      * Translates an Easy-regex into a valid PCRE regex.
00236      * @param string $regex PCRE or Easy-regex.
00237      * @return string PCRE-valid regex
00238      */
00239     public static function translateRegex( $regex )
00240     {
00241         if ( substr( $regex, 0, 1 ) == '#' )
00242             return $regex;
00243         else
00244             return '#' . str_replace( self::$_REGEX_WILDCARDS, self::$_REGEX_WCREPLACE, str_replace( self::$_REGEX_WSAVE, self::$_REGEX_WILDCARDS, str_replace( self::$_REGEX_ESCAPED, self::$_REGEX_UNESCAPED, str_replace( self::$_REGEX_UNESCAPED, self::$_REGEX_ESCAPED, str_replace( self::$_REGEX_WILDCARDS, self::$_REGEX_WSAVE, $regex ) ) ) ) ) . '$#';
00245     }
00246 
00247 }
00248 
00249 ?>