These are some of the functions I use intensively for my projects.
As I don't want my functions to interfere with any of yours, they all use the Embryo
namespace.
This function returns the depth of a given array.
- 0 if the array is empty
- 1 for unidimensional arrays
- 2 to x for every other array types
WARNING: a recursive array could currently cause an infinite loop (See issue #29).
These helpers are made specifically for command-line interfaces (CLI)
Just add $args = Embryo\cliArguments();
at the beginning of your CLI script, and use your CLI script as you do with a bash
script.
php myScript.php -action makeSomething -verbose
The array $args
will now contain :
- action: 'makeSomething'
- verbose: true
You can also give an array of allowed arguments in the first parameter of cliArguments()
. Any parameter other than
the ones listed will generate and error and die.
Calling this function clears the command line interface. Tested with Windows and Unix environments.
Tells whether the current script is called in a command-line interface.
Displays a nice progressBar.
Since the display is updated less frequently than the spinner, it is slightly quicker than cliSpinner
.
foreach($myvar as $var) {
Embryo\cliProgressBar($currentIndex, $total);
}
Prompts a question to the user, and returns a boolean: true
if the answer is the first letter or the first answer,
false
in every other case.
If no answers are set, default answers are [y]es / [n]o
Embryo\cliPrompt('Do you want to continue ?');
Will display Do you want to continue ? [y]es / [n]o
, and will return true if the answer is either y
or yes
.
Embryo\cliPrompt('Do you want to continue ?', ['continue', 'abort']);
Will display Do you want to continue ? [c]ontinue / [a]bort
, and will return true if the answer is either c
or
continue
.
Displays a question to the user, and returns its answer as a string.
$answer = Embryo\cliQuestion('What is the airspeed velocity of an unladen swallow ?');
Adds a nice spinner to your loops.
foreach($myvar as $var) {
Embryo\cliSpinner('Manipulating data');
// Do something
}
Embryo\cliSpinner('Done !', true);
Displays an array as a readable table.
By default, the first value's keys will be used as header names.
$table = [
[
'id' => 1,
'name' => 'John Doe',
'email' => 'john@doe.com',
],
[
'id' => 2,
'name' => 'Jane Doe',
'email' => 'jane@doe.com',
],
];
echo Embryo\cliTable($table);
will be displayed like this :
╔════╤══════════╤══════════════╗
║ id │ name │ email ║
╟────┼──────────┼──────────────╢
║ 1 │ John Doe │ john@doe.com ║
║ 2 │ Jane Doe │ jane@doe.com ║
╚════╧══════════╧══════════════╝
Dumps a variable. The second parameters allows to dump only if the corresponding $_REQUEST ($_GET or $_POST) parameter exists, and is equivalent to true.
\Embryo\d($myVar);
Like \Embryo\d()
, this function dumps a variable. It also dies just after.
If nothing has been sent to the browser before the call, and the data is an array or an object, a json header will be sent and the data will be displayed as a json string.
Pretty prints a given value by wrapping a var_dump into <pre>
tags
Creates an URL from any UTF-8 compatible given string.
It removes the accents, and replaces all non-alphanumeric character by hyphens.
$url = \Embryo\seoUrl("I'm giving my résumé to the café, Señor !");
// $url equals 'i-m-giving-my-resume-to-the-cafe-senor'
This method is intended to be a reverse of PHP's builtin parse_url.
The parsed url's query
key can be a string or an array.
Returns the correct base64 value for a given image path.
Completes a string to a given length. If the length is shorter than the string, it will return the full, non-altered string.
$str = \Embryo\strComplete('test', 10, ' ');
// $str is 'test '
Cuts a text at a given number of characters.
If $isTotalLength
is set to true
, the final maximum length will be $length
. If it set to false, the final maximum
length will be $length + strlen($end)
.
If $length is >= 40, the function will not cut into a word, but just after the previous word.
Tells whether a string contains four-byte UTF-8 characters
Tells whether a given string is valid JSON
Tells whether a given string is encoded in UTF-8.
Returns whether the given string contains valid XML code (including HTML)
Removes all accented characters from a given string.
Removes all four-bytes UTF-8 characters from a given UTF-8 string.
It can be used to prevent Illegal mix of collation
errors in your database queries, for example, if your database is not set to UTF8MB4.