http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 11 LOC: 235 Statements: 84

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


Report generated at 2007-10-08T19:32:24-05:00