Properties

$key_X

$key_X : 

Gets content at key X

Type

$index_X

$index_X : 

Gets content at key X

Type

$array

$array : 

Gets content as primitive array

Type

$index

$index : 

Gets current index

Type

$length

$length : 

Number of elements included into the collection

Type

$is_last

$is_last : 

Tests if it is the last element (while context)

Type

$is_first

$is_first : 

Tests if it is the first element (while context)

Type

$is_last_but_one

$is_last_but_one : 

Tests if it is the last but one element (while context)

Type

$last

$last : 

Gets last element

Type

$first

$first : 

Gets the first element

Type

$last_but_one

$last_but_one : 

Gets the last but one element

Type

$shift

$shift : 

Takes the first element and remove it from the collection

Type

$pop

$pop : 

Takes the last element and remove it from the collection

Type

$random

$random : 

Gets randomly one element

Type

$shuffle

$shuffle : 

Gets new collection with the same content as current one, but into shuffle order.

Type

$join

$join : 

Concatenate all element into a string if each element has toString ethod or are primitive type.

Type

$implode

$implode : 

Same as join magic attribute

Type

$max

$max : 

Gets max numeric value contained into collection

Type

$min

$min : 

Gets min numeric value contained into collection

Type

$current

$current : 

Gets current element

Type

$key

$key : 

Gets index of current element

Type

$next

$next : 

Place index on the next element

Type

$rewind

$rewind : 

Rewind :-)

Type

$valid

$valid : 

Is there another element after current one?

Type

$value

$value : mixed

Type

mixed — Primitive value

$count

$count : integer

Contains number of items

Type

integer

$position

$position : integer

Contains current position/

Type

integer

Methods

__get()

__get( $name) : mixed

Magic getters engine.

By default, return internal O::$value attribute.

Parameters

$name

Returns

mixed

__toString()

__toString() : string

Collection into string context.

When used into string context, this method convert current number of item into string.

Example:

$a = new A();
$a->add('one')->add(two);
echo $a; // '2'

Returns

string

__set()

__set( $name,  $value)

Parameters

$name
$value

__construct()

__construct( $arr)

Parameters

$arr

getIterator()

getIterator()

current()

current()

key()

key()

next()

next()

rewind()

rewind()

valid()

valid()

count()

count()

take()

take(mixed $idx) : mixed

Gets item having given index. Shorthand for `take` method.

Parameters

mixed $idx

N or integer

Returns

mixed

get()

get(mixed $idx) : mixed

Gets item having given index. Shorthand for `take` method.

Parameters

mixed $idx

N or integer

Returns

mixed

at()

at(mixed $idx) : mixed

Alias of take() method

Parameters

mixed $idx

N or integer

Returns

mixed

exist()

exist(mixed $idx) : boolean

Tests whether given index exists.

This checks if given index exists into the collection.

$a = new A();
$a->add('foo');
$a->add('bar');
$a->add('thing');
$a->exist(6); // false
$a->exist(2); // true

Parameters

mixed $idx

Value of index, as integer-like

Throws

\InvalidArgumentException

If given index is not integer-like value.

Returns

boolean

has()

has(mixed $thing) : boolean

Checks whether collection has given thing or not.

Checks whether given thing is available into the collection.

Example:

$a = new A();
$a->add('foo')->add('bar')->add('thing');
$a->has('bar'); // true
$a->has('other'); // false

Parameters

mixed $thing

The element to find.

Returns

boolean

add()

add(mixed $thing) : \Malenki\Bah\A

Adds new element into the current collection.

Adds new element at the end of the collection.

Example:

$a = new A();
$a->add('foo')->add('bar')->add('thing');
var_dump($a->array); // 'foo', 'bar', 'thing'

Parameters

mixed $thing

The foo element to add…

Returns

\Malenki\Bah\A

push()

push(mixed $thing) : \Malenki\Bah\A

Adds new element into the current collection (Alias).

Parameters

mixed $thing

The foo element to add…

Returns

\Malenki\Bah\A

delete()

delete(mixed $idx) : \Malenki\Bah\A

Deletes element at given position.

This delete for current position an item at position pos. So, returned object is same as current one.

Example:

$a = new A();
$a->add('one')->add('two')->add('three');
echo $a->delete(1)->join(', '); // 'one, three'

Parameters

mixed $idx

An integer-like value for position

Throws

\InvalidArgumentException

If given position is not an integer-like value.

\OutOfRangeException

If given position does not exist.

Returns

\Malenki\Bah\A

remove()

remove(mixed $idx) : \Malenki\Bah\A

Removes element at given position (Alias).

Parameters

mixed $idx

An integer-like value for position

Throws

\InvalidArgumentException

If given position is not an integer-like value.

\OutOfRangeException

If given position does not exist.

Returns

\Malenki\Bah\A

rm()

rm(mixed $idx) : \Malenki\Bah\A

Deletes element at given position (Alias).

Parameters

mixed $idx

An integer-like value for position

Throws

\InvalidArgumentException

If given position is not an integer-like value.

\OutOfRangeException

If given position does not exist.

Returns

\Malenki\Bah\A

del()

del(mixed $idx) : \Malenki\Bah\A

Deletes element at given position (Alias).

Parameters

mixed $idx

An integer-like value for position

Throws

\InvalidArgumentException

If given position is not an integer-like value.

\OutOfRangeException

If given position does not exist.

Returns

\Malenki\Bah\A

replace()

replace(mixed $idx, mixed $thing) : \Malenki\Bah\A

Replaces element at given position.

Changes one element of the collection by a new one giving a position.

Example:

$a = new A();
$a->add('one')->add('two')->add('three');
$a->replace(1, 'deux')->join(', '); // 'one, deux, three'

Parameters

mixed $idx

An integer-like value for position

mixed $thing

The thing that replaces foo… :)

Throws

\InvalidArgumentException

If index is not an integer-like value

\OutOfRangeException

If position does not exist

Returns

\Malenki\Bah\A

implode()

implode(mixed $sep) : \Malenki\Bah\S

Joins all elements into the collection together as a string object.

All elements are converted to sstring, if possible, and joined side by side with given separator.

If some elements are array or A or H object, then they are convert using same method and same séarators.

Example:

$a = new A();
$a->add('one');
$a->add(array('two', 'three'));
$a->add(new S('four'));
$a->add(new A(array('five', 'six')));
$a->add(new H(array('seven', 'eight')));
$a->add(new C('9'));
$a->add(new N(10));
$a->add(11);

echo $a->implode(', '); // 'one, two, three, four, five, six, seven, eight, 9, 10, 11'

Parameters

mixed $sep

String-like value as separator

Throws

\InvalidArgumentException

If separator is not string-like value

\RuntimeException

If at least one item contains into collection or sub-collection cannot be converted to a string.

Returns

\Malenki\Bah\S

join()

join(mixed $sep) : \Malenki\Bah\S

Joins all elements into the collection together as a string object (Alias) .

Parameters

mixed $sep

String-like value as separator

Throws

\InvalidArgumentException

If separator is not string-like value

\RuntimeException

If at least one item contains into collection or sub-collection cannot be converted to a string.

Returns

\Malenki\Bah\S

pad()

pad(mixed $size, mixed $value) : \Malenki\Bah\A

Fill the collection with given element on given size.

If current collection already has values, then they are kept.

Example:

$a = new A(array('foo','bar'));
$a->pad(5); // 'foo', 'bar', null, null, null
$a->pad(5, 'thing'); // 'foo', 'bar', 'thing', 'thing', 'thing'

Parameters

mixed $size

An integer-like value for the size

mixed $value

A value. If not given, null is used.

Returns

\Malenki\Bah\A

find()

find(mixed $what) : \Malenki\Bah\A

find

Parameters

mixed $what

Expression as a string-like value

Throws

\InvalidArgumentException

If expression is not string-like value.

\InvalidArgumentException

If test use negative number, they are not negative number as index into collection.

Returns

\Malenki\Bah\A

map()

map(callable $func) : \Malenki\Bah\A

Applys given function arg on every item of the collection.

Given argument must be a callable function. This function accept as argument item of collection.

The result is returned into new \Malenki\Bah\A instance.

Example:

$cube = function ($n) {
    return $n * $n * $n;
};

$a = new A(range(1, 5));
$a->map($cube)->array; // array(1, 8, 27, 64, 125)

Parameters

callable $func

Throws

\InvalidArgumentException

If param is not callable

Returns

\Malenki\Bah\A

walk()

walk( $func,  $other)

Parameters

$func
$other

filter()

filter( $func)

Parameters

$func

random()

random(integer $n) : \Malenki\Bah\A

Gets random items from the collection.

Parameters

integer $n

Number of item to take.

Throws

\InvalidArgumentException

If number of items is not an integer-like value.

\InvalidArgumentException

If number of items is less than one.

\RuntimeException

If number of items to take is greater than total available items.

Returns

\Malenki\Bah\A

diff()

diff(mixed $arr) : \Malenki\Bah\A

Returns elements not in common into current collection and given one.

This method takes one argument: an array-like collection to compare with current one and find elements not in common into second.

Given argument can be \Malenki\Bah\A object, \Malenki\Bah\H object or simple arry.

Example:

$a = new A();
$a->add('one')->add('two');
$b = new A();
$b->add('three', 'one');
$a->diff($b); // 'two'

Parameters

mixed $arr

An array-like collection (array, A or H object)

Throws

\InvalidArgumentException

If given argument is not an array-like value

Returns

\Malenki\Bah\A

inter()

inter(mixed $arr) : \Malenki\Bah\A

Gets commons elements into currrent collection and given collection.

Same elements found into current collection and given collection are returned into new \Malenki\Bah\A instance.

Given collection can be simple array, \Malenki\Bah\A object or \Malenki\Bah\H object.

$a1 = new A();
$a1->add('blue')->add('white')->add('red');
$a2 = new A();
$a2->add('green')->add('white')->add('red');

var_dump($a1->inter($a2)->array); // array('white', 'red')
var_dump($a2->inter($a1)->array); // array('white', 'red')

Parameters

mixed $arr

Collection (array or object)

Throws

\InvalidArgumentException

If argument is not array-like value

Returns

\Malenki\Bah\A

merge()

merge() : \Malenki\Bah\A

Merge current collection with other(s) .

You can merge with current collection any number of array-like variables.

Example:

$a = new A();
$a->add('one')->add('two');

$b = new A();
$b->add('three');

$c = new A();
$c->add('four')->add('five');

$a->merge($b, $c); // has 'one', 'two', 'three', 'four', 'five'

Throws

\InvalidArgumentException

If at least one collection has bad type

Returns

\Malenki\Bah\A

concat()

concat() : \Malenki\Bah\A

Merge current collection with other(s) (Alias).

Throws

\InvalidArgumentException

If at least one collection has bad type

Returns

\Malenki\Bah\A

chunk()

chunk(\Malenki\Bah\N|integer $size) : \Malenki\Bah\A

Divides current collection into subcollection having given size.

Collection is splitted into several parts, each of them has size given in argument, the last can have small size.

Example:

$a = new A(array(1, 2, 3, 4, 5, 6, 7, 8, 9));
$a->chunk(3); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
$a->chunk(4); // [[1, 2, 3, 4], [5, 6, 7, 8], [9]]

Parameters

\Malenki\Bah\N|integer $size

Chunk’s size, as an integer-like value.

Throws

\InvalidArgumentException

If argument is not integer-like value.

\InvalidArgumentException

If given size is less than one.

Returns

\Malenki\Bah\A

slice()

slice( $offset,  $length)

Parameters

$offset
$length

search()

search(mixed $foo) : mixed

Search index of the given element.

This returns the first index found for the given element if there are several identicals.

If no element found, then returns null.

If element is found, then N object is returned.

Example:

$a = new A();
$a->add('zéro')->add('un')->add('deux');
echo $a->search('un'); // '1'

Parameters

mixed $foo

The element to find

Returns

mixed

hasRange()

hasRange(mixed $arr) : boolean

Checks whether current collection has given range inside.

The given range range must be included into the collection without change, into the same order, without break.

Example:

$a = new A(array('un', 'deux', 'trois', 'quatre'));
$a->hasRange(array('deux', 'trois')); // true
$a->hasRange(array('deux', 'un')); // false

Parameters

mixed $arr

Array-like collection

Throws

\RuntimeException

If given collection is a void array (arrays are allowed, but not void)

\InvalidArgumentException

If given argument is not an array-like value.

Returns

boolean

zip()

zip() : \Malenki\Bah\A

Combines provided collections with current one.

This works like array_combine() PHP function, but it can use many collections or arrays.

If some collection have less contents than others, then into combined result, void value are replaced by null.

Examples:

$a = new A(array('un', 'deux', 'trois'));
$b = new A(array('a', 'b', 'c'));
$c = new A(array(1, 2, 3));

$a->zip($b, $c);

// will give this structured A objects:
// Row 1: 'un', 'a', 1
// Row 2: 'deux', 'b', 2
// Row 3: 'trois', 'c', 3

$a = new A(array('un', 'deux', 'trois'));
$b = new A(array('a', 'b'));
$c = new A(array(1, 2, 3));

$a->zip($b, $c);

// will give this structured A objects:
// Row 1: 'un', 'a', 1
// Row 2: 'deux', 'b', 2
// Row 3: 'trois', null, 3

Throws

\InvalidArgumentException

If at least one collection is not array, or \Malenki\BahA object or \Malenki\Bah\H object

Returns

\Malenki\Bah\A

mustBeStringOrScalar()

mustBeStringOrScalar(mixed $arg, string $arg_name) : void

Checks whether given value is string-like or scalar type.

Value must be of string-like type (string or object having __toString() method) or a basic scalar type.

The aim of this method is only to raise \InvalidArgumentException if given value has not the good type.

Parameters

mixed $arg

The value to test.

string $arg_name

Optional name of the value, used into exception’s message.

Throws

\InvalidArgumentException

If value’s type is not valid

mustBeString()

mustBeString(mixed $arg, string $arg_name) : void

Checks whether given value is string-like type.

A string-like value is string or object having __toString() method.

The aim of this method is only to raise \InvalidArgumentException if given value has not the good type.

Parameters

mixed $arg

The value to test.

string $arg_name

Optional name of the value, used into exception’s message.

Throws

\InvalidArgumentException

If value’s type is not valid

mustBeInteger()

mustBeInteger(mixed $arg, string $arg_name) : void

Checks whether given value is integer-like type.

An integer-like value is integer or \Malenki\Bah\N object.

The aim of this method is only to raise \InvalidArgumentException if given value has not the good type.

Parameters

mixed $arg

The value to test.

string $arg_name

Optional name of the value, used into exception’s message.

Throws

\InvalidArgumentException

If value’s type is not valid

mustBeFloat()

mustBeFloat(mixed $arg, string $arg_name) : void

Checks whether given value is float-like type.

A float-like value is float or \Malenki\Bah\N object.

The aim of this method is only to raise \InvalidArgumentException if given value has not the good type.

Parameters

mixed $arg

The value to test.

string $arg_name

Optional name of the value, used into exception’s message.

Throws

\InvalidArgumentException

If value’s type is not valid

mustBeDouble()

mustBeDouble(mixed $arg, string $arg_name) : void

Checks whether given value is double-like type.

A double-like value is string or \Malenki\Bah\N object.

The aim of this method is only to raise \InvalidArgumentException if given value has not the good type.

Parameters

mixed $arg

The value to test.

string $arg_name

Optional name of the value, used into exception’s message.

Throws

\InvalidArgumentException

If value’s type is not valid

mustBeNumeric()

mustBeNumeric(mixed $arg, string $arg_name) : void

Checks whether given value is numeric-like type.

A numeric-like value is numeric (integer, float or double) or \Malenki\Bah\N object.

The aim of this method is only to raise \InvalidArgumentException if given value has not the good type.

Parameters

mixed $arg

The value to test.

string $arg_name

Optional name of the value, used into exception’s message.

Throws

\InvalidArgumentException

If value’s type is not valid

mustBeArray()

mustBeArray(mixed $arg, string $arg_name) : void

Checks whether given value is an array-like type.

A array-like value is an array or \Malenki\Bah\A object.

The aim of this method is only to raise \InvalidArgumentException if given value has not the good type.

Parameters

mixed $arg

The value to test.

string $arg_name

Optional name of the value, used into exception’s message.

Throws

\InvalidArgumentException

If value’s type is not valid

mustBeHash()

mustBeHash(mixed $arg, string $arg_name) : void

Checks whether given value is a hash-like type.

A hash-like value is an array having named-index or \Malenki\Bah\H object.

The aim of this method is only to raise \InvalidArgumentException if given value has not the good type.

Parameters

mixed $arg

The value to test.

string $arg_name

Optional name of the value, used into exception’s message.

Throws

\InvalidArgumentException

If value’s type is not valid

mustBeArrayOrHash()

mustBeArrayOrHash(mixed $arg, string $arg_name) : void

Checks whether given value is an array-like or hash-like type.

The aim of this method is only to raise \InvalidArgumentException if given value has not the good type.

Parameters

mixed $arg

The value to test.

string $arg_name

Optional name of the value, used into exception’s message.

Throws

\InvalidArgumentException

If value’s type is not valid

mustBeCallable()

mustBeCallable(mixed $arg, mixed $arg_name) : void

Checks whether the given argument is callable.

This is usefull to test is an argument can be call as a function. If this argument is not callable, then this raises an \InvalidArgumentException.

Parameters

mixed $arg

The value to test

mixed $arg_name

Optional name of the value, used into error message

Throws

\InvalidArgumentException

If arg is not callable

toSimpleArray()

toSimpleArray( $arr)

Parameters

$arr

_length()

_length()

_isLastButOne()

_isLastButOne()

_isLast()

_isLast()

_isFirst()

_isFirst()

_lastButOne()

_lastButOne()

_last()

_last()

_first()

_first()

_unique()

_unique() : \Malenki\Bah\A

Removes any duplicate entries.

This method removes all duplicate entries, so resulting collection has only unique items.

Example:

$a = new A();
$a->add('foo');
$a->add('bar');
$a->add('foo');
$a->add('thing');
var_dump($a->unique->array); // has: 'foo', 'bar', 'thing'

Returns

\Malenki\Bah\A

_uniq()

_uniq() : \Malenki\Bah\A

Removes any duplicate entries (Alias).

Returns

\Malenki\Bah\A

_shift()

_shift() : mixed

Shift.

Removes first element of the current collection and returns it.

This is runtime part of magic getter A::$shift.

Example:

$a = new A();
$a->add('one')->add('two')->add('three');
echo count($a); // 3
echo $a->shift; // 'one'
echo count($a); // 2

Throws

\RuntimeException

If current collection is void.

Returns

mixed

_pop()

_pop() : mixed

Pop.

Removes last element of the current collection and returns it.

This is runtime part of magic getter A::$pop.

Example:

$a = new A();
$a->add('one')->add('two')->add('three');
echo count($a); // 3
echo $a->pop; // 'three'
echo count($a); // 2

Throws

\RuntimeException

If current collection is void.

Returns

mixed

_maxOrMin()

_maxOrMin( $type)

Parameters

$type

_array()

_array() : array

Converts current collection as a simple array, recursively.

This method converts all current collection as a simple array primitive type, and all sub-collectionis included into current one are also converted to array: this is recursive.

Example:

$a = new A();
$a->add('foo')->add('bar');
$a->add(new A(array('other', 'thing'))) // works with H too
$a->array; // array('foo', 'bar', array('other', 'thing'))

Returns

array

_arr()

_arr() : array

Converts current collection as a simple array, recursively (Alias).

Returns

array

_shuffle()

_shuffle() : \Malenki\Bah\A

Returns a shuffled copy of the current collection.

This runtime part of magic getter A::$shuffle randomize collection’s content to a new collection object.

Example:

$a = new A(array('one', 'two', 'three'));
echo $a->shuffle->join(', '); // 'three, one, two' or other order…

Returns

\Malenki\Bah\A

_reverse()

_reverse() : \Malenki\Bah\A

Reverses items’ order as new collection.

Returns new collection having current items’ order reversed.

Example:

$a = new A();
$a->add('one')->add('two')->add('three');
var_dump($a->reverse->array); // 'three', 'two', 'one'

This is runtime par of magic getter.

Returns

\Malenki\Bah\A

_sort()

_sort() : \Malenki\Bah\A

Sort elements of the collection.

Sort all elements of the collection by returning new \Malenki\Bah\A object.

Example:

$a = new A(array('blue', 'white', 'red'));
echo $a->sort->join(', '); 'blue, red, white'

This is runtime part of magic getter.

Returns

\Malenki\Bah\A

_flatten()

_flatten() : \Malenki\Bah\A

Flatten the current collection.

If collection contains other arrays or A objects or H objects, then this method will take all this content to create unqiue collection having one dimension.

Example:

$a = new A(array('one', array('two', 'three', new A(array('four', 'five')))));
var_dump($a->flatten->array); // has: 'one', 'two', 'three', 'four', 'five'

Returns

\Malenki\Bah\A

_flat()

_flat() : \Malenki\Bah\A

Flatten the current collection (Alias).

Returns

\Malenki\Bah\A

mergeEngine()

mergeEngine(array $args) : \Malenki\Bah\A

Merge current collection with other(s) .

This is runtime part of A::merge() and A::concat().

Parameters

array $args

All collection to merge into array

Throws

\InvalidArgumentException

If at least one collection has bad type

Returns

\Malenki\Bah\A