http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 6 LOC: 156 Statements: 61
Legend: executednot executeddead code
Source file Statements Methods Total coverage
Tuple.php 100.0% 100.0% 100.0%
 
1
<?php
2
3
/**
4
 * Xyster Framework
5
 *
6
 * This source file is subject to the new BSD license that is bundled
7
 * with this package in the file LICENSE.txt.
8
 * It is also available through the world-wide-web at this URL:
9
 * http://www.opensource.org/licenses/bsd-license.php
10
 *
11
 * @category  Xyster
12
 * @package   Xyster_Data
13
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
14
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
15
 * @version   $Id: Tuple.php 418 2010-10-18 21:40:08Z jonathanhawk $
16
 */
17
namespace Xyster\Data;
18
/**
19
 * A set that holds rows and columns
20
 *
21
 * @category  Xyster
22
 * @package   Xyster_Data
23
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
24
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
25
 */
26
class Tuple extends Set
27 1
{
28
    protected $_names = array();
29
    protected $_values = array();
30
31
    /**
32
     * Creates a new tuple
33
     *
34
     * @param array $values An associative array of group names and their values
35
     * @param \Xyster\Collection\ICollection $contents The objects or arrays to add to the tuple
36
     */
37
    public function __construct(array $values, \Xyster\Collection\ICollection $contents = null)
38
    {
39 3
        parent::__construct($contents);
40
41 3
        $this->_values = $values;
42 3
        $this->_names = array_keys($values);
43
    }
44
45
    /**
46
     * Gets the names of the groups
47
     *
48
     * @return array
49
     */
50
    public function getNames()
51
    {
52 1
        return $this->_names;
53
    }
54
55
    /**
56
     * Gets the value of a group
57
     *
58
     * @param string $name  The group name
59
     * @return string
60
     */
61
    public function getValue($name)
62
    {
63 1
        return $this->_values[$name];
64
    }
65
66
    /**
67
     * Gets the values for the groups
68
     *
69
     * @return array
70
     */
71
    public function getValues()
72
    {
73 1
        return $this->_values;
74
    }
75
76
    /**
77
     * Flattens the tuple into a data row
78
     *
79
     * @param Symbol\FieldClause $fields
80
     * @return array
81
     */
82
    public function toRow(Symbol\FieldClause $fields)
83
    {
84 2
        $values = array();
85 2
        foreach ($fields as $field) {
86 2
            $values[$field->getAlias()] = ( $field instanceof Symbol\AggregateField ) ?
87 2
                    $this->aggregate($field) : $this->_values[$field->getAlias()];
88 2
        }
89 2
        return $values;
90
    }
91
92
    /**
93
     * Creates the Tuples for a collection
94
     *
95
     * @param Set $rs  The dataset to add rows representing the tuples
96
     * @param mixed $collection  The collection of objects/hashtables to use
97
     * @param Symbol\FieldClause $fields  The field objects to evaluate
98
     * @param array $having  Optional. An array of {@link Symbol\Criterion} objects
99
     * @param int $limit  Optional. The maximum number of tuples to create
100
     * @param int $offset  Optional.  The number of tuples to skip before adding
101
     * @throws \Xyster\Data\DataException if there are no grouped columns in the $fields array
102
     */
103
    static public function makeTuples(Set $rs, $collection, Symbol\FieldClause $fields, array $having = null, $limit = 0, $offset = 0)
104
    {
105 2
        $groups = array();
106 2
        foreach ($fields as $v) {
107 2
            if ($v instanceof Symbol\GroupField) {
108 1
                $groups[] = $v;
109 1
            }
110 2
        }
111 2
        if (!count($groups)) {
112 1
            throw new \Xyster\Data\DataException('You must specify at least one grouped field');
113
        }
114
115 1
        $tuples = array();
116 1
        $tupleValues = array();
117 1
        foreach ($collection as $v) {
118 1
            $groupValues = array();
119 1
            $groupHash = '';
120 1
            foreach ($groups as $group) {
121 1
                $value = Symbol\Evaluator::get($v, $group);
122 1
                $groupValues[$group->getAlias()] = $value;
123 1
                $groupHash .= "['" . $value . "']";
124 1
            }
125 1
            $tupleValues[$groupHash] = $groupValues;
126 1
            if (!isset($tuples[$groupHash])) {
127 1
                $tuples[$groupHash] = array();
128 1
            }
129 1
            $tuples[$groupHash][] = $v;
130 1
        }
131
132 1
        $loffset = 0;
133 1
        foreach ($tupleValues as $hash => $values) {
134 1
            $tuple = new self($values,
135 1
                    \Xyster\Collection\Collection::using($tuples[$hash]));
136 1
            $ok = true;
137 1
            if ($having) {
138 1
                foreach ($having as $crit) {
139 1
                    if (!$crit->evaluate($tuple)) {
140 1
                        $ok = false;
141 1
                    }
142 1
                }
143 1
            }
144 1
            if ($ok) {
145 1
                if ($loffset < $offset) {
146 1
                    $loffset++;
147 1
                } else {
148 1
                    $rs->add($tuple->toRow($fields));
149 1
                    if ($limit > 0 && count($rs) == $limit) {
150 1
                        break;
151
                    }
152
                }
153 1
            }
154 1
        }
155
    }
156 1
}


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