Mecha CMS

Deep learning on using the API.

Class Anemon

Author: Taufik Nurrohman · 170 Views

Tweet

Array workers.

Step 

Create list of namespace steps from a string.

Anemon::step($input, $NS = '.', $dir = 1);

Example:

$test_1 = Anemon::step('foo.bar.baz');
$test_2 = Anemon::step('foo.bar.baz', '.', -1);

Result:

$test_1 = [
    'foo.bar.baz',
    'foo.bar',
    'foo'
];

$test_2 = [
    'foo.bar.baz',
    'bar.baz',
    'baz'
];

Edge 

Prevent input value exceeds the minimum and maximum value.

Anemon::edge($input, $min = null, $max = null);

Example:

$min = 0;
$max = 100;

__test__(
    Anemon::edge(4, $min, $max),
    Anemon::edge(-4, $min, $max)
    Anemon::edge(104, $min, $max)
);

Result:

4

0

100

Set 

Set or replace array value recursively.

Anemon::set(array &$input, $key, $value = null);

Example:

$array = [
    'foo' => 1,
    'bar' => [
        'baz' => 2,
        'qux' => 3
    ]
];

Anemon::set($array, 'bar.baz', '#');

Result:

$array = [
    'foo' => 1,
    'bar' => [
        'baz' => '#',
        'qux' => 3
    ]
];

Get 

Get array value recursively.

Anemon::get(array &$input, $key = null, $fail = false);

Example:

$array = [
    'foo' => 1,
    'bar' => [
        'baz' => 2,
        'qux' => 3
    ]
];

$test_1 = Anemon::get($array, 'bar.baz', '#');
$test_2 = Anemon::get($array, 'bar.foo', '#');

Result:

$test_1 = 2;
$test_2 = '#';

Reset 

Remove array value recursively.

Anemon::reset(array &$input, $key = null);

Example:

$array = [
    'foo' => 1,
    'bar' => [
        'baz' => 2,
        'qux' => 3
    ]
];

Anemon::reset($array, 'bar.baz');

Result:

$array = [
    'foo' => 1,
    'bar' => [
        'qux' => 3
    ]
];

Extend 

Extend two or more arrays.

Anemon::extend(array &$input, $a[, $b[, $c[, ...]]]);

Example:

$a = [
    'foo' => 1,
    'bar' => [
        'baz' => 2,
        'qux' => 3
    ]
];

$b = [
    'foo' => 1,
    'bar' => [
        'baz' => '#'
    ],
    'qux' => '#'
];

$test = Anemon::extend($a, $b);

Result:

$test = [
    'foo' => 1,
    'bar' => [
        'baz' => '#',
        'qux' => 3
    ],
    'qux' => '#'
];
$a = [
    'foo' => 1,
    'bar' => [
        'baz' => '#',
        'qux' => 3
    ],
    'qux' => '#'
];

Concat 

Concatenate two or more arrays.

Anemon::concat(array &$input, $a[, $b[, $c[, ...]]]);

Example:

$a = [
    0 => 'a',
    1 => 'b',
    2 => 'c'
];

$b = [
    0 => 'd',
    1 => 'e',
    2 => 'f'
];

$test = Anemon::concat($a, $b);

Result:

$test = [
    0 => 'a',
    1 => 'b',
    2 => 'c',
    3 => 'd',
    4 => 'e',
    5 => 'f'
];
$a = [
    0 => 'a',
    1 => 'b',
    2 => 'c',
    3 => 'd',
    4 => 'e',
    5 => 'f'
];

With Anemon::extend(), the result will be different:

$a = [
    0 => 'a',
    1 => 'b',
    2 => 'c'
];

$b = [
    0 => 'd',
    1 => 'e',
    2 => 'f'
];

$test = Anemon::extend($a, $b);

Result:

$test = [
    0 => 'd',
    1 => 'e',
    2 => 'f'
];
$a = [
    0 => 'd',
    1 => 'e',
    2 => 'f'
];

Eat 

Array walker. Starts with eating or walking…

$array = [
    'foo' => 0,
    'bar' => 1,
    'baz' => 2,
    'qux' => 3
];

Anemon::eat($array); // …

Vomit 

Output specific array data.

->vomit($key = null, $fail = false);

Example:

$test_1 = Anemon::eat($array)->vomit();
$test_2 = Anemon::eat($array)->vomit('bar', '#');
$test_3 = Anemon::eat($array)->vomit('wow', '#');

Result:

$test_1 = $array;
$test_2 = 1;
$test_3 = '#';

Is 

Reduce array data to those that pass the function’s test.

->is($fn ($value, $key));

Example:

$test = Anemon::eat($array)->is(function($value, $key) {
    return $value < 2;
})->vomit();

Result:

$test = [
    'foo' => 0,
    'bar' => 1
];

Not 

Reduce array data to those that does not pass the function’s test.

->not($fn ($value, $key));

Example:

$test = Anemon::eat($array)->not(function($value, $key) {
    return $value < 2;
})->vomit();

Result:

$test = [
    'baz' => 2,
    'qux' => 3
];

Shake 

Shuffle array values.

->shake($preserve_key = true);

Example:

$test_1 = Anemon::eat($array)->shake()->vomit();
$test_2 = Anemon::eat($array)->shake(false)->vomit();

Result:

$test_1 = [
    'qux' => 3,
    'foo' => 0,
    'bar' => 1,
    'baz' => 2
];

$test_2 = [
    0 => 3,
    1 => 0,
    2 => 1,
    3 => 2
];

Sort 

Sort array values.

->sort($sort = 1, $preserve_key = false);

Example:

$array = [
    'foo' => [
        'size' => 'medium',
        'color' => 'green',
        'id' => 2
    ],
    'bar' => [
        'id' => 4
    ],
    'baz' => [
        'size' => 'large',
        'color' => 'blue',
        'id' => 3
    ],
    'qux' => [
        'size' => 'small',
        'color' => 'red',
        'id' => 1
    ]
];

//  1: ASC
// -1: DESC

// basic
$test_1 = Anemon::eat($array)->sort(1)->vomit();

// sort by value of the `color` property
$test_2 = Anemon::eat($array)->sort([1, 'color'])->vomit();

// preserve original array key
$test_3 = Anemon::eat($array)->sort([-1, 'color'], true)->vomit();

// set default value of array item with missing `color` property as `#black`
$test_4 = Anemon::eat($array)->sort([-1, 'color'], '#black')->vomit();

Result:

$test_1 = [
    0 => [
        'id' => 4
    ],
    1 => [
        'size' => 'large',
        'color' => 'blue',
        'id' => 3
    ],
    2 => [
        'size' => 'medium',
        'color' => 'green',
        'id' => 2
    ],
    3 => [
        'size' => 'small',
        'color' => 'red',
        'id' => 1
    ]
];

$test_2 = [
    0 => [
        'size' => 'large',
        'color' => 'blue',
        'id' => 3
    ],
    1 => [
        'size' => 'medium',
        'color' => 'green',
        'id' => 2
    ],
    2 => [
        'size' => 'small',
        'color' => 'red',
        'id' => 1
    ]
];

$test_3 = [
    'qux' => [
        'size' => 'small',
        'color' => 'red',
        'id' => 1
    ],
    'foo' => [
        'size' => 'medium',
        'color' => 'green',
        'id' => 2
    ],
    'baz' => [
        'size' => 'large',
        'color' => 'blue',
        'id' => 3
    ]
];

$test_4 = [
    'qux' => [
        'size' => 'small',
        'color' => 'red',
        'id' => 1
    ],
    'foo' => [
        'size' => 'medium',
        'color' => 'green',
        'id' => 2
    ],
    'baz' => [
        'size' => 'large',
        'color' => 'blue',
        'id' => 3
    ],
    'bar' => [
        'id' => 4,
        'color' => '#black'
    ]
];

Chunk 

Chunk an array.

->chunk($chunk = 5, $index = null, $fail = []);

Example:

$array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

$test_1 = Anemon::eat($array)->chunk(4);
$test_2 = Anemon::eat($array)->chunk(4, 2);
$test_3 = Anemon::eat($array)->chunk(4, 11, false);

Result:

$test_1 = [
    [0, 1, 2, 3],
    [4, 5, 6, 7],
    [8, 9]
];

$test_2 = [8, 9];

$test_3 = false;

Walk 

$array = [
    'foo' => 0,
    'bar' => 1,
    'baz' => 2,
    'qux' => 3
];

Anemon::walk($array); // …

Next 

Move to the next item.

->next($skip = 0);

Example:

Anemon::walk($array)->next()->current();
Anemon::walk($array)->next(1)->current(); // skip 1 item

Previous 

Move to the previous item.

->previous($skip = 0);

Example:

Anemon::walk($array)->previous()->current();
Anemon::walk($array)->previous(1)->current(); // skip 1 item

To 

Move to the 2nd item.

->to($index);

Example:

Anemon::walk($array)->to(1)->current();

Before 

Insert before current item.

->before($value, $key = null);

Example:

$test_1 = Anemon::walk($array)->to(2)->before('#')->vomit();
$test_2 = Anemon::walk($array)->to(2)->before('#', '#key')->vomit();

Result:

$test_1 = [
    'foo' => 0,
    'bar' => 1,
    2 => '#',
    'baz' => 2,
    'qux' => 3
];

$test_2 = [
    'foo' => 0,
    'bar' => 1,
    '#key' => '#',
    'baz' => 2,
    'qux' => 3
];

After 

Insert after current item.

->after($value, $key = null);

Example:

$test_1 = Anemon::walk($array)->to(2)->after('#')->vomit();
$test_2 = Anemon::walk($array)->to(2)->after('#', '#key')->vomit();

Result:

$test_1 = [
    'foo' => 0,
    'bar' => 1,
    'baz' => 2,
    3 => '#',
    'qux' => 3
];

$test_2 = [
    'foo' => 0,
    'bar' => 1,
    'baz' => 2,
    '#key' => '#',
    'qux' => 3
];

Append 

Append an array item.

->append($value, $key = null);

Example:

$test_1 = Anemon::walk($array)->append('#')->vomit();
$test_2 = Anemon::walk($array)->append('#', '#key')->vomit();

Result:

$test_1 = [
    0 => '#',
    'foo' => 0,
    'bar' => 1,
    'baz' => 2,
    'qux' => 3
];

$test_2 = [
    '#key' => '#',
    'foo' => 0,
    'bar' => 1,
    'baz' => 2,
    'qux' => 3
];

Prepend 

Prepend an array item.

->prepend($value, $key = null);

Example:

$test_1 = Anemon::walk($array)->prepend('#')->vomit();
$test_2 = Anemon::walk($array)->prepend('#', '#key')->vomit();

Result:

$test_1 = [
    'foo' => 0,
    'bar' => 1,
    'baz' => 2,
    'qux' => 3,
    4 => '#'
];

$test_2 = [
    'foo' => 0,
    'bar' => 1,
    'baz' => 2,
    'qux' => 3,
    '#key' => '#'
];

First 

Get the first item.

->first();

Example:

$test = Anemon::walk($array)->first();

Last 

Get the last item.

->last();

Example:

$test = Anemon::walk($array)->last();

Count 

Count items.

->count($deep = false);

Example:

$test = Anemon::walk($array)->count();

Key 

Get array key by index.

->key($index, $fail = false);

Example:

$test = Anemon::walk($array)->key(1);

Index 

Get array index by key.

->index($key, $fail = false);

Example:

$test = Anemon::walk($array)->index('foo');

Alter 

This is just a shortcut for switch and case statements.

Anemon::alter($input, $replace = [], $fail = null);

Example:

$input = 'bar';

$test = Anemon::alter($input, [
    'foo' => 1,
    'bar' => 2,
    'baz' => 3
]);

Result:

$test = 2;

Constructor 

Create flexible array variable. An instance of Anemon class can be used to make the array becomes possible to be accessed as string, object or function.

Anemon($array = [], $separator = ', ');

Example:

$test = new Anemon(['foo', 'bar', 'baz']);

echo $test;
echo $test(' + ');
echo $test->join(' + ');
echo $test->{1};
echo $test->vomit(1);

Result:

foo, bar, baz

foo + bar + baz

foo + bar + baz

bar

bar

Array with null value and array with keys prefixed by a _ will be ignored by default:

$test = new Anemon([
    'foo' => 0,
    'bar' => 1,
    'baz' => 2,
    '_hidden' => '#'
], ', ');

echo $test;
echo $test(' + ');
echo $test->join(' + ');
echo $test->foo;
echo $test->_hidden;
echo $test->vomit('foo');

Result:

0, 1, 2

0 + 1 + 2

0 + 1 + 2

0

#

0

0 Comments

No comments yet.