YANA 4 PHP Framework
Docs for page toolbox.php
Common tools
This file contains a variety of tools that might be usefull to all applications, no matter wether the use the rest of the framework or not.
Name | Type | Description |
---|---|---|
$input | array | |
$case | int|bool | CASE_UPPER or CASE_LOWER |
This is a recursive implementation of the PHP function array_change_key_case(). It takes the same arguments: an array $input to work on and an optional argument $case. The argument $case can be one of two constants: CASE_LOWER and CASE_UPPER, where CASE_LOWER is the default.
As of version 2.8.8 this function was replaced by Hashtable::changeCase() and marked deprecated.
Name | Type | Description |
---|---|---|
$arguments | array | list of arguments |
$types | array | list of types, with the following values: IS_INT, IS_STRING, IS_FLOAT, IS_BOOL, IS_ARRAY, IS_SCALAR, IS_NUMERIC, IS_OBJECT, IS_RESOURCE, IS_NULL |
$name | string | optional name of calling function |
This function returns bool(true) on success, or a string containing an error message if it fails.
Example:
- function foo1 ($int, $number)
- {
- $chk === true || die($chk);
- // ...
- }
- // You may exclude an argument from the check by using IS_MIXED
- function foo2 ($int, $mixed, $bool)
- {
- $chk === true || die($chk);
- // ...
- }
- // You may have an optional argument by using IS_NULL
- function foo3 ($int, $bool = false)
- {
- $chk === true || die($chk);
- // ...
- }
- // When checking a single argument you may shorten the function call by
- // providing the second argument as an integer instead of an array.
- function foo4 ($string)
- {
- $chk === true || die($chk);
- // ...
- }
- // As an option you may include the name of the calling function in error messages
- function foo5 ($int, $bool = false)
- {
- $chk === true || die($chk);
- // ...
- }
Note that you may combine types by using the '|' operator, like: IS_INT | IS_FLOAT.
To conclude this, here is a brief "best-practice" example. This is a pattern I personally would suggest you to use.
- $types = array(
- IS_STRING, // argument 1
- IS_NULL|IS_STRING, // argument 2
- IS_NULL|IS_INT // argument 3
- );
- if ($chk !== true) {
- return false;
- }
Be warned that this function is meant to be used for situations where you can live with a certain overhead to profit from additional means of security. If a function can only be called from another function, which already has checked the input arguments, you may want to use assertions and type casting instead.
Name | Type | Description |
---|---|---|
$array | array | input array that should be cloned |
This function creates a deep-copy of the input $array and returns it.
"Deep-copy" means, it tries to clone objects registered in the array by calling the function "copy()", if the object has any, or by using the keyword "clone" as of PHP 5.
Note that this will not work correctly, if the object has neither the one nor the other.
Name | Type | Description |
---|---|---|
$dir | string | |
$filter | string | |
$switch | int | possible values YANA_GET_ALL, YANA_GET_DIRS, YANA_GET_FILES |
The argument $filter may contain multiple file extension, use a pipe '|' sign to seperate them. Example: "*.xml|*.html" will find all xml- and html-files
The argument $switch may be used to get only subdirectories (YANA_GET_DIRS), or only files (YANA_GET_FILES), or all contents (YANA_GET_ALL), which is the default.
Name | Type | Description |
---|---|---|
$A | array | |
$B | array |
This function is pretty much the same as the php function "array_merge_recursive" except for the way how duplicate keys are treated. Dupplicate keys get replaced in this implementation rather than being appended.
As of version 2.8.8 this function was replaced by Hashtable::merge() and marked deprecated.
Name | Type | Description |
---|---|---|
&$array | array | |
$needle | scalar |
If the array contains $needle, the key of $needle is returned. Otherwise this functions returns bool(false).
This function does something similar to PHP's in_array(), except, that it expects the input to be a numeric, unique, sorted array.
If the input is sorted, searching for a key is far more performant using this function than the original, especially for large arrays.
To be more technical: qSearchArray() performs the search on a sorted array in O(log(n)) running time, which is the same as searching for a key in a red-black tree. while a "normal", full scan of the array takes O(n) running time.
Example:
- // Search through a large, sorted, numeric array of strings.
- // E.g. a file where each line is representing a value.
- if ($i === false) {
- print "Value 'foo' not found!\n";
- } else {
- print "Found 'foo' in line $i.\n";
- }
- }
Name | Type | Description |
---|---|---|
$value | mixed | the input data |
$type | string | desired type, note that this should always be a scalar type |
$length | int | maximum length |
$escape | int | choose how special characters should be treated |
This function scrubbs your user input data shiny and clean.
It ensures: the data has a given type, maximum length, and syntax. E.g. if the data comes out of an input-field use this function with the argument $escape set to YANA_ESCAPE_LINEBREAK, to enforce the input does not have any unexpected line breaks.
Valid values for parameter $type:
Note: type "upload" will return the path to the uploaded file on success and an integer error constant on error. See the PHP manual for details on these codes.
Valid values for parameter $escape:
Interpretation of the $length parameter depends on the $type argument given.
For type float and integer, if the number of digits exceeds the maximum, the maximum number allowed will be returned instead.
For type float see the following examples:
$value=-3, $length=1 : return -3 $value=3.2, $length=1 : return 3 $value=3.4, $length=1 : return 3 $value=3.5, $length=1 : return 4 $value=3.6, $length=1 : return 4 $value=9.9, $length=1 : return 9 $value=10, $length=1 : return 9 $value=1.11, $length=2 : return 1.1 $value=11.11, $length=2 : return 11 $value=111.11, $length=2 : return 99 $value=0.115, $length=2 : return .12
If type is "image", the input value should be the index of the file in the $_FILES array. The function will return the name of the file or an integer, to identify the error encountered. See the PHP manual at php.net/docs "Features / Handling file uploads / Error Messages Explained" for detailed information about the returned error codes. In addition the function will return bool(false) if the uploaded file is not an image.
Documentation generated on Sun, 11 Mar 2007 15:02:55 +0100 by phpDocumentor 1.3.1