http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

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


Report generated at 2010-10-18T17:19:49-04:00