http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 11 LOC: 215 Statements: 54

Source file Statements Methods Total coverage
Junction.php 96.3% 100.0% 96.9%
   
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
 * @version   $Id$
15
 */
16
/**
17
 * @see Xyster_Data_Criterion
18
 */
19 1
require_once 'Xyster/Data/Criterion.php';
20
/**
21
 * @see Xyster_Data_Clause_Interface
22
 */
23 1
require_once 'Xyster/Data/Clause/Interface.php';
24
/**
25
 * A Junction is an infix expression of {@link Xyster_Data_Criterion} objects
26
 *
27
 * A typical example of a junction is a SQL where clause:
28
 * <code>( <var>expression</var> AND <var>expression</var> )</code>
29
 *
30
 * @category  Xyster
31
 * @package   Xyster_Data
32
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
33
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
34
 */
35
class Xyster_Data_Junction extends Xyster_Data_Criterion implements Xyster_Data_Clause_Interface
36
{
37
    /**
38
     * The criteria in the junction (an array of {@link Xyster_Data_Criterion})
39
     *
40
     * @var array
41
     */
42
    protected $_criteria = array();
43
44
    /**
45
     * The operator ('AND' or 'OR')
46
     *
47
     * @var string
48
     */
49
    protected $_operator;
50
51
    /**
52
     * Creates a new junction
53
     *
54
     * @param Xyster_Data_Criterion $lc
55
     * @param Xyster_Data_Criterion $rc
56
     * @param string $operator
57
     */
58
    protected function __construct( Xyster_Data_Criterion $lc, Xyster_Data_Criterion $rc, $operator )
59
    {
60 55
        $this->_operator = $operator;
61
62 55
        if ( $lc instanceof Xyster_Data_Junction && $lc->_operator == $operator ) {
63 2
            foreach( $lc->_criteria as $criterion ) {
64 2
                $this->_criteria[] = $criterion;
65 2
            }
66 2
        } else {
67 55
            $this->_criteria[] = $lc;
68
        }
69
70 55
        if ( $rc instanceof Xyster_Data_Junction && $rc->_operator == $operator ) {
71 1
            foreach( $rc->_criteria as $criterion ) {
72 1
                $this->_criteria[] = $criterion;
73 1
            }
74 1
        } else {
75 55
            $this->_criteria[] = $rc;
76
        }
77
    }
78
79
    /**
80
     * Adds a Criterion to this Junction
81
     *
82
     * @param Xyster_Data_Criterion $c
83
     * @return Xyster_Data_Junction
84
     */
85
    public function add( Xyster_Data_Criterion $c )
86
    {
87 11
        if ( $c instanceof Xyster_Data_Junction && $c->_operator == $this->_operator ) {
88 1
            foreach( $c->_criteria as $criterion ) {
89 1
                $this->_criteria[] = $criterion;
90 1
            }
91 1
        } else {
92 11
            $this->_criteria[] = $c;
93
        }
94 11
        return $this;
95
    }
96
97
    /**
98
     * Gets the number of entries in the clause
99
     *
100
     * @return int
101
     */
102
    public function count()
103
    {
104 1
    	return count($this->_criteria);
105
    }
106
107
    /**
108
     * Evaluates the Junction for a given object
109
     *
110
     * @param mixed $value
111
     * @return boolean
112
     */
113
    public function evaluate( $value )
114
    {
115 11
        $ok = true;
116 11
        if ( $this->_operator == "OR" ) {
117 3
            $ok = false;
118 3
            foreach( $this->_criteria as $crit ) {
119 3
                if ( $crit->evaluate($value) ) {
120 3
                    $ok = true;
121 3
                    break;
122 0
                }
123 3
            }
124 3
        } else {
125 9
            foreach( $this->_criteria as $crit ) {
126 9
                if ( !$crit->evaluate($value) ) {
127 6
                    $ok = false;
128 6
                    break;
129 0
                }
130 9
            }
131
        }
132 11
        return $ok;
133
    }
134
135
    /**
136
     * Gets the Criteria in this Junction
137
     *
138
     * @return Xyster_Collection
139
     */
140
    public function getCriteria()
141
    {
142 31
        require_once 'Xyster/Collection.php';
143 31
        return Xyster_Collection::using($this->_criteria, true);
144
    }
145
146
    /**
147
     * Gets the iterator for this clause
148
     *
149
     * @return Iterator
150
     */
151
    public function getIterator()
152
    {
153
    	// no need to return an emptyIterator -- at least 2 items always
154 1
    	require_once 'Xyster/Collection/Iterator.php';
155 1
    	return new Xyster_Collection_Iterator($this->_criteria);
156
    }
157
158
    /**
159
     * Gets the Junction operator
160
     *
161
     * @return string
162
     */
163
    public function getOperator()
164
    {
165 28
        return $this->_operator;
166
    }
167
168
    /**
169
     * Converts the clause into an array of its symbols
170
     *
171
     * @return array
172
     */
173
    public function toArray()
174
    {
175 1
    	return array_values($this->_criteria);
176
    }
177
178
    /**
179
     * Returns the string syntax for this Junction
180
     *
181
     * @return string
182
     */
183
    public function __toString()
184
    {
185 1
        $criteria = array();
186 1
        foreach( $this->_criteria as $v ) {
187 1
            $criteria[] = $v->__toString();
188 1
        }
189 1
        return "( " . implode(" " . $this->_operator . " ", $criteria) . " )";
190
    }
191
192
    /**
193
     * Create a new 'OR' junction, i.e. ( x OR y )
194
     *
195
     * @param Xyster_Data_Criterion $left
196
     * @param Xyster_Data_Criterion $right
197
     * @return Xyster_Data_Junction
198
     */
199
    static public function any( Xyster_Data_Criterion $left, Xyster_Data_Criterion $right )
200
    {
201 4
        return new Xyster_Data_Junction($left, $right, 'OR');
202
    }
203
204
    /**
205
     * Create a new 'AND' junction, i.e. ( x AND y )
206
     *
207
     * @param Xyster_Data_Criterion $left
208
     * @param Xyster_Data_Criterion $right
209
     * @return Xyster_Data_Junction
210
     */
211
    static public function all( Xyster_Data_Criterion $left, Xyster_Data_Criterion $right )
212
    {
213 37
        return new Xyster_Data_Junction($left, $right, 'AND');
214
    }
215
}


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