https://github.com/camelotproject/collection
https://github.com/camelotproject/collection
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/camelotproject/collection
- Owner: CamelotProject
- License: mit
- Created: 2018-08-31T10:23:46.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-11-08T13:37:32.000Z (over 3 years ago)
- Last Synced: 2025-01-13T08:27:07.876Z (5 months ago)
- Language: PHP
- Size: 190 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Camelot Collection
==================This library provides objects and functionality to help with groups of items and data sets.
Check out the [API documentation][api-docs].
`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
```php
$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
```
```php
$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"
```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.