Code Monkey home page Code Monkey logo

phlinq's Introduction

PHLinq (flink)

My modest go at getting a little bit of a LINQy feel to PHP.

Examples:

class Person {
    public $name, $age;
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }
}

$persons = array(
    new Person("peter", 10),
    new Person("jane",  14),
    new Person("allan", 42),
    new Person("lis",   23),
    new Person("jimmy", 19),
    new Person("ruth",   7)
);


// names of persons above 18 ordered alphabetically
$names = phlinq($persons)
    ->where(function($p) { return $p->age > 18; })
    ->select(function($p) { return $p->name; })
    ->order()
    ;

var_dump($names->toArray());
/*
array(3) {
  [0]=>
  string(4) "allan"
  [1]=>
  string(5) "jimmy"
  [2]=>
  string(3) "lis"
} 
*/


// same names but ordered by age (youngest first)
$names = phlinq($persons)
    ->where(function($p) { return $p->age > 18; })
    ->orderBy(function($p) { return $p->age; })
    ->select(function($p) { return $p->name; })
    ;

var_dump($names->toArray());

/*
array(3) {
  [0]=>
  string(5) "jimmy"
  [1]=>
  string(3) "lis"
  [2]=>
  string(4) "allan"
}
*/

Available Methods:

  • toArray - convert to array()
  • count - count the elements in the result
  • firstOrNull - get the first element or null if none is present
  • first - get the first element, throws exception on failure to find any
  • lastOrNull - get last element or null if none is present
  • last - get last element, throws exception on failure to find any
  • singleOrNull - get a single element based on a condition or null if no element mathces
  • single - get a single element based on a condition, throws an exception on failure to find any
  • where - filter elements based on condition
  • select - select values from the elements
  • order - order the elements
  • orderBy - order the elements by a given value
  • orderDesc - like order but, descending
  • orderByDesc - like orderBy, but descending

phlinq's People

Contributors

jonlt avatar

Stargazers

Matthew Winnard avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.