http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 6 LOC: 168 Statements: 66

Source file Statements Methods Total coverage
Tuple.php 95.5% 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
 * Xyster_Data_Set
22
 */
23 1
require_once 'Xyster/Data/Set.php';
24
/**
25
 * A set that holds rows and columns
26
 *
27
 * @category  Xyster
28
 * @package   Xyster_Data
29
 * @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org)
30
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
31
 */
32
class Xyster_Data_Tuple extends Xyster_Data_Set
33
{
34
    protected $_names = array();
35
	protected $_values = array();
36
37
	/**
38
	 * Creates a new tuple
39
	 *
40
	 * @param array $values An associative array of group names and their values
41
	 * @param Xyster_Collection_Interface $contents The objects or arrays to add to the tuple
42
	 */
43
	public function __construct( array $values, Xyster_Collection_Interface $contents = null )
44
	{
45 6
	    parent::__construct($contents);
46
47 6
	    $this->_values = $values;
48 6
	    $this->_names = array_keys($values);
49
	}
50
51
	/**
52
	 * Gets the names of the groups
53
	 *
54
	 * @return array
55
	 */
56
	public function getNames()
57
	{
58 1
		return $this->_names;
59
	}
60
61
	/**
62
	 * Gets the value of a group
63
	 *
64
	 * @param string $name  The group name
65
	 * @return string
66
	 */
67
	public function getValue($name)
68
	{
69 1
		return $this->_values[$name];
70
	}
71
72
	/**
73
	 * Gets the values for the groups
74
	 *
75
	 * @return array
76
	 */
77
	public function getValues()
78
	{
79 1
		return $this->_values;
80
	}
81
82
	/**
83
	 * Flattens the tuple into a data row
84
	 *
85
	 * @param array $fields
86
	 * @return array
87
	 */
88
	public function toRow( array $fields )
89
	{
90 4
		$values = array();
91 4
		foreach( $fields as $field ) {
92 4
			$values[$field->getAlias()] = ( $field instanceof Xyster_Data_Field_Aggregate ) ?
93 4
				$this->aggregate($field) : $this->_values[$field->getAlias()];
94 4
		}
95 4
		return $values;
96
	}
97
98
	/**
99
	 * Creates the Tuples for a collection
100
	 *
101
	 * @param Xyster_Data_Set $rs  The dataset to add rows representing the tuples
102
	 * @param mixed $collection  The collection of objects/hashtables to use
103
	 * @param array $fields  An array of {@link Xyster_Data_Field} objects to evaluate
104
	 * @param array $having  Optional. An array of {@link Xyster_Data_Criterion} objects
105
	 * @param int $limit  Optional. The maximum number of tuples to create
106
	 * @param int $offset  Optional.  The number of tuples to skip before adding
107
	 * @throws Xyster_Data_Set_Exception if there are no grouped columns in the $fields array
108
	 */
109
	static public function makeTuples( Xyster_Data_Set $rs, $collection, array $fields, array $having = null, $limit = 0, $offset = 0 )
110
	{
111 5
		$groups = array();
112 5
		foreach( $fields as $v ) {
113 5
			if (! $v instanceof Xyster_Data_Field ) {
114 1
				require_once 'Xyster/Data/Set/Exception.php';
115 1
				throw new Xyster_Data_Set_Exception('The $fields array must only contain Xyster_Data_Field objects');
116 0
			}
117 5
			if ( $v instanceof Xyster_Data_Field_Group ) {
118 4
				$groups[] = $v;
119 4
			}
120 5
		}
121 4
		if ( !count($groups) ) {
122 1
			require_once 'Xyster/Data/Set/Exception.php';
123 1
			throw new Xyster_Data_Set_Exception('You must specify at least one grouped field');
124 0
		}
125
126 3
		$tuples = array();
127 3
		$tupleValues = array();
128 3
		foreach( $collection as $v ) {
129 3
			$groupValues = array();
130 3
			$groupHash = '';
131 3
			foreach( $groups as $group ) {
132 3
				$value = $group->evaluate($v);
133 3
				$groupValues[$group->getAlias()] = $value;
134 3
				$groupHash .= "['".$value."']";
135 3
			}
136 3
			$tupleValues[$groupHash] = $groupValues;
137 3
			if ( !isset($tuples[$groupHash]) ) {
138 3
				$tuples[$groupHash] = array();
139 3
			}
140 3
			$tuples[$groupHash][] = $v;
141 3
		}
142
143 3
		$loffset = 0;
144 3
		require_once 'Xyster/Collection.php';
145 3
		foreach( $tupleValues as $hash=>$values ) {
146 3
			$tuple = new Xyster_Data_Tuple($values,
147 3
			    Xyster_Collection::using($tuples[$hash]));
148 3
			$ok = true;
149 3
			if ( $having ) {
150 3
				foreach( $having as $crit ) {
151 3
					if ( !$crit->evaluate($tuple) ) {
152 1
						$ok = false;
153 1
					}
154 3
				}
155 3
			}
156 3
			if ( $ok ) {
157 3
				if ( $loffset < $offset ) {
158 2
					$loffset++;
159 2
				} else {
160 2
					$rs->add($tuple->toRow($fields));
161 2
					if ( $limit > 0 && count($rs) == $limit ) {
162 1
						break;
163 0
					}
164
				}
165 3
			}
166 3
		}
167
	}
168
}


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