2017 © Pedro Peláez
 

library ginq

LINQ to Object inspired DSL for PHP

image

ginq/ginq

LINQ to Object inspired DSL for PHP

  • Wednesday, December 13, 2017
  • by akanehara
  • Repository
  • 24 Watchers
  • 167 Stars
  • 103,543 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 14 Forks
  • 5 Open issues
  • 15 Versions
  • 11 % Grown

The README.md

Ginq

Array handling in PHP? Be happy with Ginq!

Ginq is a DSL that can handle arrays and iterators of PHP unified., (*1)

Ginq is inspired by Linq to Object, but is not a clone., (*2)

Many functions in Ginq are evaluated in lazy, and no actions are taken until that time. This features bring you many benefits., (*3)

Install

composer.json:, (*4)

{
    "require": {
        "ginq/ginq": "dev-master"
    }
}

see: https://packagist.org/packages/ginq/ginq, (*5)

Usage

$xs = Ginq::from(array(1,2,3,4,5,6,7,8,9,10))
        ->where(function($x) { return $x % 2 != 0; })
        ->select(function($x) { return $x * $x; });
        ;

You pass Ginq data and build a query with it. In this example above, you order Ginq to choose even numbers and square each of them., (*6)

But Ginq do nothing, Ginq knows only you want a result of chosen and squared numbers., (*7)

Let's execute foreach loop with Ginq to get the result., (*8)

foreach ($xs as $x) { echo "$x "; }

The result is, (*9)

1 9 25 49 81

You got the expected result!, (*10)

Next, you can get an array with toList., (*11)

$xs->toList();

``` array(1,9,25,49,81);, (*12)


**Ginq** has functions, well-known in SQL, such as `join()`, `orderBy()`, and `groupBy()` other than `select()`, `where()` listed above. ## Selector and Predicate Most of methods in **Ginq** receive a closure as a argument. You may not be familiar with closures, but it is very simple things. There are just three types of closures in **Ginq**, you can remember simply. These are predicate, selector, and connection selector. ### Predicate A closure that passed to a method that do select, such as `where()` is called **predicate**. Predicate is a closure that receive a pair of key and values in the elements and return boolean value. ```php function ($v, [$k]) { return $v % 2 == 0; }

You get even numbers when you pass this closure to where(). You can skip second argument when you don't need it in the process., (*13)

Selector

A closure that passed to a method that do projection, such as select() is called selector., (*14)

Selector is a closure that receive a pair of key and value in the elements and create a new value or key, and then return it., (*15)

function ($v, [$k]) { return $v * $v ; }

You get squared numbers of original when you pass this closure to select()., (*16)

This function is used to specify the key of grouping with groupBy(), the key of sorting with groupBy()., (*17)

Connection Selector

Connection Selector is one of the selector that combine two elements into one, is used with join(), zip()., (*18)

function ($v0, $v1, [$k0, $k1]) { return array($v0, $v1) ; }

This function receive 4 arguments, two values and two keys, and then create new value or key and return it. You can skip arguments when you don't need it in the process., (*19)

These are zip() example that combine each elements from two arrays., (*20)

$foods  = array("meat", "pasta", "salada");
$spices = array("time", "basil", "dill");

$xs = Ginq::from($foods)
        ->zip($spices, function($f, $s) {
            return "$f with $s!";
        })
        ;

foreach ($xs as $x) { echo "$x\n"; }
meat with time!
pasta with basil!
salada with dill!

Shortcuts of predicate and selector

Selector can receive a character string instead of a closure., (*21)

They return the value of the field when the element is an object, or return the value of the key when it is an array., (*22)

So,, (*23)

Ginq::from($xs)->select('[key].property');

The example above is same as two examples below., (*24)

Ginq::from($xs)->select(
    function ($v, $k) { return $v['key'].property; }
);

see: Property Access (Symfony) http://symfony.com/doc/current/components/property_access/index.html, (*25)

More complex examples

References

Development

PreRequirements

  • Docker installed.
  • Docker Compose installed.

How to start development (Run test)

  • cd docker
  • docker-compose up -d
  • docker-compose exec php ash
  • (in php container)
    • composer install
    • vendor/bin/phpunit

Note

The Versions

13/12 2017

dev-master

9999999-dev

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Atsushi Kanehara

array iterator dsl linq

22/07 2015

v0.2.4

0.2.4.0

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Atsushi Kanehara

array iterator dsl linq

24/08 2014

v0.2.3

0.2.3.0

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Atsushi Kanehara

array iterator dsl linq

24/08 2014

dev-develop

dev-develop

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Atsushi Kanehara

array iterator dsl linq

24/08 2014

dev-feature/explicit-comparer

dev-feature/explicit-comparer

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Atsushi Kanehara

array iterator dsl linq

04/07 2014

dev-feature/generator-impl

dev-feature/generator-impl

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Atsushi Kanehara

array iterator dsl linq

15/05 2014

v0.2.2

0.2.2.0

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Atsushi Kanehara

array iterator dsl linq

14/04 2014

v0.2.1

0.2.1.0

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Atsushi Kanehara

array iterator dsl linq

10/04 2014

v0.2.0

0.2.0.0

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Atsushi Kanehara

array iterator dsl linq

05/03 2014

dev-feature/expr

dev-feature/expr

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Atsushi Kanehara

array iterator dsl linq

26/02 2014

v0.1.4

0.1.4.0

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Atsushi Kanehara

array iterator dsl linq

14/02 2014

v0.1.3

0.1.3.0

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Atsushi Kanehara

array iterator dsl linq

23/01 2014

v0.1.2

0.1.2.0

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Atsushi Kanehara

array iterator dsl linq

10/10 2013

v0.1.1

0.1.1.0

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Atsushi Kanehara

array iterator dsl linq

17/09 2013

v0.1.0

0.1.0.0

LINQ to Object inspired DSL for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Atsushi Kanehara

array iterator dsl linq