pyther/linquo

There is no license information available for the latest version (v0.1.0) of this package.

Sequence with Linq like support for PHP

v0.1.0 2025-05-31 11:07 UTC

This package is auto-updated.

Last update: 2025-06-12 18:44:32 UTC


README

Linquo is a lightweight PHP library to support LINQ-like operations on sequences such as arrays with the following features:

  • support all .Net LINQ methods up to .Net 10
  • brings additional methods (like toJson)
  • based on Lazy Evaluation
  • fast execution
  • support for Comparer like StringComparer::$OrdinalIgnoreCase
  • no external dependencies

let's start with a simple example:

$people = [array of peoples];

// give me all adults names in alphabetical order as an array
$names = from($people)
    ->where(fn($e) => $e->age >= 18)   // only persons of 18 or older  
    ->orderBy(fn($e) => $e->name)      // order by name
    ->select(fn($e) => $e->name)       // select the name only 
    ->toArray();                       // convert to array

and a more complex one:

echo $books
    ->where(fn($e) => $e->year > 1850)  // all books after 1850
    ->orderBy(fn($e) => $e->year)       // sort by year
    ->join(                             // join with ...  
        $authors,                       // ... authors ...
        fn($book) => $book->authorId,   // ... where the books author id ...
        fn($author) => $author->id,     // ... matches the authors id.
        fn($book, $author) => "{$book->title} by {$author->name} in {$book->year}"
    )->toJson();                        // and return as json

results to:

[
    "Men and Women by Robert Browning in 1855",
    "Great Expectations by Charles Dickens in 1861",
    "The Ring and the Book by Robert Browning in 1868",
    "War and Peace by Leo Tolstoy in 1869",
    "The Adventures of Tom Sawyer by Mark Twain in 1876",
    "Adventures of Huckleberry Finn by Mark Twain in 1884"
]

Installation

Install as Composer Package:

composer require pyther/linquo

Supported LINQ methods

List of supported LINQ methods, ordered by name:

Method Description
aggregate Aggregate the elements of a sequence into a single value.
aggregateBy Aggregate the elements of a sequence into a grouped result.
all Check if all elements in the sequence satisfy a given predicate.
any Check if any element in the sequence satisfies a given predicate.
append Append a value to the current sequence.
average Calculate the average of the elements in the sequence.
case Cast the elements of the sequence to a specified type or apply a function to each element.
chunk Chunk the sequence into smaller sequences of a specified size.
concat Concatenate the elements of another sequence, array, traversable or iterable to the end of the sequence.
contains Check if the sequence contains a specific value.
count Count the number of elements in the sequence.
countBy Count the number of elements in the sequence based on a key selector.
distinct Create a distinct sequence by removing duplicate elements.
distinctBy Create a distinct sequence by removing duplicate elements based on a key selector.
elementAt Get the element at a specified index in the sequence.
elementAtOrNull Get the element at a specified index in the sequence or null if it doesn't exist.
empty Create an empty Sequence.
except Create a new sequence by applying the set difference of two sequences.
exceptBy Create a new sequence by applying the set difference of two sequences based on a key selector.
first Get the first element of the sequence or the first element that satisfies a predicate.
firstOrNull Get the first element of the sequence or null if no element satisfies a predicate.
groupBy Group the elements of the sequence by a specified key selector.
groupJoin Join the elements of two sequences based on key equality, and groups the results.
index Return a sequence of elements with their indices.
intersect Get the intersection of two sequences, returning elements that are present in both.
intersectBy Get the intersection of two sequences based on a key selector, returning elements that are present in both.
join Join two sequences based on key equality into a new sequence.
last Get the last element of the sequence or the last element that satisfies a predicate.
lastOrNull Get the last element of the sequence or null if no element satisfies a predicate.
leftJoin Left join two sequences based on key equality into a new sequence.
max Get the maximum value in the sequence or the maximum value based on a function.
maxBy Get the maximum value in the sequence based on a function and an optional comparer.
min Get the minimum value in the sequence or the minimum value based on a function.
minBy Get the minimum value in the sequence based on a function and an optional comparer.
ofType Filter the elements of the sequence based on their type.
order Order the elements of the sequence based on a comparer or default comparison.
orderBy Order the elements of the sequence based on a key selector and an optional comparer.
orderByDescending Order the elements of the sequence in descending order based on a key selector.
orderDescending Order the elements of the sequence in descending order based on a comparer or default comparison.
prepend Prepend a value or a sequence to the current sequence.
range Create a Sequence from a range of integers.
repeat Create a Sequence that repeats a value a specified number of times.
reverse Create a new sequence in reverse order.
rightJoin Right join two sequences based on key equality into a new sequence.
select Select elements from the sequence based on a selector function.
selectMany Select elements from the sequence and flatten the results into a single sequence.
sequenceEqual Check if two sequences are equal based on their elements and an optional comparer.
shuffle Shuffle the elements of the sequence randomly.
single Get the the element that satisfies the predicate or throw an exception if more than one element is found.
singleOrNull Get a single element from the sequence or null if no element is found.
skip Skip a specified number of elements in the sequence.
skipLast Skip the last specified number of elements in the sequence.
skipWhile Skip elements in the sequence while a predicate is true.
sum Get the sum of the elements in the sequence or the sum based on a selector function.
take Take a specified number of elements from the sequence.
takeLast Take the last specified number of elements from the sequence.
takeWhile Take elements from the sequence while a predicate is true.
thenBy Order an already ordered sequence in ascending order.
thenByDescending Order an already ordered sequence in descending order.
toArray Convert the sequence to an array.
toDictionary Convert the sequence to a dictionary (associative array)
toHashSet Convert the sequence to a hash set (array with unique values).
toList Convert the sequence to a doubly linked list.
toLookup Convert the sequence to a lookup (associative array with multiple values for each key).
tryGetNonEnumeratedCount Try to get the count of elements in the sequence without enumerating it.
union Get the union of two sequences, returning elements that are present in either sequence.
unionBy Get the union of two sequences based on a key selector, returning elements that are present in either sequence.
where Filter the elements of the sequence based on a predicate.
zip Zip two sequences together, combining elements from both sequences into a new sequence.

Additional methods

List of additional methods, ordered by name:

Method Description
toJson Convert the sequence to a JSON string.

Supported interfaces

All sequences support methods and behaviour of the following interfaces:

Method Description
IteratorAggregate
Countable

ToDo

  • complete inline docs
  • create documentation with samples
  • optimize all SET operations using hashs and multidimensional lookups