Class \Scrivo\ArgumentCheck

The ArgumentCheck class contains utility functions for type checking of arguments.

All public methods in the Scrivo library should implement type checking of function arguments, either by PHP type hinting for non-scalar types or runtime type checking of scalar types.

Defined in: ArgumentCheck.php.


Constant summary

Name Description
TYPE_BOOLEAN Constant to denote a boolean type.
TYPE_FLOAT Constant to denote a float type.
TYPE_INTEGER Constant to denote an integer type.
TYPE_STRING Constant to denote a string type.

Method summary

Attr. Type Name / Description
public static boolean

assert($arg, $type, $set)

Test if a variable or set of variables in an array comply to a certain (set of) type(s), and optionally test if the variable value(s) exist in a given set of values.

private static boolean

assertArg($arg, $type, $set)

Test if a variable complies to a certain (set of) type(s), and optionally test if it exists in a given set.

public static

assertArgs($arguments, $args, $minCount)

Check the argument count, types and values of all arguments passed to a method or function.

private static boolean

isType($arg, $type)

Simple test for scalar types.

 


Constants

TYPE_BOOLEAN

Constant to denote a boolean type.

Value: 2

TYPE_FLOAT

Constant to denote a float type.

Value: 3

TYPE_INTEGER

Constant to denote an integer type.

Value: 1

TYPE_STRING

Constant to denote a string type.

Value: 4


Methods

public static boolean assert(mixed $arg, mixed $type, array $set=null)

Test if a variable or set of variables in an array comply to a certain (set of) type(s), and optionally test if the variable value(s) exist in a given set of values.

function aFunc($arg) { // Test for boolean type \Scrivo\ArgumentCheck::assert( $arg, \Scrivo\ArgumentCheck::TYPE_BOOLEAN);

function aFunc($arg) { // Test for integer type and in the set (1,4,9) \Scrivo\ArgumentCheck::assert( $arg, \Scrivo\ArgumentCheck::TYPE_INTEGER, array(1,4,9));

function aFunc($arg) { // Test for integer or string type \Scrivo\ArgumentCheck::assert( $arg, array(\Scrivo\ArgumentCheck::TYPE_BOOLEAN, \Scrivo\ArgumentCheck::TYPE_STRING));

function aFunc(array $arg) { // Test for array of string type: note array in function arguments \Scrivo\ArgumentCheck::assert( $arg, \Scrivo\ArgumentCheck::TYPE_STRING));

Parameters:

Type Name Def. Description
mixed $arg

The variable to test.

mixed $type

The assumed type(s) of the variable, one or more (array) out of the \Scrivo\ArgumentsCheck::TYPE_* constants.

array $set null

Optional to test if the given argument exists in a specific set.

Returns:

boolean Test if a variable or set of variables in an array comply to a certain (set of) type(s), and optionally test if the variable value(s) exist in a given set of values.

private static boolean assertArg(mixed $arg, mixed $type, array $set=null)

Test if a variable complies to a certain (set of) type(s), and optionally test if it exists in a given set.

Parameters:

Type Name Def. Description
mixed $arg

The variable to test.

mixed $type

The assumed type(s) of the variable, one or more (array) out of the \Scrivo\ArgumentsCheck::TYPE_* constants.

array $set null

Optional to test if the given argument exists in a specific set.

Returns:

boolean Test if a variable complies to a certain (set of) type(s), and optionally test if it exists in a given set.

public static assertArgs(array $arguments, array $args, \Scrivo\number $minCount=-1)

Check the argument count, types and values of all arguments passed to a method or function.

See also ArgumentCheck::assert.

function aFunc(\Scrivo\String $aStr, $anInt=0) { \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( // String already tested by type hint null // Test for integer type and in the set (1,4,9) array(\Scrivo\ArgumentCheck::TYPE_INTEGER, array(1,4,9) ), // At least one argument needs to be given. 1);

Parameters:

Type Name Def. Description
array $arguments

Argument list (func_get_args).

array $args

An array with values that the arguments need to comply with. Use null to skip the test.

\Scrivo\number $minCount -1

If a variable length argument list was used, can use this to set the minimum number of arguments.

private static boolean isType(mixed $arg, int $type)

Simple test for scalar types.

Parameters:

Type Name Def. Description
mixed $arg

The variable to test.

int $type

The assumed type of the variable, one out of the \Scrivo\ArgumentsCheck::TYPE_* constants.

Returns:

boolean Simple test for scalar types.

Throws:

Exception Type Description
\Scrivo\SystemException If the $type argument is not one out of the \Scrivo\ArgumentsCheck::TYPE_* constants.

Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013