http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 8 LOC: 177 Statements: 49

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


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