Mero Utils
, (*1)
Library with features that increase productivity, (*2)
Instalation with composer
- Open your project directory;
- Run
composer require mero/utils
to add Mero Utils
in your project vendor.
Type classes
Collection
Object type extended array data type with additional methods., (*3)
count()
Counts all elements in the collection., (*4)
<?php
use Mero\Utils\Collection;
$var = new Collection();
$var[] = 'First element';
$var[] = 'Second element';
$var->count(); // Will return 2
find()
Finds the first value matching the closure condition., (*5)
<?php
use Mero\Utils\Collection;
$var = new Collection([
'First element',
'Second element',
1,
4,
10,
]);
$var->find(function($it) {
return $it == 'Second element';
}); // Will return 'Second element'
findAll()
Finds all values matching the closure condition., (*6)
<?php
use Mero\Utils\Collection;
$var = new Collection([
'First element',
'Second element',
1,
4,
10,
'Second element',
'Second element',
]);
$var->findAll(function($it) {
return $it == 'Second element';
}); // Will return ['Second element', 'Second element', 'Second element']
collect()
Iterates through this collection transforming each entry into a new value using the
transform closure returning a list of transformed values., (*7)
<?php
use Mero\Utils\Collection;
$var = new Collection([1, 2, 3]);
$var->collect(function($it) {
return $it * 3;
}); // Will return [3, 6, 9]
each()
Iterates through an Collection, passing each item to the given closure., (*8)
<?php
use Mero\Utils\Collection;
$var = new Collection([1, 2, 3]);
$var->each(function($it) {
echo $it."\n";
});
// Will return:
// 1
// 2
// 3
eachWithIndex()
Iterates through an Collection, passing each item to the given closure., (*9)
<?php
use Mero\Utils\Collection;
$var = new Collection(['Element1', 'Element2', 'Element3']);
$var->eachWithIndex(function($it, $index) {
echo $index." - ".$it."\n";
});
// Will return:
// 0 - Element1
// 1 - Element2
// 2 - Element3