Download the PHP package seboettg/collection without Composer
On this page you can find all versions of the php package seboettg/collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download seboettg/collection
More information about seboettg/collection
Files in seboettg/collection
Package collection
Short Description Collection is a set of useful PHP wrapper classes for arrays, similar to Java Collection. Contains ArrayList, Stack, Queue.
License MIT
Informations about the package collection
Collection
Collection is a set of useful wrapper classes for arrays, similar to Java's or Kotlin's collection packages.
Table of Contents
- Versions
- Installing Collection
- Lists
- Getting started
- Iterate over lists
- List operations
- Map elements
- Filter elements
- Logical operations
- forEach
- sorting
- Maps
- Getting started
- Access elements
- Manipulate
- Map elements
- Filter map entries
- Combining Lists and Maps
- Stack
- Queue
- Contribution
Versions
Since Version 4.0 you need PHP 7.4 or higher to use this library. Since Version 2.1 you need PHP 7.1 to use Collection library. Previous versions are running from PHP 5.6 upwards.
Installing Collection
The recommended way to install Collection is through Composer.
Next, run the Composer command to install the latest stable version of Collection:
After installing, you need to require Composer's autoloader:
You can then later update Collection using composer:
Lists
List is an ordered collection with access to elements by indices – integer numbers that reflect their position. Elements can occur more than once in a list. In other words: a list can contain any number of equal objects or occurrences of a single object. Two lists are considered equal if they have the same sizes and structurally equal elements at the same positions.
Lists are completely new implemented for version 4.0. The handling is much more oriented on a functional approach. Further more methods for associative arrays are moved to map.
Getting started
Output
You also create a list from an existing array
Output
As you may notice, this will reset the array keys
You can also create an empty List:
output
Iterate over lists
Output:
or
output
List operations
You may add the elements of another list to a list by using plus
:
output
The same operation is applicable with arrays, with the same result:
You can also subtract the elements of another list or any iterable
using minus
:
output
To get the intersection of two lists (or an iterable), you can use the intersect
method:
output
To get a list containing distinct elements, use distinct
:
output
Map all elements of a list
If you need to modify all elements in a list, you can do it easily by using the map
method:
There is also a mapNotNull
method that eliminates null
values from the result:
Filter elements in a list
The filter
method returns a list containing only elements matching the given predicate.
Logical operations
With the methods any
and all
you can check whether all elements (all) or at least one element (any) match a predicate.
forEach method
With the forEach method you can apply a closure or lambda functions on each element.
output:
Sorting a list
Implement the Comparable interface
Create a comparator class
Sort your list
sort your list using a custom order
Maps
A Map stores key-value pairs; keys are unique, but different keys can be paired with equal values. The Map interface provides specific methods, such as access to value by key, searching keys and values, and so on.
Getting started
A Map is a collection of keys that are paired with values. Therefore, to create a Map you need pairs first:
output
You can also create an empty Map:
output
Access elements
you are also able to get all map entries as a list of pairs
output
Manipulate a map
Map elements
The signature of given transform function for mapping must have either a Pair
parameter or a key
and a value
parameter.
The map function always returns a list of type ListInterface
:
output
You get the same result with a key-value signature:
Filter entries of a map
You may filter for elements by this way:
or by this way:
Combining Lists and Maps
There are a lot of opportunities to use lists and maps in real world scenarios with a lot of advantages e.g. less boilerplate code and better code readability.
The following json file represents a customer file that we want to use for processing.
customer.json
We would like to get a map that associates the customer id with respective objects of type Customer
and we want to apply a filter
so that we get only customers with lastname Doe.
output
Another example: Assuming we have a customer service with a getCustomerById
method.
We have a list of IDs with which we want to request the service.
output
Stack
A stack is a collection of elements, with two principal operations:
- push, which adds an element to the collection, and
- pop, which removes the most recently added element that was not yet removed.
An Stack is a LIFO data structure: last in, first out.
Examples
push, pop and peek
search
The search function returns the position where an element is on this stack. If the passed element occurs as an element in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost element on the stack is considered to be at distance 1. If the passed element does not occur in the stack, this method returns 0.
Queue
A queue is a collection in which the elements are kept in order. A queue has two principle operations:
- enqueue
- dequeue
Examples
Contribution
Fork this Repo and feel free to contribute your ideas using pull requests.