Snippet
<?php
/*
Copyright (C) 2007 Josh Erickson <josh @ josherickson.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Version 0.1:
* First version.
*
* Version 0.2:
* -Fixed issue with specifying $type as 'any' which would result in a "Unknown Error".
*/
/**
* A class to provide rudimentary strongly typed arguments for functions and class methods.
*/
class strict_args {
/**
* This holds the function and arguments for the functions we're working with.
*
* @static
* @name $funcs
* @access private
*/
/**
* This holds the last error.
*
* @static
* @name $lastError
* @access private
*/
private static $lastError = null;
/**
* Add an argument you wish to have parsed.
*
* Only supplied arguments will be returned after parsing is done. False on error. Check strict_args::lastError() for details.
*
* @static
* @name add
* $param string $function The name of the function this argument will apply to.
* $param string|int $argument The argument name that will be used.
* $param string $type The type of variable it is suppose to be. 'any' is default. See http://us.php.net/manual/en/language.types.php for valid types.
* $param mixed $default The default value of the argument if none is given. Default value is NULL.
* $param bool $require Whether or not the argument is require. If false, default value is given. Default value is FALSE.
* $param bool $strict Whether or not the supplied argument is suppose to be of a specific type or not. Default value is FALSE.
* @return bool Always returns true.
* @access public
*/
public static function add ($function, $arg, $type= 'any', $default= null, $require= false, $strict= false) {
//$hash = md5($name,$type,$default,$require,$strict);
//self::$funcs[$hash] = array($name,$type,$default,$require,$strict);
self:: $funcs[$function][$arg] = array($type, $default, $require, $strict);
return true;
}
/**
* Get the last error, if there be one
*
* Returns the last error if there is one or false if there are none.
*
* @static
* @return mixed
*/
public static function lastError () {
if(self::$lastError===null) {
return false;
} else {
return self::$lastError;
}
return false;
}
/**
* Checks whether or not the functions supplied arguments are valid or not based on provided criteria.
*
* <p>Returns an array of values if they all match up to supplied parameters for them. However if
* $onlyReturnBool is set to TRUE, it will only return a boolean as to whether or the check
* succeeds.</p>
* <p>The $arguments parameter must contain keys that align to the argument names provided to
* strict_args::add().</p>
*
* @static
* @param string $function The name of the function that will be checked.
* @param array $arguments The arguments that are to be checked.
* @param bool $onlyReturnBool If set to true, will only return a boolean value to whether or not it succeeds. Default is false;
* @return mixed
*/
public static function check ($function, $arguments, $onlyReturnBool = false) {
if(! isset(self:: $funcs[$function])) {
self::$lastError = "The function '{$function}' is never supplied to strict_args.";
return false;
}
foreach(self::$funcs[$function] AS $k => $v) {
//Do we have corresponding values?
//Check if arg exists in provided, if not, is it required?
$val = null;
if(! isset($arguments[$k]) AND self:: $funcs[$function][$k][2]== true) {
self::$lastError = "Argument '{$k}' is required but not given.";
return false;
} elseif(! isset($arguments[$k]) AND self:: $funcs[$function][$k][2]== false) {
$val = self::$funcs[$function][$k][1];
} else {
$val = $arguments[$k];
}
$strict = self::$funcs[$function][$k][3];
if($strict == true AND self::$funcs[$function][$k][0] != 'any') {
switch(self::$funcs[$function][$k][0]) {
case 'array':
if(! is_array($val)) { self:: $lastError = "Argument '{$k}' is required to be an array."; return false; }
break;
case 'bool':
if(! is_bool($val)) { self:: $lastError = "Argument '{$k}' is required to be a boolean."; return false; }
break;
case 'float':
if(! is_float($val)) { self:: $lastError = "Argument '{$k}' is required to be a float."; return false; }
break;
case 'int':
if(! is_int($val)) { self:: $lastError = "Argument '{$k}' is required to be an integer."; return false; }
break;
case 'null':
if(! is_null($val)) { self:: $lastError = "Argument '{$k}' is required to be null."; return false; }
break;
case 'numeric':
if(!is_muneric($val)) { self::$lastError = "Argument '{$k}' is required to be numeric."; return false;}
break;
case 'object':
if(! is_object($val)) { self:: $lastError = "Argument '{$k}' is required to be an object."; return false; }
break;
case 'resource':
if(! is_resource($val)) { self:: $lastError = "Argument '{$k}' is required to be a resource."; return false; }
break;
case 'scalar':
if(! is_scalar($val)) { self:: $lastError = "Argument '{$k}' is required to be scalar."; return false; }
break;
case 'string':
if(! is_string($val)) { self:: $lastError = "Argument '{$k}' is required to be a string."; return false; }
break;
default:
self::$lastError = "Unknown Error";
return false;
}
}
$rtn[$k] = $val;
}
return ($onlyReturnBool==true)?(bool)$rtn:$rtn;
}
}
//Typical function definition.
function test1($a,$b,$c=false) {
/*
$a should be an integer.
$b and $c should be a boolean.
*/
strict_args::add(__FUNCTION__,0,'int');
strict_args::add(__FUNCTION__,1,'bool',null,true,true);
strict_args::add(__FUNCTION__,2,'string',null,true,true);
$parsed_args = strict_args::check(__FUNCTION__,$args);
if($parsed_args == false) {
echo strict_args:: lastError();
} else {
echo "Arguments have passed check.";
}
}
//Overloading a function
function test2() {
/*
Try #1. Same as test1()
$a should be an integer.
$b and $c should be a boolean.
*/
strict_args::add(__FUNCTION__,0,'int');
strict_args::add(__FUNCTION__,1,'bool',null,true,true);
strict_args::add(__FUNCTION__,2,'string',null,true,true);
if(strict_args::check(__FUNCTION__,$args,true)) {
//We could just call test1() right here! :O
} else {
echo "try #1 did not passed.";
}
/*
Try #2. slightly modified.
$a should be an integer.
$b should be a boolean.
$c should be a string.
*/
strict_args::add(__FUNCTION__,0,'int');
strict_args::add(__FUNCTION__,1,'bool',null,true,true);
strict_args::add(__FUNCTION__,2,'string',null,'lol',true);
if(strict_args::check(__FUNCTION__,$args,true)) {
} else {
echo "try #2 did not passed.";
}
/*
Try #3. ...And yet a little more slightly modified.
$a should be an integer.
$b should be a boolean.
$c should be a string.
*/
strict_args::add(__FUNCTION__,0,'int');
strict_args::add(__FUNCTION__,1,'bool',null,true,true);
strict_args::add(__FUNCTION__,2,'string','lol',false,false);
if(strict_args::check(__FUNCTION__,$args,true)) {
} else {
echo "try #3 did not passed.";
}
}
test1(1,false,true);
test2(1,false);
?>
Copy & Paste
|