http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 2 LOC: 72 Statements: 13

Source file Statements Methods Total coverage
Comparator.php 92.3% 100.0% 93.3%
   
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_Collection_Comparator_Interface
18
 */
19 1
require_once 'Xyster/Collection/Comparator/Interface.php';
20
/**
21
 * Comparator for objects or associative arrays
22
 *
23
 * @category  Xyster
24
 * @package   Xyster_Data
25
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
26
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
27
 */
28
class Xyster_Data_Comparator implements Xyster_Collection_Comparator_Interface
29
{
30
	/**
31
	 * The clause of {@link Xyster_Data_Sort} objects
32
	 *
33
	 * @var Xyster_Data_Sort_Clause
34
	 */
35
	protected $_sorts;
36
37
	/**
38
	 * Create a new Data Comparator object
39
	 *
40
	 * If you pass a clause containing no objects, this comparator will always
41
	 * return zero when comparing objects.
42
	 *
43
	 * @param Xyster_Data_Sort_Clause $sorts A clause of sort objects
44
	 */
45
	public function __construct( Xyster_Data_Sort_Clause $sorts )
46
	{
47 7
        $this->_sorts = $sorts;
48
	}
49
50
	/**
51
	 * Compares two arguments for sorting
52
	 *
53
	 * Returns a negative integer, zero, or a positive integer as the first
54
	 * argument is less than, equal to, or greater than the second.
55
	 *
56
	 * @param mixed $a
57
	 * @param mixed $b
58
	 */
59
	public function compare( $a, $b )
60
	{
61 6
		foreach( $this->_sorts as $sort ) {
62 6
			$av = $sort->getField()->evaluate($a);
63 6
			$bv = $sort->getField()->evaluate($b);
64 6
			if ( $av < $bv ) {
65 5
				return ( $sort->getDirection() == 'ASC' ) ? -1 : 1;
66 5
			} else if ( $av > $bv ) {
67 5
				return ( $sort->getDirection() == 'ASC' ) ? 1 : -1;
68 0
			}
69 2
		}
70 2
		return 0;
71
	}
72
}


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