| 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 LibreWorks, LLC (http://libreworks.net) |
| 13 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 14 |
|
*/ |
| 15 |
|
namespace Xyster\Data\Symbol; |
| 16 |
|
/** |
| 17 |
|
* A field or column that has some aggregate function applied to it |
| 18 |
|
* |
| 19 |
|
* @category Xyster |
| 20 |
|
* @package Xyster_Data |
| 21 |
|
* @copyright Copyright LibreWorks, LLC (http://libreworks.net) |
| 22 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 23 |
|
*/ |
| 24 |
|
class AggregateField extends Field |
| 25 |
1 |
{ |
| 26 |
|
/** |
| 27 |
|
* The pattern to match aggregate function fields |
| 28 |
|
*/ |
| 29 |
|
const AGGREGATE_REGEX = '/^(?P<function>AVG|MAX|MIN|COUNT|SUM)\((?P<field>[\w\W]*)\)$/i'; |
| 30 |
|
|
| 31 |
|
/** |
| 32 |
|
* @var Aggregate |
| 33 |
|
*/ |
| 34 |
|
protected $_function; |
| 35 |
|
|
| 36 |
|
/** |
| 37 |
|
* Creates a new Aggregate Field |
| 38 |
|
* |
| 39 |
|
* @param Aggregate $function The aggregate function applied |
| 40 |
|
* @param string $name The field name (be it a property, column, whatever) |
| 41 |
|
* @param string $alias The alias for this field |
| 42 |
|
*/ |
| 43 |
|
protected function __construct( Aggregate $function, $name, $alias = null ) |
| 44 |
|
{ |
| 45 |
21 |
parent::__construct($name, $alias); |
| 46 |
21 |
$this->_function = $function; |
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
/** |
| 50 |
|
* Gets the aggregate function associated with this field |
| 51 |
|
* |
| 52 |
|
* @return Aggregate The assigned aggregate function |
| 53 |
|
*/ |
| 54 |
|
public function getFunction() |
| 55 |
|
{ |
| 56 |
19 |
return $this->_function; |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
/** |
| 60 |
|
* String representation of this object |
| 61 |
|
* |
| 62 |
|
* @magic |
| 63 |
|
* @return string |
| 64 |
|
*/ |
| 65 |
|
public function __toString() |
| 66 |
|
{ |
| 67 |
1 |
return $this->_function->getValue() . '(' . parent::__toString() . ')'; |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
/** |
| 71 |
|
* Matches for aggregate functions |
| 72 |
|
* |
| 73 |
|
* @param string $haystack |
| 74 |
|
* @return array |
| 75 |
|
*/ |
| 76 |
|
static public function match( $haystack ) |
| 77 |
|
{ |
| 78 |
147 |
$matches = array(); |
| 79 |
147 |
preg_match(self::AGGREGATE_REGEX, trim($haystack), $matches); |
| 80 |
147 |
return $matches; |
| 81 |
|
} |
| 82 |
1 |
} |