http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 6 LOC: 163 Statements: 66

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


Report generated at 2008-01-20T12:13:39-05:00