| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* This source file is subject to the new BSD license that is bundled |
| 6 |
|
* with this package in the file LICENSE.txt. |
| 7 |
|
* It is also available through the world-wide-web at this URL: |
| 8 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 9 |
|
* |
| 10 |
|
* @category Xyster |
| 11 |
|
* @package Xyster_Data |
| 12 |
|
* @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net) |
| 13 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 14 |
|
*/ |
| 15 |
|
/** |
| 16 |
|
* @see Xyster_Data_Field |
| 17 |
|
*/ |
| 18 |
1 |
require_once 'Xyster/Data/Field.php'; |
| 19 |
|
/** |
| 20 |
|
* A field or column that has some aggregate function applied to it |
| 21 |
|
* |
| 22 |
|
* @category Xyster |
| 23 |
|
* @package Xyster_Data |
| 24 |
|
* @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net) |
| 25 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 26 |
|
*/ |
| 27 |
|
class Xyster_Data_Field_Aggregate extends Xyster_Data_Field |
| 28 |
|
{ |
| 29 |
|
/** |
| 30 |
|
* The pattern to match aggregate function fields |
| 31 |
|
* |
| 32 |
|
*/ |
| 33 |
|
const AGGREGATE_REGEX = '/^(?P<function>AVG|MAX|MIN|COUNT|SUM)\((?P<field>[\w\W]*)\)$/i'; |
| 34 |
|
|
| 35 |
|
/** |
| 36 |
|
* @var Xyster_Data_Aggregate |
| 37 |
|
*/ |
| 38 |
|
protected $_function; |
| 39 |
|
|
| 40 |
|
/** |
| 41 |
|
* Creates a new Aggregate Field |
| 42 |
|
* |
| 43 |
|
* @param Xyster_Data_Aggregate $function The aggregate function applied |
| 44 |
|
* @param string $name The field name (be it a property, column, whatever) |
| 45 |
|
* @param string $alias The alias for this field |
| 46 |
|
*/ |
| 47 |
|
protected function __construct( Xyster_Data_Aggregate $function, $name, $alias = null ) |
| 48 |
|
{ |
| 49 |
42 |
parent::__construct($name, $alias); |
| 50 |
42 |
$this->_function = $function; |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
/** |
| 54 |
|
* Evaluates the reference for the given object |
| 55 |
|
* |
| 56 |
|
* @param mixed $object |
| 57 |
|
* @return mixed |
| 58 |
|
*/ |
| 59 |
|
public function evaluate( $object ) |
| 60 |
|
{ |
| 61 |
16 |
return ( $object instanceof Xyster_Data_Set ) ? |
| 62 |
16 |
$object->aggregate($this) : parent::evaluate($object); |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
/** |
| 66 |
|
* Gets the aggregate function associated with this field |
| 67 |
|
* |
| 68 |
|
* @return Xyster_Data_Aggregate The assigned aggregate function |
| 69 |
|
*/ |
| 70 |
|
public function getFunction() |
| 71 |
|
{ |
| 72 |
31 |
return $this->_function; |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
/** |
| 76 |
|
* String representation of this object |
| 77 |
|
* |
| 78 |
|
* @magic |
| 79 |
|
* @return string |
| 80 |
|
*/ |
| 81 |
|
public function __toString() |
| 82 |
|
{ |
| 83 |
1 |
return $this->_function->getValue() . '(' . parent::__toString() . ')'; |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
/** |
| 87 |
|
* Matches for aggregate functions |
| 88 |
|
* |
| 89 |
|
* @param string $haystack |
| 90 |
|
* @return array |
| 91 |
|
*/ |
| 92 |
|
static public function match( $haystack ) |
| 93 |
|
{ |
| 94 |
325 |
$matches = array(); |
| 95 |
325 |
preg_match(self::AGGREGATE_REGEX, trim($haystack), $matches); |
| 96 |
325 |
return $matches; |
| 97 |
|
} |
| 98 |
|
} |