Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

LINQ for PHP

, 5 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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 predica

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 lamda 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.

<?php
 
	class cmGn {
		public static function <a href="http://www.php.net/count">count</a>(<a href="http://www.php.net/array">Array</a> $array, $predicate = null) {
			if ($predicate === null)
				return <a href="http://www.php.net/count">count</a>($array);
			if (!<a href="http://www.php.net/is_callable">is_callable</a>($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(<a href="http://www.php.net/array">Array</a> $array, $predicate) {
			if (!<a href="http://www.php.net/is_callable">is_callable</a>($predicate))
				throw new cmGnException('Provided predicate is not a callable function.');
			$newAr = <a href="http://www.php.net/array">Array</a>();
			foreach ($array as $item)
				if ($predicate($item) === true)
					$newAr[] = $item;
			return $newAr;
		}
 
		public static function select(<a href="http://www.php.net/array">Array</a> $array, $predicate) {
			if (!<a href="http://www.php.net/is_callable">is_callable</a>($predicate))
				throw new cmGnException('Provided predicate is not a callable function.');		
			$newAr = <a href="http://www.php.net/array">Array</a>();
			foreach ($array as $item)
				$newAr[] = $predicate($item);
			return $newAr;
		}
 
		public static function selectMany(<a href="http://www.php.net/array">Array</a> $array, $predicate) {
			if (!<a href="http://www.php.net/is_callable">is_callable</a>($predicate))
				throw new cmGnException('Provided predicate is not a callable function.');		
			$newAr = <a href="http://www.php.net/array">Array</a>();
			foreach ($array as $item)
				foreach ($predicate($item) as $newItem)
					$newAr[] = $newItem;
			return $newAr;
		}
 
		public static function first(<a href="http://www.php.net/array">Array</a> $array, $predicate) {
			if (!<a href="http://www.php.net/is_callable">is_callable</a>($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(<a href="http://www.php.net/array">Array</a> $array, $predicate) {
			if (!<a href="http://www.php.net/is_callable">is_callable</a>($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(<a href="http://www.php.net/array">Array</a> $array, $predicate) {
			return cmGn::first(<a href="http://www.php.net/array_reverse">array_reverse</a>($array), $predicate);
		}
 
		public static function lastOrNull(<a href="http://www.php.net/array">Array</a> $array, $predicate) {
			return cmGn::firstOrNull(<a href="http://www.php.net/array_reverse">array_reverse</a>($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 = <a href="http://www.php.net/array">Array</a>(0, 0, 1, 1, 2, 2, 3, 3);
			cmAssert::areEqual(
				4,
				<a href="http://www.php.net/count">count</a>(cmGn::where($array, <a href="http://www.php.net/create_function">create_function</a>('$v', 'return $v < 2;')))
			);
			foreach (cmGn::where($array, <a href="http://www.php.net/create_function">create_function</a>('$v', 'return $v == 3;')) as $item) {
				cmAssert::areEqual(3, $item);
			}
		}
 
		protected function testCmGnCount() {
			$array = <a href="http://www.php.net/array">Array</a>(0, 0, 1, 1, 2, 2, 3, 3);
			cmAssert::areEqual(
				4,
				cmGn::<a href="http://www.php.net/count">count</a>($array, <a href="http://www.php.net/create_function">create_function</a>('$v', 'return $v < 2;'))
			);
			cmAssert::areEqual(
				2,
				cmGn::<a href="http://www.php.net/count">count</a>($array, <a href="http://www.php.net/create_function">create_function</a>('$v', 'return $v == 3;'))
			);
		}
 
		protected function testCmGnSelect() {
			$array = <a href="http://www.php.net/array">Array</a>(0, 0, 1, 1, 2, 2, 3, 3);
			$newAr = cmGn::select($array, <a href="http://www.php.net/create_function">create_function</a>('$v', 'return $v - 1;'));
			foreach ($array as $item) {
				cmAssert::isTrue(<a href="http://www.php.net/in_array">in_array</a>($item - 1, $newAr, true));
			}
		}
 
		protected function testCmGnSelectMany() {
			$array = <a href="http://www.php.net/array">Array</a>(<a href="http://www.php.net/array">Array</a>(0, 1, 2, 3), <a href="http://www.php.net/array">Array</a>(0, 1, 2, 3), <a href="http://www.php.net/array">Array</a>(0, 1, 2, 3));
			$newAr = cmGn::selectMany($array, <a href="http://www.php.net/create_function">create_function</a>('$a', 'return $a;'));
			cmAssert::areEqual(3, cmGn::<a href="http://www.php.net/count">count</a>($newAr, <a href="http://www.php.net/create_function">create_function</a>('$v', 'return $v === 3;')));
			cmAssert::areEqual(3, cmGn::<a href="http://www.php.net/count">count</a>($newAr, <a href="http://www.php.net/create_function">create_function</a>('$v', 'return $v === 1;')));
			cmAssert::areEqual(12, <a href="http://www.php.net/count">count</a>($newAr));
		}
 
		protected function testCmGnFirst() {
			$array = <a href="http://www.php.net/array">Array</a>(<a href="http://www.php.net/array">Array</a>(0), <a href="http://www.php.net/array">Array</a>(0, 1), <a href="http://www.php.net/array">Array</a>(0, 1, 2));
			$first = cmGn::first($array, <a href="http://www.php.net/create_function">create_function</a>('$a', 'return count($a) == 3;'));
			cmAssert::areEqual(3, <a href="http://www.php.net/count">count</a>($first));
			cmAssert::isTrue(<a href="http://www.php.net/in_array">in_array</a>(2, $first));
			try {
				$first = cmGn::first($array, <a href="http://www.php.net/create_function">create_function</a>('$a', 'return count($a) == 4;'));
				cmAssert::failed();
			}
			catch (cmGnException $e) {
				// The test passed if we got here.
			}
		}
 
		protected function testCmGnFirstOrNull() {
			$array = <a href="http://www.php.net/array">Array</a>(<a href="http://www.php.net/array">Array</a>(0), <a href="http://www.php.net/array">Array</a>(0, 1), <a href="http://www.php.net/array">Array</a>(0, 1, 2));
			$first = cmGn::firstOrNull($array, <a href="http://www.php.net/create_function">create_function</a>('$a', 'return count($a) == 3;'));
			cmAssert::areEqual(3, <a href="http://www.php.net/count">count</a>($first));
			cmAssert::isTrue(<a href="http://www.php.net/in_array">in_array</a>(2, $first));
			$first = cmGn::firstOrNull($array, <a href="http://www.php.net/create_function">create_function</a>('$a', 'return count($a) == 4;'));
			cmAssert::areEqual(null, $first);
		}
 
		protected function testCmGnLast() {
			$array = <a href="http://www.php.net/array">Array</a>(<a href="http://www.php.net/array">Array</a>(0, 1), <a href="http://www.php.net/array">Array</a>(2, 3), <a href="http://www.php.net/array">Array</a>(3, 4));
			$last = cmGn::last($array, <a href="http://www.php.net/create_function">create_function</a>('$a', 'return in_array(3, $a);'));
			cmAssert::isTrue(<a href="http://www.php.net/in_array">in_array</a>(4, $last));
			try {
				$last = cmGn::last($array, <a href="http://www.php.net/create_function">create_function</a>('$a', 'return in_array(5, $a);'));
				cmAssert::failed();
			}
			catch (cmGnException $e) {
				// The test passed if we got here.
			}
		}
 
		protected function testCmGnLastOrNull() {
			$array = <a href="http://www.php.net/array">Array</a>(<a href="http://www.php.net/array">Array</a>(0, 1), <a href="http://www.php.net/array">Array</a>(2, 3), <a href="http://www.php.net/array">Array</a>(3, 4));
			$last = cmGn::lastOrNull($array, <a href="http://www.php.net/create_function">create_function</a>('$a', 'return in_array(3, $a);'));
			cmAssert::isTrue(<a href="http://www.php.net/in_array">in_array</a>(4, $last));
			$last = cmGn::lastOrNull($array, <a href="http://www.php.net/create_function">create_function</a>('$a', 'return in_array(5, $a);'));
			cmAssert::areEqual(null, $last);
		}
 
		public function test() {
			$this->alert('Starting tests...');
			$methd = <a href="http://www.php.net/get_class_methods">get_class_methods</a>(<a href="http://www.php.net/get_class">get_class</a>($this));
			$methd = $methd ? $methd : <a href="http://www.php.net/array">Array</a>();
			$tests = cmGn::where($methd, <a href="http://www.php.net/create_function">create_function</a>('$m', 'return cmTest::isTest($m);'));
 
			$this->alert(<a href="http://www.php.net/count">count</a>($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 = <a href="http://www.php.net/strpos">strpos</a>($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 + (<a href="http://www.php.net/abs">abs</a>($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 + (<a href="http://www.php.net/abs">abs</a>($code) * -1));
		}
	}
 
	class cmGnException extends cmException {
		const baseCodeModifier = -128000;
 
		public function __construct($message = null, $code = 0) {
			parent::__construct($message,
				cmGnException::baseCodeModifier + (<a href="http://www.php.net/abs">abs</a>($code) * -1));
		}
	}
 
	$test = new cmTest();
	$test->test();
 
?>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

qenn
Engineer
United States United States
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 1 Pinmember ProgramFOX28-Jul-13 4:19 
GeneralMy vote of 1 Pinmemberstikves8-Jan-12 18:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 5 Jan 2012
Article Copyright 2012 by qenn
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid