Giter VIP home page Giter VIP logo

coollection's Introduction

Coollection

Latest Stable Version License Code Quality Build Downloads

Coverage StyleCI PHP

Access collection items as objects properties

Coollection is Laravel's Illuminate\Collection repackaged to be used as all properties were objects:

$collection->name

$collection->addresses->first()->street_name

$collection->flatten()->cars->filter(function($car) { return $car->name == 'ferrari' })->last()->model

$countries->where('name.common', 'United States')->first()->currency->name->english;

Tightenco\Collect

To be agnostic and have complete idependence from Laravel's, but also to allow it to be used in Laravel applications, this package extends Tightenco\Collect, developed by Matt Stauffer from Tighten.

Why?

Answering with a question: which one is easier to look at?

collect(
    collect(
        collect(
            collect(
                $collection['cars']
            )->unique('constructor')['models']
        )->last()['model']
    )['colors']
)->first()['rgb']

or

$collection->cars->unique('constructor')->last()->model->colors->first()->rgb

Isn't this easier on the eyes?

collect($vimeo)->body->data->first()->metadata->connections->likes->total;

Or you prefer this one?

collect($vimeo)['body']['data'][0]['metadata']['connections']['likes']['total'];

Used with Laravel request it is useful, if you receive:

{"pagination":{"perPage":100,"pageNumber":1}}

You collect() it:

$input = coollect($request->all());

And you can just:

$input->pagination->perPage

Instead of:

$input->get('pagination')['perPage']

You can also use "dot notation" to get your items:

$input->get('pagination.perPage')

PHP Agnostic

This is an agnostic PHP package, an extraction of Illuminate\Support\Collection with all needed classes, interfaces and traits. For that if you wish just to use Illuminate's Collection, you just have to import the class Collection:

$collection = new IlluminateExtracted\Support\Collection(['my collection']); 

or use the usual helper:

$collection = collect(['my collection']); 

Documentation

It's Laravel's Collection, at full power, so you can check its docs. The only difference is that you can access items (array keys) as properties, like any other PHP object:

$collection->map($mapper)->reduce($reducer)->random()->address->street

$this->sendThanks(
    $collection->where('full_name', 'Barack Obama')->addresses->random()
);    

$countries->where('name.common', 'United States')->first()->currency->symbol;

Install

Via Composer

$ composer require pragmarx/coollection

Usage

Instantiate, the class directly or using the helper:

$c = new Coollection(['first_name' => 'Barack Obama']);

$c = coollect(['first_name' => 'Barack Obama']);

Then you use it as an object:

echo $c->first_name;

// Barack Obama


echo $c->flip()->barak_obama == 'first_name' 
    ? 'true' 
    : 'false';

// true

One word keys are case insensitive:

echo $c->rio;
echo $c->RIO;

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

coollection's People

Contributors

antonioribeiro avatar stylecibot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

coollection's Issues

Migrate to illuminate/collections

Your README states that you depend on Tightenco\Collect, but that package has been deprecated in favour of the official upstream illuminate/collections package (which is also provided by laravel/framework). Curiously, you don't even seem to use that package in favour of your own pragmarx/ia-collection, which could also be deprecated as far as I can tell.

This would be nice to reduce the amount of duplicate code I'm pulling in a Laravel project, and certainly be easier for you to maintain without having to check for and pull in any upstream changes all the time. Mostly though it's just a pet-peeve that I've always got to search for the correct Collection/Arr/Str classes to use when I just want the official ones.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.