2017 © Pedro Peláez
 

library php-collection-helper

Simple array and object-oriented collection

image

softboxlab/php-collection-helper

Simple array and object-oriented collection

  • Thursday, June 15, 2017
  • by tarcisiojr
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Build Status codecov, (*1)

Simple PHP Collection and array helper

Simple collection class to work with arrays on object-oriented paradigm and some array (collection) helpers with standardization of the parameters (data first, callback later, like javascript)., (*2)

Collection

In this definition, our collection is Immutable, which means that every operation should return a new collection, keeping the original collection intact., (*3)

$collection = new Collection([1, 2, 3, 4]);
$newCollection = $collection->map(function ($item) {
    return $item * 2;
});

print_r($newCollection->all()); // [2, 4, 6, 8]
print_r($collection->all()); // [1, 2, 3, 4]

Collection helper

This is the base support to the Collection class. All the methods are implemented here., (*4)

List of available methods

  • map($data, $callback ($value, $key));
    • Map all the items of data with the provided callback. The callback will receive both value's and key's of the array.
  • filter($data, $callback($value, $key), $keepKeys);
    • Filter the given array with the provided callback. Only the items when the callback return's true will be returned on the filtered data. If false is passed to keepKeys parameter, the keys will be reseted.
  • reduce($data, $callback($value, $key), $initialValue);
    • Reduce's the given data with the given callback.
  • transform($data, $changes, $delimiter);
    • Transform the keys (change) of the given $data array with the $changes array. To navigate along the array, use the Laravel's dot syntax. If $delimiter provided, it changed the Laravel's dot syntax separator.
  • transformArray($data, $changed, $delimiter);
    • Same as transform, but should be used on a list, which will apply on every item. Same as map with transform.

Transformer class

Simple array transformer class, which convert all the keys by new ones. Something like a from-to where the all the keys of the given array will be replaced the by the value of the given replaced array when the key matches., (*5)

Example:, (*6)

$transformer = new Transformer(['namae' => 'nombre']);
$data = ['namae' => 'William'];
$transformedData = $transformer->transform($data);

print_r($transformedData); // ['nombre' => 'William'];

Contributing

Feel free to make any pull request you judge necessary!, (*7)

The Versions

15/06 2017

dev-master

9999999-dev

Simple array and object-oriented collection

  Sources   Download

MIT

The Development Requires

by William Johnson dos Santos Okano

15/06 2017

v0.0.1

0.0.1.0

Simple array and object-oriented collection

  Sources   Download

MIT

The Development Requires

by William Johnson dos Santos Okano