thomas-squall / utils
Generic Utilities for PHP
Requires
- ext-json: *
- thomas-squall/file-utils: ^0.2.6
- thomas-squall/string-utils: ^0.5.5
README
List of available functions
Other contained Repositories
array_to_object
Description
Returns an object out of an array. Note that only fields corresponding to the array keys will be returned.
Definition
array_to_object($array, $object)
Where:
- $array is the array to convert
- $object is the object or class to be returned
- $add_not_existing_properties is used to choose whether to add properties not defined in the class to the object
Usage
class Person { public $name; public $age; } $array = [ "name" => "MyName", "surname" => "MySurname", "age" => 27 ]; print_r(array_to_object($array, "Person")); echo PHP_EOL; print_r(array_to_object($array, "Person", true));
This will print:
Person Object ( [name] => MyName [age] => 27 ) Person Object ( [name] => MyName [age] => 27 [surname] => MySurname )
json_to_object
Description
Returns an object out of a json string. Note that only fields corresponding to the json keys will be returned unless you pass $add_not_existing_properties to true.
Definition
json_to_object($json, $object, $add_not_existing_properties = false)
Where:
- json is the json to convert
- $object is the object or class to be returned
- $add_not_existing_properties is used to choose whether to add properties not defined in the class to the object
Usage
class Person { public $name; public $age; } $json = '{ "name": "MyName", "surname": "MySurname", "age": 27 }'; print_r(array_to_object($json, "Person")); echo PHP_EOL; print_r(array_to_object($json, "Person", true));
This will print:
Person Object ( [name] => MyName [age] => 27 ) Person Object ( [name] => MyName [age] => 27 [surname] => MySurname )
parse_args
Description
Build a new array out of the $options one with defaults values taken from $defaults when nothing is found.
Definition
parse_args($options, $defaults)
Where:
- $options is the array to be parsed and used to build the new array
- $defaults is the array containing default values to use when no correspondence is found
Usage
$defaults = [ "name" => "MyName", "surname" => "MySurname", "age" => 27 ]; $person = [ "name" => "MyRealName", "surname" => "MyRealSurname" ]; print_r(parse_args($person, $defaults));
This will print:
Array ( [name] => MyRealName [surname] => MyRealSurname [age] => 27 )
get
Description
Checks if the provided value is empty and, if it is, the default value will be returned. The value itself will be returned otherwise.
Definition
get($value, $default = "")
Where:
- $value is the value to be checked to be empty or not
- $default is the value to pass in case the first is empty. Defaulted to empty string
Usage
$value = ""; echo get($value, "hello");
This will print:
hello
dispose
Description
Disposes any item.
Definition
dispose(&$item)
Where:
- $item is the item to be disposes
Usage
$array = [ "Hello", " ", "world" ]; dispose($array);
is_cli
Description
Returns true if the script is running in the cli or not.
Definition
is_cli()
Usage
if (is_cli()) echo "This script is running in the shell"; else echo "This script is not running in the shell";