Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| SortRevisionsByRevisionId | |
0.00% |
0 / 13 |
|
0.00% |
0 / 2 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| __invoke | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Data\Utils; |
| 4 | |
| 5 | use Flow\Exception\InvalidParameterException; |
| 6 | use Flow\Model\AbstractRevision; |
| 7 | |
| 8 | /** |
| 9 | * Sorts AbstractRevision objects by revision ID |
| 10 | */ |
| 11 | class SortRevisionsByRevisionId { |
| 12 | /** |
| 13 | * Order, either ASC or DESC. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $order; |
| 18 | |
| 19 | /** |
| 20 | * @param string $order ASC or DESC |
| 21 | * @throws InvalidParameterException |
| 22 | */ |
| 23 | public function __construct( $order ) { |
| 24 | if ( $order !== 'ASC' && $order !== 'DESC' ) { |
| 25 | throw new InvalidParameterException( "Must specify ASC or DESC" ); |
| 26 | } |
| 27 | |
| 28 | $this->order = $order; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Compares two revisions |
| 33 | * |
| 34 | * @param AbstractRevision $a |
| 35 | * @param AbstractRevision $b |
| 36 | * @return int |
| 37 | */ |
| 38 | public function __invoke( AbstractRevision $a, AbstractRevision $b ) { |
| 39 | $aId = $a->getRevisionId()->getAlphadecimal(); |
| 40 | $bId = $b->getRevisionId()->getAlphadecimal(); |
| 41 | |
| 42 | if ( $aId < $bId ) { |
| 43 | $result = -1; |
| 44 | } elseif ( $aId > $bId ) { |
| 45 | $result = 1; |
| 46 | } else { |
| 47 | $result = 0; |
| 48 | } |
| 49 | |
| 50 | if ( $this->order === 'ASC' ) { |
| 51 | return $result; |
| 52 | } else { |
| 53 | return -$result; |
| 54 | } |
| 55 | } |
| 56 | } |