http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 11 LOC: 239 Statements: 91

Source file Statements Methods Total coverage
Set.php 96.7% 100.0% 97.1%
   
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 355
        $this->_columns = new Xyster_Collection_Set();
46
47 355
        if ( $values instanceof Xyster_Collection_Interface ) {
48 71
            if ( !count($this->_columns) && count($values) ) {
49 71
                $first = $values->getIterator()->current();
50 71
                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 69
                $this->_createColumnsFromItem($first);
55 69
            }
56 69
            $this->merge($values);
57 69
        }
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 166
        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 164
        if ( !count($this->_columns) ) {
80 156
            $this->_createColumnsFromItem($item);
81 156
        }
82 164
        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 77
        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 77
        $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 19
        $function = $field->getFunction();
108 19
        switch( strtoupper($function->getValue()) ) {
109 19
            case "MAX":
110 5
                $value = max($this->fetchColumn($field));
111 5
                break;
112 15
            case "MIN":
113 2
                $value = min($this->fetchColumn($field));
114 2
                break;
115 13
            case "SUM":
116 2
                $value = array_sum($this->fetchColumn($field));
117 2
                break;
118 11
            case "AVG":
119 2
                $value = array_sum($this->fetchColumn($field))/count($this->_items);
120 2
                break;
121 9
            case "COUNT":
122 9
            default:
123 9
                $value = count($this->_items);
124 9
        }
125 19
        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 19
        $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
    	$param = ( $sorts instanceof Xyster_Data_Symbol ) ? $sorts : null;
205
206 7
        require_once 'Xyster/Data/Sort/Clause.php';
207 7
        $clause = new Xyster_Data_Sort_Clause($param);
208
209 7
        if ( $param === null ) {
210 4
	        if ( !is_array($sorts) && ! $sorts instanceof Traversable ) {
211 2
	        	$sorts = array($sorts);
212 2
	        }
213 4
	        if ( is_array($sorts) || $sorts instanceof Traversable ) {
214 4
	        	foreach( $sorts as $sort ) {
215 4
	        		if ( ! $sort instanceof Xyster_Data_Sort ) {
216 2
	        			require_once 'Xyster/Data/Set/Exception.php';
217 2
	        			throw new Xyster_Data_Set_Exception('Only Xyster_Data_Sort objects can be used');
218 0
	        		}
219 2
	        		$clause->add($sort);
220 2
	        	}
221 2
	        }
222 2
        }
223
224 5
        require_once 'Xyster/Data/Comparator.php';
225 5
        $this->sort(new Xyster_Data_Comparator($clause));
226
    }
227
228
    /**
229
     * Creates the columns for this set from an array or object
230
     *
231
     * @param mixed $item either an array or an object
232
     */
233
    protected function _createColumnsFromItem( $item )
234
    {
235 164
        foreach( $item as $k => $v ) {
236 68
            $this->addColumn($k);
237 68
        }
238
    }
239
}


Report generated at 2008-03-05T18:27:43-05:00