Git Product home page Git Product logo

bolt-collection's Introduction

Bolt Collection

travis codecov

This library provides objects and functionality to help with groups of items and data sets.

Check out the API documentation.

Bag and MutableBag

These are object-oriented implementations of arrays.

The goal of these classes:

  • Provide functionality on par with built-in array methods
  • Provide useful functionality lacking from built-in array methods
  • Provide a fluent-like experience
  • Make implementing code more readable

Examples

$arr = [
    'debug' => true,
    'color' => 'blue',
    'db' => [
        'driver' => 'sqlite',
    ],
];

$bag = Bag::from($arr)
    ->defaults([
        'debug' => false,
        'color' => 'green',
    ])
    ->replace([
        'color' => 'red',
    ])
;
$bag->get('debug'); // true
$bag->getPath('db/driver'); // "sqlite"
$bag->keys()->join(', '); // "debug, color, db"
$bag->isAssociative(); // true
$colors = MutableBag::of('red', 'blue', 'yellow')
    ->merge(['green', 'orange'])
;
$colors->isIndexed(); // true
$colors->indexOf('yellow'); // 2
$colors[2]; // "yellow"

$colors->prepend('purple');
$colors[] = 'pink';

$colors->first(); // "purple"
$colors->last(); // "pink"

$colors->shuffle()->first(); // one of the colors

$colors->chunk(2); // Bags represented as arrays:
// [ ['purple', 'red'], ['blue', 'yellow'], ['green, 'orange'], ['pink'] ]

$colors->removeFirst(); // "purple"
$colors->removeFirst(); // "red"

These examples only cover half of the functionality. See the API documentation for more.

All methods accepting a collection will accept other Bags, arrays, stdClass, and Traversable objects. This makes it very easy work with any collection-like object.

Hasn't this been done already?

Obviously others think PHP arrays suck as well and have attempted to resolve this.

Symfony's ParameterBag is a good basic map (associative arrays) container but is lacking when it comes to mutating the items around and working with lists.

Doctrine's ArrayCollection is also another, more robust, option. It works well for maps and lists, but still has limited functionality due to needing to interface with a database collection. It also has some annoyances, like getKeys() returns an array instead of another ArrayCollection instance.

bolt-collection's People

Contributors

carsonf avatar gwendolenlynch avatar largo avatar

Watchers

 avatar

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.