Skip to content

Latest commit

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Linquo

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:

MethodDescription
aggregateAggregate the elements of a sequence into a single value.
aggregateByAggregate the elements of a sequence into a grouped result.
allCheck if all elements in the sequence satisfy a given predicate.
anyCheck if any element in the sequence satisfies a given predicate.
appendAppend a value to the current sequence.
averageCalculate the average of the elements in the sequence.
caseCast the elements of the sequence to a specified type or apply a function to each element.
chunkChunk the sequence into smaller sequences of a specified size.
concatConcatenate the elements of another sequence, array, traversable or iterable to the end of the sequence.
containsCheck if the sequence contains a specific value.
countCount the number of elements in the sequence.
countByCount the number of elements in the sequence based on a key selector.
distinctCreate a distinct sequence by removing duplicate elements.
distinctByCreate a distinct sequence by removing duplicate elements based on a key selector.
elementAtGet the element at a specified index in the sequence.
elementAtOrNullGet the element at a specified index in the sequence or null if it doesn't exist.
emptyCreate an empty Sequence.
exceptCreate a new sequence by applying the set difference of two sequences.
exceptByCreate a new sequence by applying the set difference of two sequences based on a key selector.
firstGet the first element of the sequence or the first element that satisfies a predicate.
firstOrNullGet the first element of the sequence or null if no element satisfies a predicate.
groupByGroup the elements of the sequence by a specified key selector.
groupJoinJoin the elements of two sequences based on key equality, and groups the results.
indexReturn a sequence of elements with their indices.
intersectGet the intersection of two sequences, returning elements that are present in both.
intersectByGet the intersection of two sequences based on a key selector, returning elements that are present in both.
joinJoin two sequences based on key equality into a new sequence.
lastGet the last element of the sequence or the last element that satisfies a predicate.
lastOrNullGet the last element of the sequence or null if no element satisfies a predicate.
leftJoinLeft join two sequences based on key equality into a new sequence.
maxGet the maximum value in the sequence or the maximum value based on a function.
maxByGet the maximum value in the sequence based on a function and an optional comparer.
minGet the minimum value in the sequence or the minimum value based on a function.
minByGet the minimum value in the sequence based on a function and an optional comparer.
ofTypeFilter the elements of the sequence based on their type.
orderOrder the elements of the sequence based on a comparer or default comparison.
orderByOrder the elements of the sequence based on a key selector and an optional comparer.
orderByDescendingOrder the elements of the sequence in descending order based on a key selector.
orderDescendingOrder the elements of the sequence in descending order based on a comparer or default comparison.
prependPrepend a value or a sequence to the current sequence.
rangeCreate a Sequence from a range of integers.
repeatCreate a Sequence that repeats a value a specified number of times.
reverseCreate a new sequence in reverse order.
rightJoinRight join two sequences based on key equality into a new sequence.
selectSelect elements from the sequence based on a selector function.
selectManySelect elements from the sequence and flatten the results into a single sequence.
sequenceEqualCheck if two sequences are equal based on their elements and an optional comparer.
shuffleShuffle the elements of the sequence randomly.
singleGet the the element that satisfies the predicate or throw an exception if more than one element is found.
singleOrNullGet a single element from the sequence or null if no element is found.
skipSkip a specified number of elements in the sequence.
skipLastSkip the last specified number of elements in the sequence.
skipWhileSkip elements in the sequence while a predicate is true.
sumGet the sum of the elements in the sequence or the sum based on a selector function.
takeTake a specified number of elements from the sequence.
takeLastTake the last specified number of elements from the sequence.
takeWhileTake elements from the sequence while a predicate is true.
thenByOrder an already ordered sequence in ascending order.
thenByDescendingOrder an already ordered sequence in descending order.
toArrayConvert the sequence to an array.
toDictionaryConvert the sequence to a dictionary (associative array)
toHashSetConvert the sequence to a hash set (array with unique values).
toListConvert the sequence to a doubly linked list.
toLookupConvert the sequence to a lookup (associative array with multiple values for each key).
tryGetNonEnumeratedCountTry to get the count of elements in the sequence without enumerating it.
unionGet the union of two sequences, returning elements that are present in either sequence.
unionByGet the union of two sequences based on a key selector, returning elements that are present in either sequence.
whereFilter the elements of the sequence based on a predicate.
zipZip two sequences together, combining elements from both sequences into a new sequence.

Additional methods

List of additional methods, ordered by name:

MethodDescription
toJsonConvert the sequence to a JSON string.

Supported interfaces

All sequences support methods and behaviour of the following interfaces:

MethodDescription
IteratorAggregate
Countable

ToDo

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

About

Linquo is a lightweight PHP library to support LINQ-like operations on sequences such as arrays.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages