YaLinqo Documentation

repository·master·Indexed 19 days ago

https://github.com/athari/yalinqo

A high-performance, lazy-evaluated LINQ implementation for PHP that provides a functional, fluent API for data transformations, filtering, and aggregations on arrays and iterables. It implements over 80 methods similar to .NET's LINQ and is compatible with PHP 7.0 or higher.

Tokens
2K
Snippets
4
Records
5
Agent score
16%

What's inside YaLinqo

  1. Migration guide: Version 2.x to 3.x

    master

    When upgrading from YaLinqo 2.x to 3.x, be aware of the following changes:

    • Minimum PHP Version: Requires PHP 7.0 or higher.
    • Type Hinting: Type hints have been added to parameters for several functions, including ofType, range, rangeDown, rangeTo, toInfinity, toNegativeInfinity, matches, and split. If your code passes incorrect argument types to these functions, it may now trigger errors.
  2. Initialize YaLinqo in your PHP script

    master

    After installing via Composer, include the autoloader and import the Enumerable class. You can create an enumerable collection using Enumerable::from() or the from() global function shortcut.

    require_once 'vendor/autoloader.php';
    use \YaLinqo\Enumerable;
    
    // Using the static method
    Enumerable::from([1, 2, 3]);
    
    // Using the global function shortcut
    from([1, 2, 3]);
  3. Complex data processing example with groupJoin

    master

    This example demonstrates how to join two datasets (categories and products), filter the results, sort them, and transform the output using YaLinqo's fluent API. It shows both the shorthand syntax and the more verbose PHP 8.0+ named parameter syntax.

    // Data
    $products = [
        [ 'name' => 'Keyboard',    'catId' => 'hw', 'quantity' =>  10, 'id' => 1 ],
        [ 'name' => 'Mouse',       'catId' => 'hw', 'quantity' =>  20, 'id' => 2 ],
        [ 'name' => 'Monitor',     'catId' => 'hw', 'quantity' =>   0, 'id' => 3 ],
        [ 'name' => 'Joystick',    'catId' => 'hw', 'quantity' =>  15, 'id' => 4 ],
        [ 'name' => 'CPU',         'catId' => 'hw', 'quantity' =>  15, 'id' => 5 ],
        [ 'name' => 'Motherboard', 'catId' => 'hw', 'quantity' =>  11, 'id' => 6 ],
        [ 'name' => 'Windows',     'catId' => 'os', 'quantity' => 666, 'id' => 7 ],
        [ 'name' => 'Linux',       'catId' => 'os', 'quantity' => 666, 'id' => 8 ],
        [ 'name' => 'Mac',         'catId' => 'os', 'quantity' => 666, 'id' => 9 ],
    ];
    $categories = [
        [ 'name' => 'Hardware',          'id' => 'hw' ],
        [ 'name' => 'Operating systems', 'id' => 'os' ],
    ];
    
    // Shorthand syntax
    $result = from($categories)
        ->orderBy(fn($cat) => $cat['name'])
        ->groupJoin(
            from($products)
                ->where(fn($prod) => $prod['quantity'] > 0)
                ->orderByDescending(fn($prod) => $prod['quantity'])
                ->thenBy(fn($prod) => $prod['name'], 'strnatcasecmp'),
            fn($cat) => $cat['id'],
            fn($prod) => $prod['catId'],
            fn($cat, $prods) => [
                'name' => $cat['name'],
                'products' => $prods
            ]
        );
    
    // Verbose syntax (PHP 8.0+ with named parameters and PHP 8.1+ first-class callables)
    $result = Enumerable::from($categories)
        ->orderBy(keySelector: fn($cat) => $cat['name'])
        ->groupJoin(
            inner: from($products)
                ->where(predicate: fn($prod) => $prod['quantity'] > 0)
                ->orderByDescending(keySelector: fn($prod) => $prod['quantity'])
                ->thenBy(keySelector: fn($prod) => $prod['name'], comparer: strnatcasecmp(...)),
            outerKeySelector: fn($cat) => $cat['id'],
            innerKeySelector: fn($prod) => $prod['catId'],
            resultSelectorValue: fn($cat, $prods) => [
                'name' => $cat['name'],
                'products' => $prods
            ]
        );
    
    print_r($result->toArrayDeep());
  4. Reference of implemented YaLinqo methods

    master

    YaLinqo implements over 80 methods categorized by their purpose. Note that some methods have been renamed from their original .NET LINQ names to avoid conflicts with PHP reserved keywords. The original names are provided in parentheses in the documentation.

    * Generation: cycle, emptyEnum (empty), from, generate, toInfinity, toNegativeInfinity, matches, returnEnum (return), range, rangeDown, rangeTo, repeat, split;
    * Projection and filtering: cast, ofType, select, selectMany, where;
    * Ordering: orderBy, orderByDescending, orderByDir, thenBy, thenByDescending, thenByDir;
    * Joining and grouping: groupJoin, join, groupBy;
    * Aggregation: aggregate, aggregateOrDefault, average, count, max, maxBy, min, minBy, sum;
    * Set: all, any, append, concat, contains, distinct, except, intersect, prepend, union;
    * Pagination: elementAt, elementAtOrDefault, first, firstOrDefault, firstOrFallback, last, lastOrDefault, lastOrFallback, single, singleOrDefault, singleOrFallback, indexOf, lastIndexOf, findIndex, findLastIndex, skip, skipWhile, take, takeWhile;
    * Conversion: toArray, toArrayDeep, toList, toListDeep, toDictionary, toJSON, toLookup, toKeys, toValues, toObject, toString;
    * Actions: call (do), each (forEach), write, writeLine.