http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 6 LOC: 159 Statements: 62

Source file Statements Methods Total coverage
Tuple.php 96.8% 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
 * 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 9
	    parent::__construct($contents);
41
42 9
	    $this->_values = $values;
43 9
	    $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 Xyster_Data_Field_Clause $fields
81
	 * @return array
82
	 */
83
	public function toRow( Xyster_Data_Field_Clause $fields )
84
	{
85 7
		$values = array();
86 7
		foreach( $fields as $field ) {
87 7
			$values[$field->getAlias()] = ( $field instanceof Xyster_Data_Field_Aggregate ) ?
88 7
				$this->aggregate($field) : $this->_values[$field->getAlias()];
89 7
		}
90 7
		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 Xyster_Data_Field_Clause $fields  The 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, Xyster_Data_Field_Clause $fields, array $having = null, $limit = 0, $offset = 0 )
105
	{
106 7
		$groups = array();
107 7
		foreach( $fields as $v ) {
108 7
			if ( $v instanceof Xyster_Data_Field_Group ) {
109 6
				$groups[] = $v;
110 6
			}
111 7
		}
112 7
		if ( !count($groups) ) {
113 1
			require_once 'Xyster/Data/Set/Exception.php';
114 1
			throw new Xyster_Data_Set_Exception('You must specify at least one grouped field');
115 0
		}
116
117 6
		$tuples = array();
118 6
		$tupleValues = array();
119 6
		foreach( $collection as $v ) {
120 6
			$groupValues = array();
121 6
			$groupHash = '';
122 6
			foreach( $groups as $group ) {
123 6
				$value = $group->evaluate($v);
124 6
				$groupValues[$group->getAlias()] = $value;
125 6
				$groupHash .= "['".$value."']";
126 6
			}
127 6
			$tupleValues[$groupHash] = $groupValues;
128 6
			if ( !isset($tuples[$groupHash]) ) {
129 6
				$tuples[$groupHash] = array();
130 6
			}
131 6
			$tuples[$groupHash][] = $v;
132 6
		}
133
134 6
		$loffset = 0;
135 6
		require_once 'Xyster/Collection.php';
136 6
		foreach( $tupleValues as $hash=>$values ) {
137 6
			$tuple = new Xyster_Data_Tuple($values,
138 6
			    Xyster_Collection::using($tuples[$hash]));
139 6
			$ok = true;
140 6
			if ( $having ) {
141 6
				foreach( $having as $crit ) {
142 6
					if ( !$crit->evaluate($tuple) ) {
143 1
						$ok = false;
144 1
					}
145 6
				}
146 6
			}
147 6
			if ( $ok ) {
148 6
				if ( $loffset < $offset ) {
149 2
					$loffset++;
150 2
				} else {
151 5
					$rs->add($tuple->toRow($fields));
152 5
					if ( $limit > 0 && count($rs) == $limit ) {
153 1
						break;
154 0
					}
155
				}
156 6
			}
157 6
		}
158
	}
159
}


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