| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* LICENSE |
| 6 |
|
* |
| 7 |
|
* This source file is subject to the new BSD license that is bundled |
| 8 |
|
* with this package in the file LICENSE.txt. |
| 9 |
|
* It is also available through the world-wide-web at this URL: |
| 10 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 11 |
|
* If you did not receive a copy of the license and are unable to |
| 12 |
|
* obtain it through the world-wide-web, please send an email |
| 13 |
|
* to xyster@devweblog.org so we can send you a copy immediately. |
| 14 |
|
* |
| 15 |
|
* @category Xyster |
| 16 |
|
* @package Xyster_Data |
| 17 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 18 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 19 |
|
*/ |
| 20 |
|
/** |
| 21 |
|
* Xyster_Collection_Comparator_Interface |
| 22 |
|
*/ |
| 23 |
1 |
require_once 'Xyster/Collection/Comparator/Interface.php'; |
| 24 |
|
/** |
| 25 |
|
* Comparator for objects or associative arrays |
| 26 |
|
* |
| 27 |
|
* @category Xyster |
| 28 |
|
* @package Xyster_Data |
| 29 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 30 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 31 |
|
*/ |
| 32 |
|
class Xyster_Data_Comparator implements Xyster_Collection_Comparator_Interface |
| 33 |
|
{ |
| 34 |
|
/** |
| 35 |
|
* The array of {@link Xyster_Data_Sort} objects |
| 36 |
|
* |
| 37 |
|
* @var array |
| 38 |
|
*/ |
| 39 |
|
protected $_sorts = array(); |
| 40 |
|
|
| 41 |
|
/** |
| 42 |
|
* Create a new Data Comparator object |
| 43 |
|
* |
| 44 |
|
* @param array $sort An array of {@link Xyster_Data_Sort} objects |
| 45 |
|
*/ |
| 46 |
|
public function __construct( array $sorts ) |
| 47 |
|
{ |
| 48 |
9 |
foreach( $sorts as $sort ) { |
| 49 |
8 |
if ( ! $sort instanceof Xyster_Data_Sort ) { |
| 50 |
2 |
require_once 'Xyster/Data/Exception.php'; |
| 51 |
2 |
throw new Xyster_Data_Exception('Only Xyster_Data_Sort objects can be used'); |
| 52 |
0 |
} |
| 53 |
6 |
$this->_sorts[] = $sort; |
| 54 |
6 |
} |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
/** |
| 58 |
|
* Compares two arguments for sorting |
| 59 |
|
* |
| 60 |
|
* Returns a negative integer, zero, or a positive integer as the first |
| 61 |
|
* argument is less than, equal to, or greater than the second. |
| 62 |
|
* |
| 63 |
|
* @param mixed $a |
| 64 |
|
* @param mixed $b |
| 65 |
|
*/ |
| 66 |
|
public function compare( $a, $b ) |
| 67 |
|
{ |
| 68 |
6 |
foreach( $this->_sorts as $sort ) { |
| 69 |
6 |
$av = $sort->getField()->evaluate($a); |
| 70 |
6 |
$bv = $sort->getField()->evaluate($b); |
| 71 |
6 |
if ( $av < $bv ) { |
| 72 |
5 |
return ( $sort->getDirection() == 'ASC' ) ? -1 : 1; |
| 73 |
6 |
} else if ( $av > $bv ) { |
| 74 |
6 |
return ( $sort->getDirection() == 'ASC' ) ? 1 : -1; |
| 75 |
0 |
} |
| 76 |
2 |
} |
| 77 |
2 |
return 0; |
| 78 |
|
} |
| 79 |
|
} |