collection
:card_index_dividers: A PHP library for representing and manipulating collections.
Top Related Projects
A Collections-only split from Laravel's Illuminate Support
Quick Overview
Ramsey/collection is a PHP library that provides a set of basic collection classes and interfaces. It offers a robust and flexible way to work with arrays and array-like objects in PHP, enhancing the language's native array functionality with object-oriented design patterns.
Pros
- Provides a consistent and intuitive API for working with collections
- Offers type-safe collections through generics support
- Implements common interfaces like
IteratorAggregate
andCountable
- Well-documented and actively maintained
Cons
- May have a slight performance overhead compared to native PHP arrays
- Requires PHP 8.1 or later, which might not be available in all environments
- Learning curve for developers accustomed to working with native PHP arrays
- Adds an additional dependency to projects
Code Examples
- Creating and using a Collection:
use Ramsey\Collection\Collection;
$collection = new Collection('string');
$collection->add('apple');
$collection->add('banana');
$collection->add('cherry');
foreach ($collection as $fruit) {
echo $fruit . "\n";
}
- Using a typed ArrayCollection:
use Ramsey\Collection\ArrayCollection;
class Person {
public function __construct(public string $name, public int $age) {}
}
$people = new ArrayCollection(Person::class);
$people->add(new Person('Alice', 30));
$people->add(new Person('Bob', 25));
$names = $people->map(fn(Person $person) => $person->name);
print_r($names->toArray());
- Filtering a collection:
use Ramsey\Collection\Collection;
$numbers = new Collection('int', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$evenNumbers = $numbers->filter(fn(int $number) => $number % 2 === 0);
print_r($evenNumbers->toArray());
Getting Started
To use ramsey/collection in your project, first install it via Composer:
composer require ramsey/collection
Then, you can start using the collection classes in your PHP code:
use Ramsey\Collection\Collection;
$collection = new Collection('string');
$collection->add('Hello');
$collection->add('World');
echo $collection->first(); // Outputs: Hello
Competitor Comparisons
A Collections-only split from Laravel's Illuminate Support
Pros of Collect
- Lightweight and focused specifically on Laravel-style collections
- Seamless integration with Laravel projects
- More frequent updates and active community support
Cons of Collect
- Limited to PHP 7.3+ (ramsey/collection supports PHP 7.2+)
- Fewer advanced data structure implementations
- Less extensive documentation compared to ramsey/collection
Code Comparison
Collection creation in Collect:
$collection = collect([1, 2, 3, 4, 5]);
Collection creation in Collection:
use Ramsey\Collection\Collection;
$collection = new Collection('int', [1, 2, 3, 4, 5]);
Summary
Collect is a streamlined library tailored for Laravel developers, offering easy integration and familiar syntax. It's ideal for projects already using Laravel or those seeking a lightweight collection implementation.
Collection provides a more comprehensive set of data structures and supports a wider range of PHP versions. It's better suited for complex projects requiring advanced collection types or stricter type enforcement.
Choose Collect for Laravel-centric development or when a simple collection implementation suffices. Opt for Collection in more diverse PHP environments or when advanced data structures are needed.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ramsey/collection
A PHP library for representing and manipulating collections.
About
ramsey/collection is a PHP library for representing and manipulating collections.
Much inspiration for this library came from the Java Collections Framework.
This project adheres to a code of conduct. By participating in this project and its community, you are expected to uphold this code.
Installation
Install this package as a dependency using Composer.
composer require ramsey/collection
Usage
Examples of how to use this library may be found in the Wiki pages.
Contributing
Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTING.md.
Coordinated Disclosure
Keeping user information safe and secure is a top priority, and we welcome the contribution of external security researchers. If you believe you've found a security issue in software that is maintained in this repository, please read SECURITY.md for instructions on submitting a vulnerability report.
ramsey/collection for Enterprise
Available as part of the Tidelift Subscription.
The maintainers of ramsey/collection and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. Learn more.
Copyright and License
The ramsey/collection library is copyright © Ben Ramsey and licensed for use under the terms of the MIT License (MIT). Please see LICENSE for more information.
Top Related Projects
A Collections-only split from Laravel's Illuminate Support
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot