Skip to content

Instantly share code, notes, and snippets.

@cgutierrez
Created May 11, 2012 03:01
Show Gist options
  • Save cgutierrez/2657231 to your computer and use it in GitHub Desktop.
Save cgutierrez/2657231 to your computer and use it in GitHub Desktop.
Blackbe.lt - Lithium Method Filters
<?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
?>
<?php
class PriceCalculator extends \lithium\core\StaticObject {
public static function calculate($qty, $item) {
$price = $qty * $item->price;
return $price;
}
}
?>
<?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