# Standard library

Beside the custom libraries you can use and create in Wolff, there’s the standard library which is used by Wolff itself, but you can use it too.

These functions are accessible from anywhere inside Wolff just like the native PHP functions.

General

Value

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'].

Average

Returns the average value of the given numbers array.

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

That should return 5.24.

Echo and die

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

echod('Lorem ipsum dolor sit amet');

Print array

Print an array in a nice looking way

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

This function can take any number of parameters.

printr($array, $array2, $array3...);

Print array and die

Print an array in a nice looking way and then die

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

This function can take any number of parameters.

printrd($array, $array2, $array3...);

Var dump and die

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

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

Var dump all

Var dump all the current variables.

dumpAll();

Is int

Returns true if the given parameter complies with an integer.

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

Both of the calls showed above will return true.

Is float

Returns true if the given parameter complies with an integer.

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

Both of the calls showed above will return true.

Is bool

Returns true if the given parameter complies with an boolean.

isBool(true);
isBool('1');

Both of the calls showed above will return true.

Only the numeric values 1 and 0, and the strings 'true', 'false', '1' and '0' are counted as boolean.

Is json

Returns true if the given string is a json.

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

That will return true.

To array

Returns the given variable as a associative array.

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

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

Array to csv

Convert an array result into a csv file and then downloads it.

$array = [
    0 => [
        'name' => 'John Doe',
        'email' => '[email protected]'
    ],
    1 => [
        'name' => 'Jane Doe',
        'email' => '[email protected]'
    ],
];

//The first parameter is the array, the second is the desired file name (without extension).

arrayToCsv($array, 'filename');

Get url

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

Returns the current client IP.

getClientIP();
//In localhost it will return ::1

Get user agent

Returns the HTTP User Agent information (equivalent to $_SERVER['HTTP_USER_AGENT']).

getUserAgent();

Get server root

Returns the server root path (equivalent to $_SERVER['DOCUMENT_ROOT']).

getServerRoot();

Local

Returns true if the current request is running in localhost.

local();

In CLI

Returns true if the current code is running in a Command Line Interface, false otherwise.

inCli();

Get current page

Returns the current url relative to the project root directory.

getCurrentPage();
//If the current url is localhost/wolff/homepage it will return 'homepage'.

Get pure current page

Returns the complete current url without parameters.

getPureCurrentPage();
//If the current url is http://example.com/homepage?id=2 it will return 'http://example.com/homepage'.

Get benchmark time

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

getBenchmark();

Config

You can get the configuration constants through the following functions.

Keep in mind that the paths returned by the config.php functions are relative to the server root, except for the getPublicDir and getProjectDir functions.

The functions related to directories can take a string as a parameter which will be concatenated to the directory returned. Example:

getPublic('home.png');
//In this case it will return 'localhost/wolff/public/home.png'

Get config value

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

config('root_dir');

That is the equivalent to CONFIG['root_dir'].

The key accepts dot notation:

config('db.password');

That is the equivalent to CONFIG['db']['password'].

Get server

Returns the current server.

getServer();

Get database

Returns the current database name.

getDB();

Get database management system

Returns the current database management system.

getDBMS();

Get database user

Returns the current database username.

getDbUser();

Get database password

Returns the current database username password.

getDbPass();

Get language

Returns the current language being used by Wolff.

getLanguage();

Get directory

Returns the project root path.

getDir();

Get project directory

Returns the project root path relative to the server root.

getProjectDir();

Get system directory

Returns the system folder path.

getSystemDir();

Get app directory

Returns the app folder path.

getAppDir();

Get public path

Returns the public folder path.

getPublic();

Get cache directory

Returns the cache folder path.

getCacheDir();

Get page title

Returns the current page title.

getPageTitle();

Get main page

Returns the current main page.

getMainPage();

Get Wolff version

Returns the current version of Wolff.

wolffVersion();
//In this case it should return 2.0

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/definitions folder:
<?php

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

  2. After that add the following line to your composer.json file inside the autoload > files array

"system/definitions/yourfilename.php",
  1. Remember to run composer dump-autoload for the changes to take effect.

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