/**
  * Construct expression
  *
  * @param 	string 	$expression		Expression to create
  * @param 	string 	$defaultName	Default name in expression
  * @param  	bool	$descending		Descending order?
  * @param  	string 	$comparer		Comparer function
  */
 public function __construct($expression, $defaultName = '', $descending = false, $comparer = null)
 {
     // Default name
     $this->_defaultName = $defaultName;
     // Internal expression
     $this->_expression = new PHPLinq_Expression($expression, $defaultName);
     // Comparer function set?
     if (is_null($comparer)) {
         $comparer = 'strcmp';
     }
     // Descending?
     if ($descending) {
         $comparer = '-1 * ' . $comparer;
     }
     $this->_descending = $descending;
     // Compile comparer function
     //     Check http://www.php.net/manual/nl/function.create-function.php#14322 for this chr(0).'$f' approach...
     $f = substr($this->_expression->getFunctionReference(), 1);
     $this->_functionReference = new PHPLinq_Function($defaultName . 'A, ' . $defaultName . 'B', "return {$comparer}(\n\t\t\t\t\tcall_user_func(chr(0).'{$f}', {$defaultName}A),\n\t\t\t\t\tcall_user_func(chr(0).'{$f}', {$defaultName}B)\n\t\t\t\t);");
 }
Ejemplo n.º 2
0
 /**
  * Aggregate
  * 
  * Example: Equivalent of count(): $this->aggregate(0, '$s, $t => $s + 1');
  *
  * @param int $seed	Seed
  * @param string $expression	Expression defining the aggregate
  * @return mixed aggregate
  */
 public function aggregate($seed = 0, $expression)
 {
     $codeExpression = new PHPLinq_Expression($expression);
     $runningValue = $seed;
     foreach ($this->_data as $value) {
         $runningValue = $codeExpression->execute(array($runningValue, $value));
     }
     return $runningValue;
 }