Mecha CMS

Learn.

Function

Author: Taufik Nurrohman · 229 Views

Tweet

Helper functions.

Test 

__test__($a[, $b[, $c[, ...]]]);

Dump a PHP code to show the results:

__test__($foo, $bar, $baz);

Format 

__format__($s, $x = "\n", $d = '#', $r = true);

Create regular expression pattern from a string contains some wildcards:

$pattern = __format__('lorem %s% ipsum %i% dolor # sit amet');
ParameterDescription
$sThe string input.
$xForbidden characters for the %s% wildcard.
$dThe regular expression delimiter to escape.
$rCan the wildcard be replaced by an empty data?
PatternDescription
%s%Accept any string but $x.
%i%Accept any string numbers.
%f%Accept any string numbers and floats.
%b%Accept any string booleans.
%*%Accept any string and $x.
%[foo,bar,baz\,qux]%Accept foo, bar and baz,qux.
%[foo,bar,%i%]%Accept foo, bar and any string numbers.

This function will return a regular expression string without the delimiters:

$pattern = 'lorem ([^\n]+) ipsum (\d+) dolor \# sit amet';

Replace 

__replace__($s, $x = "\n", $r = true);

TODO

Is Anemon 

__is_anemon__($x);

Return true if $x is array or object.

Is Numeric Anemon 

__is_anemon_0__($x);

Return true if the encoded JSON version of $x starts with [ and ends with ] (a JavaScript array).

Is Associative Anemon 

__is_anemon_a__($x);

Return true if the encoded JSON version of $x starts with { and ends with } (a JavaScript object).

Is Instance 

__is_instance__($x);

Return true if $x is an instance of any class.

Is JSON 

__is_json__($x);

Return true if $x is a JSON string.

Is Serialize 

__is_serialize__($x);

Return true if $x is a serialized string.

Class to File 

__c2f__($x, $s = '-');

Convert class name into file name:

$test = __c2f__('Foo\Bar\BazQux\Abc\ABC');

Result:

$test = 'foo.bar.baz-qux.abc.a-b-c';

File to Class 

__f2c__($x, $s = '-');

Convert file name into class name:

$test = __f2c__('foo.bar.baz-qux.abc.a-b-c');

Result:

$test = 'Foo\Bar\BazQux\Abc\ABC';

URL 

__url__($key = null, $fail = false);

Extract current URL data:

$test_1 = __url__();
$test_2 = __url__('protocol');
$test_3 = __url__('wow');
$test_4 = __url__('wow', 0);

Result:

$test_1 = [
    'scheme' => 'http',
    'protocol' => 'http://',
    'host' => 'localhost',
    'port' => 80,
    'user' => null,
    'pass' => null,
    'directory' => 'sub-directory',
    'url' => '[[url.protocol]][[url.host]]/sub-directory',
    'path' => 'foo/bar',
    'query' => '?foo=bar',
    'previous' => null,
    'current' => '[[url.protocol]][[url.host]]/sub-directory/foo/bar',
    'next' => null,
    'hash' => null
];
$test_2 = 'http://';
$test_3 = false;
$test_4 = 0;

Others 

Array 

a($o, $safe = true);

Convert object to array:

$test = new stdClass;
$test->a = 'b';
$test->c = new Date;
$test->d = (object) [
    'year' => 0,
    'month' => 0,
    'day' => 0
];

$test = a($test);

Result:

$test = [
    'a' => 'b',
    'c' => Date::__set_state([
        'date' => '2016-12-25 23:11:16'
    ]),
    'd' => [
        'year' => 0,
        'month' => 0,
        'day' => 0
    ]
];

Note: Class instance will not be converted into array if $safe is set to true.

b 

Reserved.

Camel Case 

c($x, $s = "", $X = "");

Convert text to camelCase:

Declare 

d($f, $fn = null, $s__ = []);

Declare classes with callback:

Evaluate 

e($s, $x = []);

Evaluate string data to their appropriate data type:

$test_1 = e('true');
$test_2 = e([
    'true',
    'false',
    'null',
    '1',
    '1.5',
    '"true"',
    '"false"',
    '"null"',
    '"1"',
    '"1.5"',
    '{"foo":1,"bar":"1"}'
]);

Result:

$test_1 = true;

$test_2 = [
    true,
    false,
    null,
    1,
    1.5,
    'true',
    'false',
    'null',
    '1',
    '1.5',
    [
        'foo' => 1,
        'bar' => '1'
    ]
];

Filter 

f($x, $s = '-', $l = false, $X = 'a-zA-Z\d', $f = 1);

Filter/sanitize string:

Glob 

g($s = ROOT, $x = '*', $q = "", $o = false, $h = true);

Advance PHP glob function:

Hyphenate 

h($x, $s = '-', $X = "");

Convert text to slug-case with - (hyphen) as the default separator:

Include 

i($a, $b = [], $fn = [null, null], $s__ = []);

Include files with callback:

j 

Reserved.

k 

Reserved.

Lower Case 

l($x);

Convert string into lower case:

$test = l('POKÉMON');

Result:

$test = 'pokémon';

m 

Reserved.

Normalize 

n($x, $t = '    ');

Convert \r\n and \r to \n and \t to 4 spaces:

Object 

o($a, $safe = true);

Convert array to object:

$test = o([
    'a' => 'b',
    'c' => 'd',
    'e' => ['f', 'g', 'h', 'i'],
    'j' => [
        0 => 'k',
        1 => 'l',
        '2' => 'm'
    ],
    'n' => [
        0 => 'o',
        1 => 'p',
        'q' => 'r'
    ]
]);

Result:

$test = stdClass::__set_state([
    'a' => 'b',
    'c' => 'd',
    'e' => ['f', 'g', 'h', 'i'],
    'j' => ['k', 'l', 'm'],
    'n' => stdClass::__set_state([
        0 => 'o',
        1 => 'p',
        'q' => 'r'
    ])
]);

Note: Array with numeric keys will not be converted into object if $safe is set to true.

Pascal Case 

p($x, $s = "", $X = "");

Convert text to PascalCase:

Quantity 

q($x, $deep = false);

Quantity counter (length of string, number or anemon):

Require 

r($a, $b = [], $fn = [null, null], $s__ = []);

Require files with callback:

String 

s($x);

Convert data type to their string format (this is the inverse version of e):

Trim 

t($x, $o = '"', $c = null);

Trim string from ?? and !! once:

$test = t('????abc!!!!', '??', '!!');

Result:

$test = '??abc!!';

Upper Case 

u($x);

Convert text to UPPER CASE:

$test = u('pokémon');

Result:

$test = 'POKÉMON';

Unescape 

v($x);

Unescape characters (this is the inverse version of x):

Word 

w($x, $c = [], $n = false);

Convert any data to plain words:

Escape 

x($x, $c = "'", $d = '-+*/=:()[]{}<>^$.?!|\\');

Escape characters:

Yield 

y($x, $a = []);

Output/yield an echo-based function as normal return-based function:

Zip 

z($a, $b = true, $safe = true);

Export array/object into a compact PHP file:

0 Comments

No comments yet.