# Standard library

The standard library is a set of multiple functions that are accessible from anywhere inside Wolff and can be called like native PHP functions.

The standard library can be disabled, meaning that its functions won't be available in the global namespace. To disable it, set to false the stdlib_on key in the configuration array of the system/config.php file.

General

Path

path([string $path]): string

Returns the absolute path of the given relative path. Keep in mind that the given path is supposed to be relative to the project root folder.

path('app/controllers');

If the wolff project is located at /var/www/html/wolff, it will return /var/www/html/wolff/app/controllers.

Relative path

relativePath([string $path]): string

Returns the given absolute path converted to a relative path. The returned path is relative to the project root folder.

relativePath('/home/usbac/Documents/wolff/app/controllers');

If the wolff project is located at /home/usbac/Documents/wolff, it will return app/controllers.

Is associative

isAssoc(array $arr): bool

Returns true if the given array is associative, false otherwise.

Keep in mind that in the context of the function, an associative array is an array with non-numeric keys.

$arr = [
    'name' => 'Margaret',
    'age'  => 63
];

isAssoc($arr);

That should return true.

isAssoc([ 'Margaret', 'Thomas', 'Edward' ]);

That should return false.

Value

val(array $arr[, string $key]): mixed

Returns the specified key from the given array. If the given key doesn't exists it will simply return null.

$arr = [
    'name' => 'Margaret',
    'age'  => 63
];

val($arr, 'name');

That should return the string Margaret.

The key accepts dot notation:

val($arr, 'user.name');

That should be the equivalent to $arr['user']['name'].

Array remove

arrayRemove(array &$arr, $val): bool

Removes an element from the given array, based on its value.

It returns true if the element has been removed, false otherwise.

$ships = [
    'mauretania', 'lusitania', 'queen_mary'
];

arrayRemove($ships, 'lusitania');

After that, the ships variable should look like this:

(
    [0] => mauretania
    [2] => queen_mary
)

Keep in mind that the array's keys are preserved even for non-assosiative arrays.

Bytes to string

bytesToString(int $size[, int $precision]): string

Returns the given size (in bytes) as a human-readable string. The default precision is 2.

bytesToString(540000)

That example should return '527KB'.

bytesToString(10000000, 3)

That example should return '9.537MB'.

The human-readable string to return can go from B (byte) to YB (yottabyte).

Local

local([array $whitelist]): bool

Returns true if the current script is running in localhost, false otherwise.

local();

The default IPs used to check that the script is in localhost are: 127.0.0.1 and ::1.

An array with IPs can be passed to the function to extend it.

local([ '127.0.0.2', '::2' ]);

Average

average(array $arr): mixed

Returns the average value of the given numbers array. The return type could be int or float.

average([ 2.5, 5.46, 4, 9 ]);

That should return 5.24.

Echo and die

echod(...$args): void

Echo a variable and then die (for debugging purposes).

echod('Lorem ipsum dolor sit amet');

Multiple parameters can be passed.

echod('Lorem', 'ipsum', 'dolor');

Print

printr(...$args): void

Prints the given variables in a nice looking way.

$array = ['laravel', 'codeigniter', 'wolff', 'yii'];
printr($array);

This function can take any number of parameters.

printr($array, $foo, $foo2...);

Print and die

printrd(...$args): void

Prints the given variables in a nice looking way and then die.

$array = ['laravel', 'wolff', 'yii'];
printrd($array);

This function can take any number of parameters.

printrd($array, $foo, $foo2...);

Var dump and die

dumpd(...$args): void

Var dump a variable and then die (for debugging purposes).

$str = 'Hello world';
dumpd($str);

Validate CSRF

validateCsrf(): bool

Returns true if the current request is safe from csrf (cross site request forgery), false otherwise.

This simply verifies that the current user is the one who made the request to the application.

if (validateCsrf()) {
    echo 'The incoming form was made by the user, continue :)';
    // Code
} else {
    echo 'You shall not pass';
}

This can be combined with the @csrf tag available in the template engine.

If you don't want to turn on the standard library just for using this function, you can make your own implementation.

function validateCsrf()
{
    // Get
    if ($_SERVER['REQUEST_METHOD'] === 'GET') {
        return isset($_GET['__token'], $_COOKIE['__token']) && $_GET['__token'] === $_COOKIE['__token'];
    }

    // Post
    return isset($_POST['__token'], $_COOKIE['__token']) && $_POST['__token'] === $_COOKIE['__token'];
}

Is int

isInt($int): bool

Returns true if the given parameter complies with an integer, false otherwise.

isInt('1');
isInt(1);

Both of the calls showed above will return true.

Is float

isFloat($float): bool

Returns true if the given parameter complies with a float, false otherwise.

isFloat('1.5');
isFloat(1.5);

Both of the calls showed above will return true.

Is json

isJson(string $str): bool

Returns true if the given string is a json, false otherwise.

_Notice: This function modifies the json_last_error value._

$json = '{name: "John", age: 21, city: "New York"}';
isJson($json);

That will return true.

To array

toArray($obj): array

Returns the given variable as an associative array.

Useful when it is necessary to turn a multidimensional json or std object into an array.

$json = '{name: "John", age: 21, city: "New York"}';
toArray($json);

Get url

url([string $url]): string

Returns the given string as a local url. Useful for redirections.

Example:

url('home/sayHello');

If the project is located at http://localhost/wolff, the function will return http://localhost/wolff/home/sayHello.

If the project is located at https://www.getWolff.com, the function will return https://www.getWolff.com/home/sayHello.

Get client Ip

getClientIP(): string

Returns the current client IP.

getClientIP();

In localhost it will return ::1.

Get current page

getCurrentPage(): string

Returns the current url.

getCurrentPage();

Get pure current page

getPureCurrentPage()

Returns the current url without parameters.

getPureCurrentPage();

If the current url is example.com/homepage?id=2 it will return example.com/homepage.

Get benchmark time

getBenchmark(): float

Returns the time between the page load start and the current time in seconds as float.

getBenchmark();

Get public

getPublic([string $path]): string

Returns the absolute public path of the given path.

getPublic('favicon.ico');

If the project is located at /var/www/html/wolff that should return /var/www/html/wolff/public/favicon.ico.

Get config

config(string $key): mixed

Returns the specified key value from the CONFIG array, or from the environment file if env_override is set to true.

config('root_dir');

The key accepts dot notation.

config('db.password');

Adding functions to the Standard library

You can add your own functions to the standard library this way.

  1. Create a php file with the following structure in the system folder:
<?php

namespace {
    function example() {
        //Code
    }
}
  1. Add all the functions you want.

  2. Add the following line to your composer.json file inside the autoload > files array

"system/yourfilename.php",

And you are done, now your functions should be available inside all of your Wolff project.

Remember to run composer dump-autoload for the changes to take effect.

Documentation made with
Something is wrong in here? Make a issue/pull request on the repo.