Mecha CMS

Mecha CMS blog and documentation.

Class Mecha

Updated: Sunday, 07 August 2016

Array handling.

Convert Array into Object

$my_object = Mecha::O($my_array);

Convert Object into Array

$my_array = Mecha::A($my_object);

Sorting an Array

$sorted = Mecha::eat($my_array)->order($order, $key, $preserve_key, $missing)->vomit();
ParameterDescription
$orderThe array order: ASC or DESC.
$keyThe array key as a sorting reference for multidimensional array. Leave it as null or undefined for simple array handling.
$preserve_keyKeep original array keys.
$missingDefault value for $key if $key does not exist in certain array item.

Randomize an Array

$randomized = Mecha::eat($my_array)->shake()->vomit();

Vomit Specific Array Value

$bucket = Mecha::eat($my_array)->vomit($key, $fallback);
ParameterDescription
$keyThe array’s key of value you want to output. You may also use dot notation patterns for handling multidimensional array.
$fallbackFallback value if the desired array value does not exist.

Creating Array Chunks

$chunk = Mecha::eat($my_array)->chunk($sequence, $total)->vomit();
ParameterDescription
$sequenceThe sequence of array in the chunks to be outputted. Set as null to output all chunks results.
$totalThe number of array per chunk.

Nested Array Handling

Enhanced since version 1.0.2. Used to handle multidimensional arrays via dot notation patterns:

Set Array Value Recursively

$test = array();

Mecha::SVR($test, 'foo.bar.baz', 'test value');

Results:

$test = array(
    'foo' => array(
        'bar' => array(
            'baz' => 'test value'
        )
    )
);

Get Array Value Recursively

$test = array(
    'foo' => array(
        'bar' => array(
            'baz' => 'test value'
        )
    )
);

var_export(Mecha::GVR($test, 'foo.bar', 'fallback value'));
var_export(Mecha::GVR($test, 'foo.bar.abc', 'fallback value'));

Results:

array(
    'baz' => 'test value'
);

fallback value

The second parameter value will be used if the desired data does not exist.

Unset Array Value Recursively

$test = array(
    'foo' => array(
        'bar' => array(
            'baz' => 'test value'
        )
    )
);

Mecha::UVR($test, 'foo.bar');

var_export($test);

Results:

$test = array(
    'foo' => array()
);

Array Walk

Added since version 1.1.3.

Start with walking through the array. By default, it will starts from the first array item:

$test = array(
    'a' => 1,
    'b' => 2,
    'c' => 3
);

$results = Mecha::walk($test);

Move to the Next Array Item

Mecha::walk($test)->next();

Move to the Previous Array Item

Mecha::walk($test)->prev();

Move to the 2nd Array Item

Mecha::walk($test)->to(1);

Insert Data Before the Current Array

$results_1 = Mecha::walk($test)->before('wow')->vomit();
$results_2 = Mecha::walk($test)->before('wow', 'such')->vomit();

Results:

$results_1 = array(
    0 => 'wow',
    'a' => 1,
    'b' => 2,
    'c' => 3
);

$results_2 = array(
    'such' => 'wow',
    'a' => 1,
    'b' => 2,
    'c' => 3
);

Insert Data After the Current Array

$results_1 = Mecha::walk($test)->after('wow')->vomit();
$results_2 = Mecha::walk($test)->after('wow', 'such')->vomit();

Results:

$results_1 = array(
    'a' => 1,
    1 => 'wow',
    'b' => 2,
    'c' => 3
);

$results_2 = array(
    'a' => 1,
    'such' => 'wow',
    'b' => 2,
    'c' => 3
);

Replace Data of the Current Array

$results = Mecha::walk($test)->replace('wow')->vomit();

Results:

$results = array(
    'a' => 'wow',
    'b' => 2,
    'c' => 3
);

Prepend Data to Array

$results_1 = Mecha::walk($test)->prepend('wow')->vomit();
$results_2 = Mecha::walk($test)->prepend('wow', 'such')->vomit();

Results:

$results_1 = array(
    0 => 'wow',
    'a' => 1,
    'b' => 2,
    'c' => 3
);

$results_2 = array(
    'such' => 'wow',
    'a' => 1,
    'b' => 2,
    'c' => 3
);

Append Data to Array

$results_1 = Mecha::walk($test)->append('wow')->vomit();
$results_2 = Mecha::walk($test)->append('wow', 'such')->vomit();

Results:

$results_1 = array(
    'a' => 1,
    'b' => 2,
    'c' => 3,
    3 => 'wow'
);

$results_2 = array(
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'such' => 'wow'
);

Get the 1st Array Item

echo Mecha::walk($test)->first();

Get the Last Array Item

echo Mecha::walk($test)->last();

Get the Current Array Item

echo Mecha::walk($test)->current();

Get the Selected Array Item

echo Mecha::walk($test)->shake()->prev()->next()->get();

Edge Value

Added since version 1.1.3.

Use this method to make sure that the generated value is not exceed the minimum and maximum limit:

// min: 0
// max: 20
$result = Mecha::edge($data, 0, 20);

Array Loop

Added since version 1.2.0.

This …

$results = Mecha::walk($array, function($value, $key) {
    return File::B($value) . ' ' . $key;
});

… is equal to …

$results = array();
foreach($array as $key => $value) {
    $results[$key] = File::B($value) . ' ' . $key;
}
unset($array);

Handle Multiple Conditional Statements

Added since version 1.1.3.

This is just a shortcut for PHP switch and case:

$test = 's';

echo Mecha::alter($test, array(
    'a' => 'array',
    'b' => 'boolean',
    'f' => 'float',
    'i' => 'integer',
    'n' => 'null',
    'o' => 'object',
    's' => 'string'
), 'undefined');

Result:

string

Handle Missing Array Data

Added since version 1.1.3.

Useful for creating default value.

$default = array(
    'nav' => true,
    'speed' => 1000
);

$alternate = array(
    'speed' => 500
);

$result = Mecha::extend($default, $alternate);

Results for $default and $result:

$default = array(
    'nav' => true,
    'speed' => 500
);

$result = array(
    'nav' => true,
    'speed' => 500
);
Donation and Email Subscription