http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 2 LOC: 74 Statements: 18

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


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