Created
May 11, 2012 03:01
-
-
Save cgutierrez/2657231 to your computer and use it in GitHub Desktop.
Blackbe.lt - Lithium Method Filters
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
<?php | |
PriceCalculator::applyFilter('calculate', function($self, $args, $chain) { | |
// code here will get executed before the calculation has occurred | |
extract($args); //$qty, $item | |
$total = $chain->next($self, $args, $chain); | |
if (date('n') === "6") { | |
$total += 10; | |
} | |
// code here will get executed after the calculation has occurred | |
return $total; | |
}); | |
// get a fake item object | |
$item = new Item(); | |
$item->price = 25.00; | |
$item->name = "Shoes"; | |
echo PriceCalculator::calculate(4, $item); // 25 * 4 + 10 = 110 | |
?> |
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
<?php | |
class PriceCalculator extends \lithium\core\StaticObject { | |
public static function calculate($qty, $item) { | |
$price = $qty * $item->price; | |
return $price; | |
} | |
} | |
?> |
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
<?php | |
class PriceCalculator extends \lithium\core\StaticObject { | |
public static function calculate($qty, $item) { | |
// make an array from the arguments | |
$args = compact('qty', 'item'); | |
return static::_filter( | |
__METHOD__, // the name of current method | |
$args, // the arguments passed to the current method | |
function($self, $args) { | |
// extract the arguments in to individual variables in to the local scope | |
extract($args); | |
$price = $qty * $item->price; | |
return $price; | |
} | |
); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment