-
Notifications
You must be signed in to change notification settings - Fork 0
Typecasting
This class contains robust type-conversion functionality. These methods are less performant than native operations like $foo = (int) $foo, but are more intelligent and less likely to explode when passed wacky values.
Use:
\blobfolio\common\cast (by value)
\blobfolio\common\ref\cast (by reference)
This function attempts to classify the indexing method of an array.
| Type | Description | Notes |
|---|---|---|
array |
Array. | By reference. |
-
"sequential"if the keys are numeric and in sequence beginning with0 -
"indexed"if the keys are otherwise numeric -
"associative"if the keys are mixed or non-numeric -
FALSEif the array is empty
$arr1 = array(
'apples',
'bananas',
'carrots'
);
echo blobfolio\common\cast::array_type($arr1); // sequentialTypecast to array. This is like (array) hinting, but will set non-arrayable objects as empty arrays.
- By Value
- By Reference
The following are available under PHP7+:
- ::array()
| Type | Description |
|---|---|
mixed |
Value. |
Returns an array if passed by value, otherwise TRUE if by reference.
// By value.
$foo = \blobfolio\common\cast::to_array('apples'); // ['apples']
$foo = \blobfolio\common\cast::to_array(null); // []
$foo = \blobfolio\common\cast::to_array(array('apples')); // ['apples']
// By reference.
\blobfolio\common\ref\cast::to_array($foo);Typecast to boolean. This is like (bool) hinting, but will also properly interpret things like "true", "on", etc.
- By Value
- By Reference
The following are available under PHP7+:
- ::bool()
- ::boolean()
| Type | Description | Notes | Default |
|---|---|---|---|
mixed, array
|
Value. | If an array is passed, each value will be processed recursively. | |
bool |
Flatten. |
TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. |
FALSE |
Returns TRUE or FALSE.
// By value.
$foo = \blobfolio\common\cast::to_bool(true); // TRUE
$foo = \blobfolio\common\cast::to_bool(1); // TRUE
$foo = \blobfolio\common\cast::to_bool("true"); // TRUE
// By reference.
\blobfolio\common\ref\cast::to_bool($foo);Typecast to float. This is like (float) hinting, but will strip out non-numeric data and attempt to convert values like percents and US cents.
- By Value
- By Reference
The following are available under PHP7+:
- ::double()
- ::float()
| Type | Description | Notes | Default |
|---|---|---|---|
mixed, array
|
Value. | If an array is passed, each value will be processed recursively. | |
bool |
Flatten. |
TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. |
FALSE |
Returns a float if passed by value, otherwise TRUE.
// By value.
$foo = \blobfolio\common\cast::to_float('$1.00'); // 1
$foo = \blobfolio\common\cast::to_float('10%'); // .1
$foo = \blobfolio\common\cast::to_float(5.5); // 5.5
// By reference.
\blobfolio\common\ref\cast::to_float($foo);Typecast to integer. This is like (int) hinting, but again, will strip out non-numeric data and attempt to convert values like percents, etc. This function will also properly interpret things like "true", "on", etc.
- By Value
- By Reference
The following are available under PHP7+:
- ::int()
- ::integer()
| Type | Description | Notes | Default |
|---|---|---|---|
mixed, array
|
Value. | If an array is passed, each value will be processed recursively. | |
bool |
Flatten. |
TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. |
FALSE |
Returns an integer if passed by value, otherwise TRUE.
// By value.
$foo = \blobfolio\common\cast::to_int(5.5); // 5
$foo = \blobfolio\common\cast::to_int('3'); // 3
$foo = \blobfolio\common\cast::to_int(8); // 8
// By reference.
\blobfolio\common\ref\cast::to_int($foo);This strips out non-numerical data and returns a float.
- By Value
- By Reference
The following are available under PHP7+:
- ::number()
| Type | Description | Notes | Default |
|---|---|---|---|
mixed, array
|
Value. | If an array is passed, each value will be processed recursively. | |
bool |
Flatten. |
TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. |
FALSE |
Returns a float if passed by value, otherwise TRUE.
// By value.
$foo = \blobfolio\common\cast::to_number('5.5'); // 5.5
$foo = \blobfolio\common\cast::to_number(3); // 3
$foo = \blobfolio\common\cast::to_number('8%'); // 0.08
// By reference.
\blobfolio\common\ref\cast::to_number($foo);This casts to a UTF-8 string.
- By Value
- By Reference
The following are available under PHP7+:
- ::string()
| Type | Description | Notes | Default |
|---|---|---|---|
mixed, array
|
Value. | If an array is passed, each value will be processed recursively. | |
bool |
Flatten. |
TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. |
FALSE |
Returns a string if passed by value, otherwise TRUE.
// By value.
$foo = \blobfolio\common\cast::to_string(1); // 1
// By reference.
\blobfolio\common\ref\cast::to_string($foo);This typecasts the variable to the specified type, mapping to one of the above functions.
- By Value
- By Reference
| Type | Description | Notes | Default |
|---|---|---|---|
mixed, array
|
Value. | If an array is passed, each value will be processed recursively. | |
string |
Type. | Either "boolean", "bool", "integer", "int", "double", "float", "string", or "array"
|
|
bool |
Flatten. |
TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. |
FALSE |
Returns the cast variable if passed by value, otherwise TRUE.
CLI
Constants
Dom Helpers
- ::get_nodes_by_class()
- ::innerhtml()
- ::load_svg()
- ::parse_css()
- ::remove_namespace()
- ::remove_node()
- ::remove_nodes()
- ::save_svg()
Files and Paths
- ::copy()
- ::csv_headers()
- ::data_uri()
- ::dirsize()
- ::empty_dir()
- ::hash_dir()
- ::leadingslash()
- ::line_count()
- ::mkdir()
- ::path()
- ::readfile_chunked()
- ::redirect()
- ::rmdir()
- ::scandir()
- ::trailingslash()
- ::unixslash()
- ::unleadingslash()
- ::unparse_url()
- ::untrailingslash()
Formatting
- ::array_flatten()
- ::array_to_indexed()
- ::ceil()
- ::cidr_to_range()
- ::decode_entities()
- ::decode_escape_entities()
- ::decode_js_entities()
- ::decode_unicode_entities()
- ::excerpt()
- ::floor()
- ::fraction()
- ::inflect()
- ::ip_to_number()
- ::ip_to_subnet()
- ::json()
- ::json_decode()
- ::json_encode()
- ::links()
- ::list_to_array()
- ::money()
- ::number_to_ip()
- ::phone()
- ::round()
- ::to_csv()
- ::to_timezone()
- ::to_xls()
General Data Helpers
- ::array_compare()
- ::array_idiff()
- ::array_iintersect()
- ::array_ikey_exists()
- ::array_isearch()
- ::array_map_recursive()
- ::array_otherize()
- ::array_pop()
- ::array_pop_rand()
- ::array_pop_top()
- ::cc_exp_months()
- ::cc_exp_years()
- ::datediff()
- ::iin_array()
- ::in_range()
- ::ip_in_range()
- ::is_json()
- ::is_utf8()
- ::json_decode_array()
- ::length_in_range()
- ::parse_args()
- ::random_int()
- ::random_string()
- ::switcheroo()
- ::unsetcookie()
Images
Multi-Byte Wrappers
- ::parse_str()
- ::parse_url()
- ::str_pad()
- ::str_split()
- ::strlen()
- ::strpos()
- ::strrev()
- ::strrpos()
- ::strtolower()
- ::strtoupper()
- ::substr()
- ::substr_count()
- ::trim()
- ::ucfirst()
- ::ucwords()
- ::wordwrap()
Sanitizing and Validation
- ::accents()
- ::attribute_value()
- ::au_state()
- ::ca_postal_code()
- ::cc()
- ::control_characters()
- ::country()
- ::csv()
- ::date()
- ::datetime()
- ::domain()
- ::ean()
- ::email()
- ::file_extension()
- ::html()
- ::hostname()
- ::ip()
- ::iri_value()
- ::isbn()
- ::js()
- ::mime()
- ::name()
- ::password()
- ::printable()
- ::province()
- ::quotes()
- ::state()
- ::svg()
- ::timezone()
- ::to_range()
- ::upc()
- ::url()
- ::utf8()
- ::whitespace()
- ::whitespace_multiline()
- ::zip5()
Typecasting