The driving forces behind LINQ for C# are extension methods. Without extension methods, LINQ becomes a cumbersome, syntax-heavy burden. It's this burden that I've translated to PHP.
Below, you'll find a few LINQ-like methods for PHP. Each of the methods takes two arguments: a collection and a predicate. The method simply iterates through the collection, tests the predicate against each item, and returns different results depending on the purpose of the call.
I've mimicked the following LINQ methods from C#:
Count
Where
Select
SelectMany
First
FirstOrDefault
Last
LastOrDefault
It's good to note that, not only is this manner of incorporating predicates compatible with PHP 5.2, it also doesn't rely on string parsing to create a lambda expression. The predicate is created with good-ol' create_function
.
If you're looking for a full-fledged LINQ solution for PHP, check out PHPLinq.
As an added bonus, there's a simple yet effective test framework included in the example below. I'll try to write more about it some other time, but, for now, you should see the following result if you run the script in a browser:
Starting tests...
15 tests found.
Performing testAssertFailed...
Performing testAssertAreEqual...
Performing testAssertAreEqual2...
Performing testAssertAreNotEqual...
Performing testAssertAreNotEqual2...
Performing testAssertIsTrue...
Performing testAssertIsFalse...
Performing testCmGnWhere...
Performing testCmGnCount...
Performing testCmGnSelect...
Performing testCmGnSelectMany...
Performing testCmGnFirst...
Performing testCmGnFirstOrNull...
Performing testCmGnLast...
Performing testCmGnLastOrNull...
15 of 15 tests passed.
Stay tuned for the upcoming phpChimpanzee
framework which contains more ugly solutions to everyday coding problems.
CodeProject
<?php
class cmGn {
public static function count(Array $array, $predicate = null) {
if ($predicate === null)
return count($array);
if (!is_callable($predicate))
throw new cmGnException('Provided predicate is not a callable function.');
$count = 0;
foreach ($array as $item)
if ($predicate($item) === true)
$count++;
return $count;
}
public static function where
(Array $array, $predicate) {
if (!is_callable
($predicate))
throw new cmGnException('Provided predicate is not a callable function.');
$newAr = Array();
foreach ($array as $item)
if ($predicate($item) === true)
$newAr[] = $item;
return $newAr;
}
public static function select
(Array $array, $predicate) {
if (!is_callable
($predicate))
throw new cmGnException('Provided predicate is not a callable function.');
$newAr = Array();
foreach ($array as $item)
$newAr[] = $predicate($item);
return $newAr;
}
public static function selectMany
(Array $array, $predicate) {
if (!is_callable
($predicate))
throw new cmGnException('Provided predicate is not a callable function.');
$newAr = Array();
foreach ($array as $item)
foreach ($predicate($item) as $newItem)
$newAr[] = $newItem;
return $newAr;
}
public static function first
(Array $array, $predicate) {
if (!is_callable
($predicate))
throw new cmGnException('Provided predicate is not a callable function.');
foreach ($array as $item)
if ($predicate($item) === true)
return $item;
throw new cmGnException('No items were found in the
array parameter to return as the first item in a cmGn predicate query.');
}
public static function firstOrNull
(Array $array, $predicate) {
if (!is_callable
($predicate))
throw new cmGnException('Provided predicate is not a callable function.');
foreach ($array as $item)
if ($predicate($item) === true)
return $item;
return null;
}
public static function last
(Array $array, $predicate) {
return cmGn::first
(array_reverse($array),
$predicate);
}
public static function lastOrNull
(Array $array, $predicate) {
return cmGn::firstOrNull
(array_reverse($array),
$predicate);
}
}
class cmTest {
private $testCount = 0;
private $passCount = 0;
protected function alert($message) {
echo('<p style="margin:0;padding:0">' . $message . '</p>');
}
protected function testAssertFailed() {
try {
cmAssert::failed();
$this->alert('Assert failed failed.');
}
catch (Exception $e) {
// The test passed if we got here.
}
}
protected function testAssertAreEqual() {
try {
cmAssert::areEqual(1, 1);
}
catch (Exception $e) {
cmAssert::failed();
}
}
protected function testAssertAreEqual2() {
try {
cmAssert::areEqual(1, 2);
cmAssert::failed();
}
catch (Exception $e) {
// The test passed if we got here.
}
}
protected function testAssertAreNotEqual() {
try {
cmAssert::areNotEqual(1, 2);
}
catch (Exception $e) {
cmAssert::failed();
}
}
protected function testAssertAreNotEqual2() {
try {
cmAssert::areNotEqual(1, 1);
cmAssert::failed();
}
catch (Exception $e) {
// The test passed if we got here.
}
}
protected function testAssertIsTrue() {
try {
cmAssert::isTrue(true);
}
catch (Exception $e) {
cmAssert::failed();
}
}
protected function testAssertIsFalse() {
try {
cmAssert::isFalse(false);
}
catch (Exception $e) {
cmAssert::failed();
}
}
protected function testCmGnWhere() {
$array = Array
(0, 0, 1, 1, 2, 2, 3, 3);
cmAssert::areEqual(
4,
count
(cmGn::where($array,
create_function
('$v', 'return $v < 2;')))
);
foreach (cmGn::where($array,
create_function
('$v', 'return $v == 3;')) as $item) {
cmAssert::areEqual(3, $item);
}
}
protected function testCmGnCount() {
$array = Array
(0, 0, 1, 1, 2, 2, 3, 3);
cmAssert::areEqual(
4,
cmGn::count
($array,
create_function('$v', 'return $v < 2;'))
);
cmAssert::areEqual(
2,
cmGn::count
($array,
create_function('$v', 'return $v == 3;'))
);
}
protected function testCmGnSelect() {
$array = Array(0, 0, 1, 1, 2, 2, 3, 3);
$newAr = cmGn::select($array,
create_function
('$v', 'return $v - 1;'));
foreach ($array as $item) {
cmAssert::isTrue
(in_array
($item - 1, $newAr, true));
}
}
protected function testCmGnSelectMany() {
$array = Array
(Array(0, 1, 2, 3),
Array(0, 1, 2, 3),
Array(0, 1, 2, 3));
$newAr = cmGn::selectMany($array,
create_function
('$a', 'return $a;'));
cmAssert::areEqual
(3, cmGn::count($newAr,
create_function
('$v', 'return $v === 3;')));
cmAssert::areEqual
(3, cmGn::count
($newAr,
create_function('$v', 'return $v === 1;')));
cmAssert::areEqual(12, count($newAr));
}
protected function testCmGnFirst() {
$array = Array(Array(0), Array(0, 1), Array(0, 1, 2));
$first = cmGn::first($array, create_function
('$a', 'return count($a) == 3;'));
cmAssert::areEqual(3,
count($first));
cmAssert::isTrue
(in_array(2, $first));
try {
$first = cmGn::first($array,
create_function
('$a', 'return count($a) == 4;'));
cmAssert::failed();
}
catch (cmGnException $e) {
// The test passed if we got here.
}
}
protected function testCmGnFirstOrNull() {
$array = Array(Array(0), Array(0, 1), Array(0, 1, 2));
$first = cmGn::firstOrNull($array, create_function
('$a', 'return count($a) == 3;'));
cmAssert::areEqual
(3, count($first));
cmAssert::isTrue
(in_array(2, $first));
$first = cmGn::firstOrNull($array, create_function
('$a', 'return count($a) == 4;'));
cmAssert::areEqual(null, $first);
}
protected function testCmGnLast() {
$array = Array(Array(0, 1), Array(2, 3), Array(3, 4));
$last = cmGn::last($array,
create_function
('$a', 'return in_array(3, $a);'));
cmAssert::isTrue
(in_array(4, $last));
try {
$last = cmGn::last($array,
create_function('$a', 'return in_array(5, $a);'));
cmAssert::failed();
}
catch (cmGnException $e) {
// The test passed if we got here.
}
}
protected function testCmGnLastOrNull() {
$array = Array(Array(0, 1), Array(2, 3), Array(3, 4));
$last = cmGn::lastOrNull($array, create_function('$a',
'return in_array(3, $a);'));
cmAssert::isTrue(in_array(4, $last));
$last = cmGn::lastOrNull($array, create_function('$a',
'return in_array(5, $a);'));
cmAssert::areEqual(null, $last);
}
public function test() {
$this->alert('Starting tests...');
$methd = get_class_methods
(get_class($this));
$methd = $methd ? $methd : Array();
$tests = cmGn::where($methd,
create_function('$m', 'return cmTest::isTest($m);'));
$this->alert
(count($tests) . ' tests found.');
foreach ($tests as $test)
{
try {
$this->alert('Performing ' . $test . '...');
$this->testCount++;
$this->$test();
$this->passCount++;
}
catch (Exception $e) {
$this->alert($e->getMessage());
}
}
$this->alert($this->passCount .
' of ' . $this->testCount . ' tests passed.');
}
public static function isTest($test) {
$strpos = strpos
($test, 'test');
if ($strpos === false)
return false;
if ($strpos > 0)
return false;
if ($test == 'test')
return false;
return true;
}
}
class cmAssert {
public static function failed($message = null) {
$message = $message ? $message : 'Assert failed called.';
throw new cmTestException($message, -1);
}
public static function areEqual($val1, $val2, $message = null) {
$message = $message ? $message : 'Assert are equal failed.';
if ($val1 !== $val2)
throw new cmTestException($message, -2);
}
public static function areNotEqual($val1, $val2, $message = null) {
$message = $message ? $message : 'Assert are not equal failed.';
if ($val1 === $val2)
throw new cmTestException($message, -3);
}
public static function isTrue($boolean, $message = null) {
$message = $message ? $message : 'Assert is true failed.';
if ($boolean !== true)
throw new cmTestException($message, -4);
}
public static function isFalse($boolean, $message = null) {
$message = $message ? $message : 'Assert is false failed.';
if ($boolean !== false)
throw new cmTestException($message, -5);
}
}
class cmException extends Exception {
const baseCode = -1064000;
public function __construct($message = null, $code = 0) {
parent::__construct('phpChimpanzee Exception: ' .
($message ? $message : 'No message given.'),
cmException::baseCode +
(abs($code) * -1));
}
}
class cmTestException extends cmException {
const baseCodeModifier = -64000;
public function __construct($message = null, $code = 0) {
parent::__construct('Test failure... ' .
($message ? $message : 'No message given.'),
cmTestException::baseCodeModifier +
(abs($code) * -1));
}
}
class cmGnException extends cmException {
const baseCodeModifier = -128000;
public function __construct($message = null, $code = 0) {
parent::__construct($message,
cmGnException::baseCodeModifier +
(abs($code) * -1));
}
}
$test = new cmTest();
$test->test();
?>