http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

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


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