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

Source for file OrderByExpression.php

Documentation is available at OrderByExpression.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. /** PHPLinq_OrderByExpression */
  33. require_once('PHPLinq/Expression.php');
  34.  
  35.  
  36. /**
  37.  * PHPLinq_OrderByExpression
  38.  *
  39.  * @category   PHPLinq
  40.  * @package    PHPLinq
  41.  * @copyright  Copyright (c) 2008 - 2009 PHPLinq (http://www.codeplex.com/PHPLinq)
  42.  */
  43.     /**
  44.      * Internal function reference
  45.      *
  46.      * @var PHPLinq_Function 
  47.      */
  48.     private $_functionReference;
  49.     
  50.     /**
  51.      * Default name
  52.      * 
  53.      * @var string 
  54.      */
  55.     private $_defaultName = null;
  56.     
  57.     /**
  58.      * Internal expression reference
  59.      * 
  60.      * @var PHPLinq_Expression 
  61.      */
  62.     private $_expression = null;
  63.     
  64.     /**
  65.      * Descending?
  66.      *
  67.      * @var bool 
  68.      */
  69.     private $_descending = false;
  70.     
  71.     /**
  72.      * Construct expression
  73.      *
  74.      * @param     string     $expression        Expression to create
  75.      * @param     string     $defaultName    Default name in expression
  76.      * @param      bool    $descending        Descending order?
  77.      * @param      string     $comparer        Comparer function
  78.      */
  79.     public function __construct($expression$defaultName ''$descending false$comparer null{
  80.         // Default name
  81.         $this->_defaultName = $defaultName;
  82.         
  83.         // Internal expression
  84.         $this->_expression = new PHPLinq_Expression($expression$defaultName);
  85.         
  86.         // Comparer function set?
  87.         if (is_null($comparer)) {
  88.             $comparer 'strcmp';
  89.         }
  90.         
  91.         // Descending?
  92.         if ($descending{
  93.             $comparer '-1 * ' $comparer;
  94.         }
  95.         $this->_descending = $descending;
  96.         
  97.         // Compile comparer function
  98.         //     Check http://www.php.net/manual/nl/function.create-function.php#14322 for this chr(0).'$f' approach...
  99.         $f substr($this->_expression->getFunctionReference()1)
  100.         $this->_functionReference = new PHPLinq_Function(
  101.                 $defaultName 'A, ' $defaultName 'B',
  102.                 "return $comparer(
  103.                     call_user_func(chr(0).'$f', {$defaultName}A),
  104.                     call_user_func(chr(0).'$f', {$defaultName}B)
  105.                 );"
  106.         );    
  107.     }
  108.     
  109.     /**
  110.      * Execute expression
  111.      * 
  112.      * @param     mixed    $value    Value to use as expression parameter
  113.      * @return     mixed            Expression result
  114.      */
  115.     public function execute($value{
  116.         // Unused...
  117.         return $value;
  118.     }
  119.     
  120.     /**
  121.      * Get default name
  122.      *
  123.      * @return string 
  124.      */
  125.     public function getDefaultName({
  126.         return $this->_defaultName;
  127.     }
  128.     
  129.     /**
  130.      * Get function
  131.      *
  132.      * @return PHPLinq_Function 
  133.      */
  134.     public function getFunction({
  135.         return $this->_functionReference;
  136.     }
  137.     
  138.     /**
  139.      * Get function reference
  140.      *
  141.      * @return mixed 
  142.      */
  143.     public function getFunctionReference({
  144.         return $this->_functionReference->getFunctionReference();
  145.     }
  146.     
  147.     /**
  148.      * Get expression
  149.      *
  150.      * @return PHPLinq_Expression 
  151.      */
  152.     public function getExpression({
  153.         return $this->_expression;
  154.     }
  155.     
  156.     /**
  157.      * Descending?
  158.      *
  159.      * @return boolean 
  160.      */
  161.     public function isDescending({
  162.         return $this->_descending;
  163.     }
  164. }

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