PHPLinq
[ class tree: PHPLinq ] [ index: PHPLinq ] [ all elements ]

Source for file Expression.php

Documentation is available at Expression.php

  1. <?php
  2. /**
  3.  * PHPLinq
  4.  *
  5.  * Copyright (c) 2008 - 2009 PHPLinq
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPLinq
  22.  * @package    PHPLinq
  23.  * @copyright  Copyright (c) 2008 - 2009 PHPLinq (http://www.codeplex.com/PHPLinq)
  24.  * @license    http://www.gnu.org/licenses/lgpl.txt    LGPL
  25.  * @version    0.4.0, 2009-01-27
  26.  */
  27.  
  28.  
  29. /** PHPLinq_Function */
  30. require_once('PHPLinq/Function.php');
  31.  
  32.  
  33. /**
  34.  * PHPLinq_Expression
  35.  *
  36.  * @category   PHPLinq
  37.  * @package    PHPLinq
  38.  * @copyright  Copyright (c) 2008 - 2009 PHPLinq (http://www.codeplex.com/PHPLinq)
  39.  */
  40.     /**
  41.      * Default name
  42.      * 
  43.      * @var string 
  44.      */
  45.     private $_defaultName = null;
  46.     
  47.     /**
  48.      * Internal function reference
  49.      *
  50.      * @var PHPLinq_Function 
  51.      */
  52.     private $_functionReference;
  53.     
  54.     /**
  55.      * Construct expression
  56.      *
  57.      * @param     string $expression        Expression to create
  58.      * @param     string $defaultName        Default name in expression
  59.      */
  60.     public function __construct($expression$defaultName ''{
  61.         // Default name
  62.         $this->_defaultName = $defaultName;
  63.         
  64.         // Split out variable name
  65.         if (strpos($expression'=>'!== false && strpos($expression'$'strpos($expression'=>')) {
  66.             list($defaultName$expressionexplode('=>'$expression2);
  67.         }
  68.  
  69.         // Clean expression
  70.         $defaultName     trim($defaultName);
  71.         $expression     trim($expression);
  72.         
  73.         // Convert anonymous constructors
  74.         $expression        str_ireplace('new {''new{'$expression);
  75.         $expression        str_ireplace('new{''(object)array('$expression);
  76.         $expression        str_ireplace('}'')'$expression);
  77.  
  78.         // Create PHPLinq_Function instance
  79.         $this->_functionReference = new PHPLinq_Function($defaultName'return ' $expression ';');
  80.     }
  81.     
  82.     /**
  83.      * Execute expression
  84.      * 
  85.      * @param     mixed    $value    Value to use as expression parameter
  86.      * @return     mixed            Expression result
  87.      */
  88.     public function execute($value{
  89.         if (is_array($value)) {
  90.             return call_user_func_array($this->getFunctionReference()$value);
  91.         else {
  92.             return call_user_func($this->getFunctionReference()$value);
  93.         }
  94.     }
  95.     
  96.     /**
  97.      * Get default name
  98.      *
  99.      * @return string 
  100.      */
  101.     public function getDefaultName({
  102.         return $this->_defaultName;
  103.     }
  104.     
  105.     /**
  106.      * Get function
  107.      *
  108.      * @return PHPLinq_Function 
  109.      */
  110.     public function getFunction({
  111.         return $this->_functionReference;
  112.     }
  113.     
  114.     /**
  115.      * Get function reference
  116.      *
  117.      * @return mixed 
  118.      */
  119.     public function getFunctionReference({
  120.         return $this->_functionReference->getFunctionReference();
  121.     }
  122. }

Documentation generated on Tue, 27 Jan 2009 08:29:22 +0100 by phpDocumentor 1.4.1