| 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_Collection_Set_Sortable |
| 17 |
|
*/ |
| 18 |
1 |
require_once 'Xyster/Collection/Set/Sortable.php'; |
| 19 |
|
/** |
| 20 |
|
* @see Xyster_Collection_Set |
| 21 |
|
*/ |
| 22 |
1 |
require_once 'Xyster/Collection/Set.php'; |
| 23 |
|
/** |
| 24 |
|
* A set that holds rows and columns |
| 25 |
|
* |
| 26 |
|
* @category Xyster |
| 27 |
|
* @package Xyster_Data |
| 28 |
|
* @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net) |
| 29 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 30 |
|
*/ |
| 31 |
|
class Xyster_Data_Set extends Xyster_Collection_Set_Sortable |
| 32 |
|
{ |
| 33 |
|
/** |
| 34 |
|
* @var Xyster_Collection_Set |
| 35 |
|
*/ |
| 36 |
|
protected $_columns; |
| 37 |
|
|
| 38 |
|
/** |
| 39 |
|
* Creates a new data set |
| 40 |
|
* |
| 41 |
|
* @param Xyster_Collection_Interface $values The values to add |
| 42 |
|
*/ |
| 43 |
|
public function __construct( Xyster_Collection_Interface $values = null ) |
| 44 |
|
{ |
| 45 |
293 |
$this->_columns = new Xyster_Collection_Set(); |
| 46 |
|
|
| 47 |
293 |
if ( $values instanceof Xyster_Collection_Interface ) { |
| 48 |
35 |
if ( !count($this->_columns) && count($values) ) { |
| 49 |
35 |
$first = $values->getIterator()->current(); |
| 50 |
35 |
if ( !is_array($first) && !is_object($first) ) { |
| 51 |
2 |
require_once 'Xyster/Data/Set/Exception.php'; |
| 52 |
2 |
throw new Xyster_Data_Set_Exception('This set can only contain arrays or objects'); |
| 53 |
|
} |
| 54 |
33 |
$this->_createColumnsFromItem($first); |
| 55 |
33 |
} |
| 56 |
33 |
$this->merge($values); |
| 57 |
33 |
} |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
/** |
| 61 |
|
* Adds an item to the set |
| 62 |
|
* |
| 63 |
|
* This collection doesn't accept duplicate values, and will return false |
| 64 |
|
* if the provided value is already in the collection. |
| 65 |
|
* |
| 66 |
|
* It can only accept arrays or objects as items, otherwise it will throw a |
| 67 |
|
* Xyster_Data_Set_Exception. |
| 68 |
|
* |
| 69 |
|
* @param mixed $item The item to add |
| 70 |
|
* @return boolean Whether the set changed as a result of this method |
| 71 |
|
* @throws Xyster_Data_Set_Exception if the collection cannot contain the value |
| 72 |
|
*/ |
| 73 |
|
public function add( $item ) |
| 74 |
|
{ |
| 75 |
130 |
if ( !is_array($item) && !is_object($item) ) { |
| 76 |
2 |
require_once 'Xyster/Data/Set/Exception.php'; |
| 77 |
2 |
throw new Xyster_Data_Set_Exception('This set can only contain arrays or objects'); |
| 78 |
0 |
} |
| 79 |
128 |
if ( !count($this->_columns) ) { |
| 80 |
126 |
$this->_createColumnsFromItem($item); |
| 81 |
126 |
} |
| 82 |
128 |
return parent::add($item); |
| 83 |
|
} |
| 84 |
|
/** |
| 85 |
|
* Adds a column to the set |
| 86 |
|
* |
| 87 |
|
* @param string $column The name of the column |
| 88 |
|
* @throws Xyster_Data_Set_Exception if the method is called after the set contains items |
| 89 |
|
*/ |
| 90 |
|
public function addColumn( $column ) |
| 91 |
|
{ |
| 92 |
68 |
if ( count($this->_items) ) { |
| 93 |
2 |
require_once 'Xyster/Data/Set/Exception.php'; |
| 94 |
2 |
throw new Xyster_Data_Set_Exception('Columns cannot be added once items have been added'); |
| 95 |
0 |
} |
| 96 |
68 |
$this->_columns->add(Xyster_Data_Field::named($column)); |
| 97 |
|
} |
| 98 |
|
/** |
| 99 |
|
* Perform an aggregate function on a column |
| 100 |
|
* |
| 101 |
|
* @param Xyster_Data_Aggregate $function |
| 102 |
|
* @param Xyster_Data_Field|string $field |
| 103 |
|
* @return mixed |
| 104 |
|
*/ |
| 105 |
|
public function aggregate( Xyster_Data_Field_Aggregate $field ) |
| 106 |
|
{ |
| 107 |
16 |
$function = $field->getFunction(); |
| 108 |
16 |
switch( strtoupper($function->getValue()) ) { |
| 109 |
16 |
case "MAX": |
| 110 |
5 |
$value = max($this->fetchColumn($field)); |
| 111 |
5 |
break; |
| 112 |
12 |
case "MIN": |
| 113 |
2 |
$value = min($this->fetchColumn($field)); |
| 114 |
2 |
break; |
| 115 |
10 |
case "SUM": |
| 116 |
2 |
$value = array_sum($this->fetchColumn($field)); |
| 117 |
2 |
break; |
| 118 |
8 |
case "AVG": |
| 119 |
2 |
$value = array_sum($this->fetchColumn($field))/count($this->_items); |
| 120 |
2 |
break; |
| 121 |
6 |
case "COUNT": |
| 122 |
6 |
default: |
| 123 |
6 |
$value = count($this->_items); |
| 124 |
6 |
} |
| 125 |
16 |
return $value; |
| 126 |
|
} |
| 127 |
|
/** |
| 128 |
|
* Converts an entire column of values to an array |
| 129 |
|
* |
| 130 |
|
* @param mixed $field A {@link Xyster_Data_Field} or the name of the field to fetch |
| 131 |
|
* @return array The fetched values from the column |
| 132 |
|
*/ |
| 133 |
|
public function fetchColumn( $field ) |
| 134 |
|
{ |
| 135 |
15 |
if (! $field instanceof Xyster_Data_Field ) { |
| 136 |
2 |
$field = Xyster_Data_Field::named($field); |
| 137 |
2 |
} |
| 138 |
15 |
$values = array(); |
| 139 |
15 |
foreach( $this->_items as $v ) { |
| 140 |
15 |
$values[] = $field->evaluate($v); |
| 141 |
15 |
} |
| 142 |
15 |
return $values; |
| 143 |
|
} |
| 144 |
|
/** |
| 145 |
|
* Returns the first column of the first row of a statement |
| 146 |
|
* |
| 147 |
|
* @return mixed The value of the first column in the first row |
| 148 |
|
*/ |
| 149 |
|
public function fetchOne() |
| 150 |
|
{ |
| 151 |
2 |
$value = null; |
| 152 |
2 |
if ( $item = current($this->_items) ) { |
| 153 |
2 |
$value = current((array) $item); |
| 154 |
2 |
} |
| 155 |
2 |
return $value; |
| 156 |
|
} |
| 157 |
|
/** |
| 158 |
|
* Converts 2 fields into an associative array |
| 159 |
|
* |
| 160 |
|
* @param mixed $key A {@link Xyster_Data_Field} or the name of the field to fetch |
| 161 |
|
* @param mixed $value A {@link Xyster_Data_Field} or the name of the field to fetch |
| 162 |
|
* @return array The translated fields |
| 163 |
|
*/ |
| 164 |
|
public function fetchPairs( $key, $value ) |
| 165 |
|
{ |
| 166 |
4 |
$field1 = (! $key instanceof Xyster_Data_Field) ? |
| 167 |
4 |
Xyster_Data_Field::named($key) : $key; |
| 168 |
4 |
$field2 = (! $value instanceof Xyster_Data_Field) ? |
| 169 |
4 |
Xyster_Data_Field::named($value) : $value; |
| 170 |
4 |
$pairs = array(); |
| 171 |
4 |
foreach( $this->_items as $v ) { |
| 172 |
4 |
$pairs[ $field1->evaluate($v) ] = $field2->evaluate($v); |
| 173 |
4 |
} |
| 174 |
4 |
return $pairs; |
| 175 |
|
} |
| 176 |
|
/** |
| 177 |
|
* Removes elements in the collection based on a criteria |
| 178 |
|
* |
| 179 |
|
* @param Xyster_Data_Criterion $criteria The criteria for filtering |
| 180 |
|
*/ |
| 181 |
|
public function filter( Xyster_Data_Criterion $criteria ) |
| 182 |
|
{ |
| 183 |
16 |
$this->_items = array_values(array_filter($this->_items, array($criteria, 'evaluate'))); |
| 184 |
|
} |
| 185 |
|
/** |
| 186 |
|
* Gets the columns that have been added to the set |
| 187 |
|
* |
| 188 |
|
* @return Xyster_Collection_Set |
| 189 |
|
*/ |
| 190 |
|
public function getColumns() |
| 191 |
|
{ |
| 192 |
4 |
return Xyster_Collection::fixedSet($this->_columns); |
| 193 |
|
} |
| 194 |
|
/** |
| 195 |
|
* Sorts the collection by one or more columns and directions |
| 196 |
|
* |
| 197 |
|
* The $sorts parameter can either be a single {@link Xyster_Data_Sort} or |
| 198 |
|
* an array containing multiple. |
| 199 |
|
* |
| 200 |
|
* @param mixed $sorts The sorts to include |
| 201 |
|
*/ |
| 202 |
|
public function sortBy( $sorts ) |
| 203 |
|
{ |
| 204 |
7 |
if ( !is_array($sorts) ) { |
| 205 |
4 |
$sorts = array($sorts); |
| 206 |
4 |
} |
| 207 |
|
|
| 208 |
7 |
foreach( $sorts as $v ) { |
| 209 |
7 |
if (! $v instanceof Xyster_Data_Sort ) { |
| 210 |
2 |
require_once 'Xyster/Data/Set/Exception.php'; |
| 211 |
2 |
throw new Xyster_Data_Set_Exception('The argument must be one or more Xyster_Data_Sort objects'); |
| 212 |
0 |
} |
| 213 |
5 |
} |
| 214 |
|
|
| 215 |
5 |
require_once 'Xyster/Data/Comparator.php'; |
| 216 |
5 |
$this->sort( new Xyster_Data_Comparator($sorts) ); |
| 217 |
|
} |
| 218 |
|
|
| 219 |
|
/** |
| 220 |
|
* Creates the columns for this set from an array or object |
| 221 |
|
* |
| 222 |
|
* @param mixed $item either an array or an object |
| 223 |
|
*/ |
| 224 |
|
protected function _createColumnsFromItem( $item ) |
| 225 |
|
{ |
| 226 |
128 |
foreach( $item as $k => $v ) { |
| 227 |
62 |
$this->addColumn($k); |
| 228 |
62 |
} |
| 229 |
|
} |
| 230 |
|
} |