Created
December 15, 2011 21:59
-
-
Save hjr3/1483101 to your computer and use it in GitHub Desktop.
SPL FilterIterator example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$items = array( | |
array( | |
'name' => 'Pants', | |
'available' => 1, | |
'sold' => 1, | |
), | |
array( | |
'name' => 'Shoes', | |
'available' => 1, | |
'sold' => 0, | |
), | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$available = __::select($items, function($item) { | |
if (($item['available'] - $item['sold']) > 0) { | |
return true; | |
} | |
return false; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$available = function($item) { | |
if (($item['available'] - $item['sold']) > 0) { | |
echo $item['name'], PHP_EOL; | |
} | |
}; | |
__::select($items, $available); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$available = array(); | |
foreach ($items as $item) { | |
if (($item['available'] - $item['sold']) > 0) { | |
$available[] = $item; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SellableItems extends FilterIterator | |
{ | |
public function accept() | |
{ | |
$current = $this->current(); | |
if (($current['available'] - $current['soldout']) > 0) { | |
return true; | |
} | |
return false; | |
} | |
} | |
$available = new SellableItems(new ArrayIterator($items)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment