http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 29 LOC: 388 Statements: 66

Source file Statements Methods Total coverage
Field.php 95.5% 100.0% 96.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
/**
17
 * @see Xyster_Data_Aggregate
18
 */
19 1
require_once 'Xyster/Data/Aggregate.php';
20
21
/**
22
 * A simple concept for data fields and columns
23
 *
24
 * @category  Xyster
25
 * @package   Xyster_Data
26
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
27
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
28
 */
29
class Xyster_Data_Field
30
{
31
    /**
32
     * The name of the field
33
     *
34
     * @var string
35
     */
36
    protected $_name;
37
    /**
38
     * An alias for the field, equal to the field name by default
39
     *
40
     * @var string
41
     */
42
    protected $_alias;
43
44
    /**
45
     * Creates a new Field
46
     *
47
     * @param string $name  The field name (be it a property, column, whatever)
48
     * @param string $alias  The alias for this field
49
     */
50
    protected function __construct( $name, $alias=null )
51
    {
52 242
        $this->_name = trim($name);
53 242
        if ( !strlen($this->_name) ) {
54 1
            require_once 'Xyster/Data/Field/Exception.php';
55 1
            throw new Xyster_Data_Field_Exception('A field name cannot be empty');
56
        }
57 242
        $this->_alias = ( !strlen(trim($alias)) ) ? $name : trim($alias);
58
    }
59
60
    /**
61
     * Gets the name of this field
62
     *
63
     * @return string The field name
64
     */
65
    public function getName()
66
    {
67 69
        return $this->_name;
68
    }
69
    /**
70
     * Gets the alias assigned to this field
71
     *
72
     * @return string The field alias
73
     */
74
    public function getAlias()
75
    {
76 10
        return $this->_alias;
77
    }
78
    /**
79
     * Sets the alias assigned to this column
80
     *
81
     * @param string $alias  The alias to assign
82
     * @throws Xyster_Data_Field_Exception if the alias is invalid
83
     */
84
    public function setAlias( $alias )
85
    {
86 4
        if ( !strlen(trim($alias)) ) {
87 1
            require_once 'Xyster/Data/Field/Exception.php';
88 1
            throw new Xyster_Data_Field_Exception('An alias cannot be empty');
89 0
        }
90 4
        $this->_alias = trim($alias);
91
    }
92
    /**
93
     * Evaluates the reference for the given object
94
     *
95
     * @param mixed $object
96
     * @return mixed
97
     */
98
    public function evaluate( $object )
99
    {
100 79
        $value = null;
101 79
        if ( is_array($object) || $object instanceof ArrayAccess ) {
102 32
            if ( !isset($object[$this->_name]) && !array_key_exists($this->_name, $object) ) {
103 1
                require_once 'Xyster/Data/Field/Exception.php';
104 1
                throw new Xyster_Data_Field_Exception("Field name '{$this->_name}' is invalid");
105 0
            }
106 31
            $value = $object[$this->_name];
107 78
        } else if ( is_object($object) && preg_match('/^[a-z_]\w*$/i', $this->_name) ) {
108
            // name of a real property or one caught by __get()
109 48
            $value = $object->{$this->_name};
110 49
        } else if ( is_object($object) ) {
111
            // this is eval'ed becuse $this->_name might be a method call
112
            // maybe sometime in the future we can do this better...
113 6
            eval("\$value = \$object->{$this->_name};");
114 6
        } else {
115 1
            require_once 'Xyster/Data/Field/Exception.php';
116 1
            throw new Xyster_Data_Field_Exception("Only objects or arrays can be evaluated");
117
        }
118 77
        return $value;
119
    }
120
    /**
121
     * String representation of this object
122
     *
123
     * @magic
124
     * @return string
125
     */
126
    public function __toString()
127
    {
128 29
        return $this->getName();
129
    }
130
    /**
131
     * Factories an ascending {@link Xyster_Data_Sort} for this field name
132
     *
133
     * @return Xyster_Data_Sort
134
     */
135
    public function asc()
136
    {
137 3
        require_once 'Xyster/Data/Sort.php';
138 3
        return Xyster_Data_Sort::asc($this);
139
    }
140
    /**
141
     * Factories a descending {@link Xyster_Data_Sort} for this field name
142
     *
143
     * @return Xyster_Data_Sort
144
     */
145
    public function desc()
146
    {
147 3
        require_once 'Xyster/Data/Sort.php';
148 3
        return Xyster_Data_Sort::desc($this);
149
    }
150
    /**
151
     * Factories an Equal To Xyster_Data_Expression ( column = 'value' )
152
     *
153
     * @param mixed $value
154
     * @return Xyster_Data_Expression
155
     */
156
    public function eq( $value )
157
    {
158 1
        return $this->_expression(__FUNCTION__, array($this, $value));
159
    }
160
    /**
161
     * Factories a Not Equal To Xyster_Data_Expression ( column <> 'value' )
162
     *
163
     * @param mixed $value
164
     * @return Xyster_Data_Expression
165
     */
166
    public function neq( $value )
167
    {
168 2
        return $this->_expression(__FUNCTION__, array($this, $value));
169
    }
170
    /**
171
     * Factories a Less Than Xyster_Data_Expression ( column < 3 )
172
     *
173
     * @param mixed $value
174
     * @return Xyster_Data_Expression
175
     */
176
    public function lt( $value )
177
    {
178 1
        return $this->_expression(__FUNCTION__, array($this, $value));
179
    }
180
    /**
181
     * Factories a Less Than or Equal To Xyster_Data_Expression ( column <= 3 )
182
     *
183
     * @param mixed $value
184
     * @return Xyster_Data_Expression
185
     */
186
    public function lte( $value )
187
    {
188 1
        return $this->_expression(__FUNCTION__, array($this, $value));
189
    }
190
    /**
191
     * Factories a Greater Than Xyster_Data_Expression ( column > 2 )
192
     *
193
     * @param mixed $value
194
     * @return Xyster_Data_Expression
195
     */
196
    public function gt( $value )
197
    {
198 1
        return $this->_expression(__FUNCTION__, array($this, $value));
199
    }
200
    /**
201
     * Factories a Greater Than or Equal To Xyster_Data_Expression ( column >= 2 )
202
     *
203
     * @param mixed $value
204
     * @return Xyster_Data_Expression
205
     */
206
    public function gte( $value )
207
    {
208 1
        return $this->_expression(__FUNCTION__, array($this, $value));
209
    }
210
    /**
211
     * Factories a LIKE Xyster_Data_Expression ( column LIKE '%value' )
212
     *
213
     * @param mixed $value
214
     * @return Xyster_Data_Expression
215
     */
216
    public function like( $value )
217
    {
218 1
        return $this->_expression(__FUNCTION__, array($this, $value));
219
    }
220
    /**
221
     * Factories a NOT LIKE Xyster_Data_Expression ( column NOT LIKE '%value' )
222
     *
223
     * @param mixed $value
224
     * @return Xyster_Data_Expression
225
     */
226
    public function notLike( $value )
227
    {
228 2
        return $this->_expression(__FUNCTION__, array($this, $value));
229
    }
230
    /**
231
     * Factories a BETWEEN Xyster_Data_Expression ( column BETWEEN 'value' AND 'value' )
232
     *
233
     * @param mixed $start
234
     * @param mixed $end
235
     * @return Xyster_Data_Expression
236
     */
237
    public function between( $start, $end )
238
    {
239 2
        return $this->_expression(__FUNCTION__, array($this, $start, $end));
240
    }
241
    /**
242
     * Factories a NOT BETWEEN Xyster_Data_Expression ( column NOT BETWEEN 'value' AND 'value' )
243
     *
244
     * @param mixed $start
245
     * @param mixed $end
246
     * @return Xyster_Data_Expression
247
     */
248
    public function notBetween( $start, $end )
249
    {
250 1
        return $this->_expression(__FUNCTION__, array($this, $start, $end));
251
    }
252
    /**
253
     * Factories an In expression ( column IN ( 1,1,2,3,5,8,13,21,'fibonacci','sequence' ) )
254
     *
255
     * @param array $choices
256
     * @return Xyster_Data_Expression
257
     */
258
    public function in( array $choices )
259
    {
260 2
        return $this->_expression(__FUNCTION__, array($this, $choices));
261
    }
262
    /**
263
     * Factories a Not in expression ( column NOT IN ( 1,1,2,3,5,8,13,21,'fibonacci','sequence' ) )
264
     *
265
     * @param array $choices
266
     * @return Xyster_Data_Expression
267
     */
268
    public function notIn( array $choices )
269
    {
270 1
        return $this->_expression(__FUNCTION__, array($this, $choices));
271
    }
272
273
    /**
274
     * Factories an expression
275
     *
276
     * @param string $function
277
     * @param array $params
278
     * @return Xyster_Data_Expression
279
     */
280
    protected function _expression( $function, array $params = array() )
281
    {
282 15
        require_once 'Xyster/Data/Expression.php';
283 15
        return call_user_func_array(
284 15
                array('Xyster_Data_Expression',$function),
285
                $params
286 15
            );
287
    }
288
289
    /**
290
     * Creates a Xyster_Data_Field by name with an alias
291
     *
292
     * @param string $name The name of the field
293
     * @param string $alias The alias to assign
294
     * @return Xyster_Data_Field
295
     */
296
    static public function named( $name, $alias = null )
297
    {
298 235
        require_once 'Xyster/Data/Field/Aggregate.php';
299 235
        if ( $match = Xyster_Data_Field_Aggregate::match($name) ) {
300 11
            require_once 'Xyster/Enum.php';
301 11
            $function = Xyster_Enum::valueOf('Xyster_Data_Aggregate', $match['function']);
302 11
            return self::aggregate($function, $match['field'], $alias);
303 0
        } else {
304 231
            return new Xyster_Data_Field($name, $alias);
305
        }
306
    }
307
308
    /**
309
     * Creates a Xyster_Data_Field that defines a group in a result
310
     *
311
     * @param string $name
312
     * @param string $alias
313
     * @return Xyster_Data_Field_Group
314
     */
315
    static public function group( $name, $alias = null )
316
    {
317 11
        require_once 'Xyster/Data/Field/Group.php';
318 11
        return new Xyster_Data_Field_Group($name, $alias);
319
    }
320
    /**
321
     * Creates an {@link Xyster_Data_Aggregate} field
322
     *
323
     * @param Xyster_Data_Aggregate $function The aggregate used
324
     * @param string $name The name of the field to aggregate
325
     * @param string $alias Optional; the alias of the aggregate field
326
     * @return Xyster_Data_Field_Aggregate
327
     */
328
    static public function aggregate( Xyster_Data_Aggregate $function, $name, $alias = null )
329
    {
330 35
        require_once 'Xyster/Data/Field/Aggregate.php';
331 35
        return new Xyster_Data_Field_Aggregate($function, $name, $alias);
332
    }
333
    /**
334
     * Creates an {@link Xyster_Data_Aggregate} field to count items in a tuple
335
     *
336
     * @param string $name The name of the field
337
     * @param string $alias The alias to assign
338
     * @return Xyster_Data_Field_Aggregate
339
     */
340
    static public function count( $name, $alias = null )
341
    {
342 10
        return self::aggregate(Xyster_Data_Aggregate::Count(), $name, $alias);
343
    }
344
    /**
345
     * Creates an {@link Xyster_Data_Aggregate} field to sum the values in a field
346
     *
347
     * @param string $name The name of the field
348
     * @param string $alias The alias to assign
349
     * @return Xyster_Data_Field_Aggregate
350
     */
351
    static public function sum( $name, $alias = null )
352
    {
353 3
        return self::aggregate(Xyster_Data_Aggregate::Sum(), $name, $alias);
354
    }
355
    /**
356
     * Creates an {@link Xyster_Data_Aggregate} field to average the values in a field
357
     *
358
     * @param string $name The name of the field
359
     * @param string $alias The alias to assign
360
     * @return Xyster_Data_Field_Aggregate
361
     */
362
    static public function avg( $name, $alias = null )
363
    {
364 3
        return self::aggregate(Xyster_Data_Aggregate::Average(), $name, $alias);
365
    }
366
    /**
367
     * Creates an {@link Xyster_Data_Aggregate} field to find the maximum value in a field
368
     *
369
     * @param string $name The name of the field
370
     * @param string $alias The alias to assign
371
     * @return Xyster_Data_Field_Aggregate
372
     */
373
    static public function max( $name, $alias = null )
374
    {
375 6
        return self::aggregate(Xyster_Data_Aggregate::Maximum(), $name, $alias);
376
    }
377
    /**
378
     * Creates an {@link Xyster_Data_Aggregate} field to find the minimum value in a field
379
     *
380
     * @param string $name The name of the field
381
     * @param string $alias The alias to assign
382
     * @return Xyster_Data_Field_Aggregate
383
     */
384
    static public function min( $name, $alias = null )
385
    {
386 3
        return self::aggregate(Xyster_Data_Aggregate::Minimum(), $name, $alias);
387
    }
388
}


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