A Collections library for PHP.

Last update: May 2, 2022

A Library of Collections for OO Programming

Build Status

While developing and helping others develop PHP applications I noticed the trend to use PHP's arrays in nearly every task. Arrays in PHP are useful but are overused because PHP doesn't have rich standard libraries for working with common data structures and algorithms. This library hopes to fill in that gap. Undoubtably, I've made mistakes in design and implementation; hopefully more community involvement can help identify and fix such mistakes.

Requirements

Patience

This project is unstable and subject to significant changes from release to release. Stability is not a goal during this phase of development. To be clear: you are using this project at your own risk. I highly value your experience using this library and am thankful for the early adopters, but I want to emphasize that this project is highly unstable.

PHP 5.4

This project requires PHP 5.4 because PHP 5.3 is at its End Of Life (EOL). As such the project makes use of Traits, the callable type and short array syntax. Depending on how long this project takes to mature it might require PHP 5.5 for generators and improved handling of non-scalar keys in foreach loops, or maybe PHP 5.6 for ... support.

You can also use HHVM. I won't let HHVM compatibility stop me from doing what I feel is right for this library, but hopefully we can maintain compatibility.

Roadmap

There is no official roadmap for this project. I intend to release version 1.0.0 when I am confident enough in the API that it is suitable for public use. Until then, minor versions will be tagged as they are deemed helpful. This project follows semantic versioning.

How can I help?

The best way to help is to use the library (again, at your own risk) and submit issues when you find them. Over 99% of the library has been executed in a unit test but the quality of some of these tests is poor; improved tests are definitely welcome.

Can you add X structure?

Maybe. Open an issue and I'll look into it.

Why not use the existing Standard PHP Library?

The Standard PHP Library (SPL) has many problems, some of which are documented in an unfinished RFC regarding the SPL. I won't go into full details here, but essentially the SPL is not providing the standardized data structures and algorithms that I feel the PHP community needs.

What's up with the name?

This project was originally aimed to fix the SPL; as such I had named it SPL. Eventually I realized that I would do better with a different name; I picked Ardent it describes how I feel about the need for this kind of library. Unfortunately another PHP project decided to use the name after I had already picked it.

GitHub

https://github.com/morrisonlevi/Ardent
Comments
  • 1. Add priority queue.

    SPL has priority queues, but they lack a key piece of functionality: the ability to decrease the key of a queue element. A fair number of uses of priority queues depend on this. One example is Djisktra's algorithm. This class is an implementation of a pairing heap, which has performance almost as good as that of a Fibonacci heap, but is considerably simpler to implement.

    This class has a few notable differences from the SPL implementations:

    • The queue exclusively accepts objects. The reason for this is that it uses SPLObjectStorage as a way to map inserted elements with their tree objects. An elegant method for relaxing this restriction would be a nice improvment.
    • It does not implement the Iterator interface. It can if there appears to be a need, but I struggle to see the reason for it. Therefore, I'm KISSing it until proven otherwise.
    • Arbitrary elements can be removed. With the pieces in place to reprioritize elements, it seemed an easy task to add what should be a useful ability.

    One issue with this pull that I'm not thrilled with, but count as "good enough for now" is the test. It does its job, but is ugly and could use improvements from someone who's better at writing tests.

    Reviewed by mcordingley at 2015-11-06 19:02
  • 2. Let Composer Handle Autoloading

    I see that the library currently implements its own autoload function. Why not just use PSR-4 autoloading and let Composer handle this? It'll make it easier to alter this library while it is on its way to 1.0 and will make it simpler for others to contribute.

    If your concern is about performance, your current implementation is a class map. Running composer with the -o flag will generate a class map, instead of attempting to map namespaces to file system locations. Performance should be the same at that point and you'll have DRYer code, as you won't be repeating logic found elsewhere.

    Reviewed by mcordingley at 2015-11-06 18:39
  • 3. No last/first methods on sets

    There is no last or first method (to retrieve the first or last item, of course) on the set classes. This seems like it'd be something useful to have (and is present in other implementations of sets).

    Reviewed by Lusitanian at 2013-04-03 16:39
  • 4. Filter and Sort arbitrary Collections

    Is it possible to apply multiple filters on arbitrary Collections of this package? I just found a solution to apply a single filter (should be possible by using CallbackFilterIterator).

    And is there a preferred or standardised way to sort a collection of objects by a specific attribute?

    Reviewed by inkrement at 2016-02-12 22:46
  • 5. Is having main collection interfaces always extending IteratorAggregate the best option?

    Couldn't be more convenient, sometimes, to implement Traversable using Iterator?

    interface Map extends \ArrayAccess, \Countable, Enumerable { ... } -> interface Map extends \ArrayAccess, \Countable, Collection { ... }

    Reviewed by Wes0617 at 2014-11-09 00:02
  • 6. Filter Counter

    From https://github.com/morrisonlevi/Ardent/blob/master/src/Ardent/Iterator/FilteringIterator.php i notice the following code

    
        function count() {
            $i = 0;
            for ($this->rewind(); $this->valid(); $this->next()) {
                $i++;
            }
            return $i;
        }
    
    

    Why just just implement FilteringIterator::$counter has integer and just do basic increment and decrements ?

    Reviewed by olekukonko at 2013-03-24 20:29
  • 7. Method visibility

    I've been looking though this library a bit and started wondering why most methods do not have an explicit visibility declared. Is there a reason for using function foo rather than public function foo?

    There are places where I am wondering if things are intentionally public or are just missing either private or protected. For instance HashMap::areEqual. This method does not make sense as part of this the public interface of this class. An anonymous function could be used in contains. Though perhaps it is done like this to allow overriding in derivatives of HashMap?

    Reviewed by JeroenDeDauw at 2015-05-25 17:15
  • 8. Run tests on travis

    How about enabling testing with https://travis-ci.org/? :)

    You have to add a .travis.yml file (example. And enable testing for the repository here: https://travis-ci.org/profile.

    Reviewed by asm89 at 2014-01-02 20:46
  • 9. Build files for PEAR Package

    • need to update information for package.ini (Onion package builder,bundler)
    • if possible, please to move lib/bootstrap.php to other place, since lib/ will be installed to pear directory, the bootstrap.php will be put into path like /opt/local/lib/php/pear/bootstraps.php and will cause namespace pollution.
    • if lib to src, then the roles section of package.ini can be ignored.
    Reviewed by c9s at 2011-12-27 02:45
  • 10. Add HashMap::keys() to allow access to object keys

    This is more a bug report than an actual PR.

    I tried to add keys() to Map, but I have no clue how to make it work for SortedMap. This hack works for my specific case right now, so maybe including something like it is actually feasible...

    Please share your thoughts.

    Reviewed by igorw at 2013-03-14 04:42
  • 11. psr2 fixer run

    As the title says. One additional change is in the class HashMap, on line 32. The method isEmpty checks against the array count instead of using function empty()

    Reviewed by hoshsadiq at 2015-11-07 15:15
  • 12. Define Hashable Interface

    For map implementation, expecially HashMap it advantage for us to define Hashable interface, this interface only require 2 method: hashCode and isEquals modeled after Java. The advantage having this interface are the HashMap no longer take hashfunction argument and move the responsibily of compare two key is equal to the interface. It maybe overkill what do you think?

    Reviewed by syaiful6 at 2016-04-27 13:09
  • 13. Add map and filter algorithms

    PR #41 added Mappable and Filterable interfaces – this uses those to specialize functions map and filter. This is experimental as well and would like some feedback if possible.

    Reviewed by morrisonlevi at 2015-10-31 22:44
  • 14. Create Filterable and Mappable attributes

    The idea here is is that some programmers may not want to implement all of the Collection interface. So by using interface segregation we can separate them out.

    I'm not completely sold on this idea, though, which is why this is a pull request. I'm hoping to get some feedback.

    Reviewed by morrisonlevi at 2015-10-31 22:15
  • 15. Should Ardent have a TypeException?

    I would throw an EngineException (PHP7) for consistency with PHP's own type hinting. Or, better, I'd use trigger_error() for PHP5 compatibility (which would presumably result in a EngineException in PHP7)

    Reviewed by Wes0617 at 2015-04-02 22:36
PHP Integrated Query, a real LINQ library for PHP

PHP Integrated Query - Official site What is PINQ? Based off the .NET's LINQ (Language integrated query), PINQ unifies querying across arrays/iterator

May 3, 2022
🗃 Array manipulation library for PHP, called Arrayy!

?? Arrayy A PHP array manipulation library. Compatible with PHP 7+ & PHP 8+ \Arrayy\Type\StringCollection::create(['Array', 'Array'])->unique()->appen

May 8, 2022
Library for (de-)serializing data of any complexity (supports JSON, and XML)

jms/serializer Introduction This library allows you to (de-)serialize data of any complexity. Currently, it supports XML and JSON. It also provides yo

May 19, 2022
:lipstick: Scalable and durable all-purpose data import library for publishing APIs and SDKs.
:lipstick: Scalable and durable all-purpose data import library for publishing APIs and SDKs.

Porter Scalable and durable data imports for publishing and consuming APIs Porter is the all-purpose PHP data importer. She fetches data from anywhere

May 26, 2022
[READ-ONLY] Collection library in CakePHP. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Collection Library The collection classes provide a set of tools to manipulate arrays or Traversable objects. If you have ever used underscore

Mar 28, 2022
True asynchronous PHP I/O and HTTP without frameworks, extensions, or annoying code. Uses the accepted Fibers RFC to be implemented into PHP 8.1
True asynchronous PHP I/O and HTTP without frameworks, extensions, or annoying code. Uses the accepted Fibers RFC to be implemented into PHP 8.1

PHP Fibers - Async Examples Without External Dependencies True asynchronous PHP I/O and HTTP without frameworks, extensions, or annoying code behemoth

May 13, 2022
Map nested JSON structures onto PHP classes

JsonMapper - map nested JSON structures onto PHP classes Takes data retrieved from a JSON web service and converts them into nested object and arrays

May 13, 2022
Yet Another LINQ to Objects for PHP [Simplified BSD]

YaLinqo: Yet Another LINQ to Objects for PHP Online documentation GitHub repository Features The most complete port of .NET LINQ to PHP, with many add

May 9, 2022
`LINQ to Object` inspired DSL for PHP

Ginq Array handling in PHP? Be happy with Ginq! Ginq is a DSL that can handle arrays and iterators of PHP unified. Ginq is inspired by Linq to Object,

Apr 17, 2022
A repository with implementations of different data structures and algorithms using PHP

PHP Data Structures and Algorithms Data structure and Algorithm is always important for any programming language. PHP, being one of the most popular l

May 17, 2022
Leetcode for PHP, five questions a week and weekends are updated irregularly
Leetcode for PHP,  five questions a week and weekends are updated irregularly

✏️ Leetcode for PHP why do you have to sleep for a long time ,and naturally sleep after death 联系 说明 由于目前工作主要是 golang,我又新起了一个LeetCode-Go-Week项目,- Leetc

May 26, 2022
Missing data types for PHP. Highly extendable.
Missing data types for PHP. Highly extendable.

Neverending data validation can be exhausting. Either you have to validate your data over and over again in every function you use it, or you have to rely it has already been validated somewhere else and risk potential problems.

Feb 7, 2022
JsonMapper - map nested JSON structures onto PHP classes

Takes data retrieved from a JSON web service and converts them into nested object and arrays - using your own model classes.

May 10, 2021
All Algorithms implemented in Php

The Algorithms - PHP All algorithms implemented in Php (for education) These implementations are for learning purposes. They may be less efficient tha

May 22, 2022
A community driven collection of sorting algorithms in PHP

algorithms A community driven collection of sorting algorithms This repository includes a comma separated file that includes 10k numbers between 1 and

May 16, 2022
Iterators - The missing PHP iterators.

PHP Iterators Description The missing PHP iterators. Features CachingIteratorAggregate ClosureIterator: ClosureIterator(callable $callable, array $arg

May 3, 2022
Collections Abstraction library for PHP

Collections Collections Abstraction library for PHP The Collection library is one of the most useful things that many modern languages has, but for so

Dec 27, 2021
🐼 Framework agnostic package using asynchronous HTTP requests and PHP generators to load paginated items of JSON APIs into Laravel lazy collections.

Framework agnostic package using asynchronous HTTP requests and generators to load paginated items of JSON APIs into Laravel lazy collections.

Apr 30, 2022
Load files and classes as lazy collections in Laravel.

Lody Load files and classes as lazy collections in Laravel. Installation composer require lorisleiva/lody Usage Lody enables you to fetch all exist

May 21, 2022
Framework agnostic package to load heavy JSON in lazy collections.

Lazy JSON Framework agnostic package to load heavy JSON in lazy collections. Under the hood, the brilliant JSON Machine by @halaxa is used as lexer an

May 16, 2022