Skip to content
transprime-researchPublic

About

PHP array with smartness

Resources

Stars

22 stars

Watchers

3 watching

Forks

Latest commit

Β 

History

134 Commits

Folders and files

Repository files navigation

Build Status Latest Stable Version Total Downloads Latest Unstable Version Latest Monthly Downloads License

About Arrayed

Simple PHP Array(ed) in object oriented way wrapping PHP Arrays in a consistent manner.

No advanced stuff, just wrap PHP array_* functions and a little more. Do it Like a PRO πŸ†—

Looking for PHP Array on Steroid? See: https://laravel.com/docs/collections

Quick Usage

arrayed(1, 2, 'ninja')
    ->filter(fn($val) => is_int($val)) // [1,2]
    ->map(fn($val) => $val + 1) // [2, 3]
    ->flip() // [0, 1]
    ->values() // [0, 1]
    ->sum(); // 1

Instead of:

$result = array_filter([1, 2, 'ninja'], fn($val) => is_int($val));
$result = array_map(fn($val) => $val + 1, $result);
$result = array_flip($result);
$result = array_values($result);
$result = array_sum($result);

PS: You can still use the old function() { return v; }, fn() is the new short arrow function in PHP 7.4+ See: https://www.php.net/manual/en/functions.arrow.php

Installation

composer require transprime-research/arrayed

Requirement

Minimum Requirement

  • PHP 7.2 +

  • Composer

  • For using collect() method, requires illuminate\support >= 5.5

Additionally on Laravel App, if arrayed.php's config file doesn't get added automatically then run php artisan vendor:publish --tag=arrayed after installation.

Other Usages

Arrayed can be instantiated in 3 ways:

use Transprime\Arrayed\Arrayed;

// Nifty
arrayed(1, 2)->count();

// Easier
Arrayed::on(1, 2)->count();

// Normal with (new instance)
(new Arrayed(1,2))->count();

Initial values can be passed in two ways:

//Non associative
arrayed(1, 2);

//OR
arrayed([1, 2]);

// For associative array, only this way
arrayed(['a' => 1, 'b' => 2]); 

With Laravel & Laravel Collection

Laravel Collections

New: collect() method πŸŽ‰

arrayed(1,2)->collect(); // instance of Illuminate/Support/Collection
arrayed(1,2)->collect(3, 4); //merged with first one to give [1, 2, 3, 4] 

In the future, changing the default Collection class will possible by editing config/arrayed.php's collection_class value

Others:

collect(arrayed(1, 2, 3, 4));

// Or
new Collection(arrayed(1, 2, 3, 4));

// Or
Collection::make(arrayed(1, 2, 3, 4));

Laravel Response accepts Arrayed:

response()->json(arrayed(1, 2, 3)->flip());

Special methods

New πŸŽ‰ tap() method allows other actions on the last resulting Arrayed instance without mutating the last Arrayed result:

arrayed(1, 2, 3)
    ->tap(function ($arrd) {
        logger('Array has '.$arrd->count());
    });

Others

If any operation normally returns an array, the return value will give Arrayed instance so that other methods can be chained on them otherwise a non-array value is returned as can be seen that sum() returns an integer in the example below:

Example:

arrayed(['a' => 1, 'b' => 2])
    ->values() // returns array, we can chain
    ->sum(); // returns an integer, we cannot chain

You can work on a result (if its an array'ed value) by passing a closure/callable function to result() method:

arrayed(['a' => 'name', 'b' => 'age'])
    ->values()
    ->result(fn($val) => implode(',', $val)); //'name,age'

//Or

arrayed(['a' => 'name', 'b' => 'age'])
    ->values()(fn($val) => implode(',', $val)); //'name,age'

Get the original array data with raw() method

arrayed([1, 2])->raw(); //[1,2]

Piped calls

As at now not all array_* functions have been implemented. pipe() method helps to call custom function on the array result.

Such as array_unique used in this way:

arrayed(['a' => 'www', 'b' => 'dot', 'c' => 'www'])
    ->pipe('array_unique') // data is piped forward to `array_unique`
    ->flip()
    ->values()(); //['a', 'b']

The pipe method makes use of Piper - A PHP functional pipe'ing See \Transprime\Arrayed\Tests\ArrayedTest

Proxied calls

array_* methods that are not yet implemented are automatically proxied to call an array method with the assumption that they accept initial array first. Example is this:

// ->combine() method is not yet implemented

arrayed(['a', 'b'])
    ->combine(['name', 'data'])
    ->result(); //['a' => 'name', 'b' => 'data']

Coming Soon

  • Implement other array_* methods

  • pipe into Collection with collectPipe

use Illuminate\Support\Collection;

arrayed(1,2,3)->collectPipe(function (Collection $collected) {
    return $collected->take(2)->all();
})->keys();

Api implementation to be decided

APIs

These are the API's available:

static Arrayed::on(...$values): ArrayedInterface; //new instance of Arrayed

Arrayed::map($callback): ArrayedInterface;

Arrayed::filter($callback = null, int $flag = 0): ArrayedInterface;

Arrayed::reduce($function, $initial = null): ArrayedInterface;

Arrayed::merge(array $array2 = null, ...$_): ArrayedInterface;

Arrayed::mergeRecursive(...$_): ArrayedInterface;

Arrayed::flip(): ArrayedInterface;

Arrayed::intersect(array $array2, ...$_): ArrayedInterface;

Arrayed::values(): ArrayedInterface;

Arrayed::keys($overwrite = true): ArrayedInterface;

Arrayed::offsetGet($offset);

Arrayed::offsetSet($offset, $value): ArrayedInterface;

Arrayed::offsetUnset($offset): ArrayedInterface;

Arrayed::sum(): int;

Arrayed::contains($needle, bool $strict = false): bool;

Arrayed::isArray(): bool;

Arrayed::keyExists($key): bool;

Arrayed::offsetExists($offset): bool;

Arrayed::empty(): bool;

Arrayed::count(): int;

Arrayed::pipe(callable $action, ...$parameters);

Arrayed::result(callable $callable = null);

Arrayed::raw(): array;

Arrayed::initial(): array; // Deprecated, use raw() instead

Arrayed::tap(Closure $closure): ArrayedInterface;

Arrayed::copy(): ArrayedInterface;

Arrayed::collect(...$with): array;

// Other Array_* methods

Arrayed::changeKeyCase(int $case = null): ArrayedInterface;

Arrayed::chunk(int $size, bool $preserve_keys = false): ArrayedInterface;

Arrayed::column($column, $index_key = null): ArrayedInterface;

Arrayed::countValues(): ArrayedInterface;

Arrayed::diffAssoc(array $array2, array ...$_): ArrayedInterface;

Arrayed::diff(array $array2, array ...$_): ArrayedInterface;

Arrayed::reverse(bool $preserve_keys = false): ArrayedInterface;

Arrayed::diffUassoc(callable $key_compare_func, array $array2, array ...$_): ArrayedInterface;

Arrayed::diffKey(array $array2, array ...$_): ArrayedInterface;

Additional Information

This package is part of a series of "Code Dare"

See other packages in this series here:

Similar packages

Licence

MIT (See LICENCE file)

About

PHP array with smartness

Resources

Stars

22 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages