http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 29 LOC: 416 Statements: 67

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


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