*/
protected function pageableSortedRows( ResultWrapper $res, $column, $limit ) {
$rows = iterator_to_array( $res, false );
- $count = count( $rows );
- if ( !$count ) {
- return [ [], null ]; // nothing to do
- } elseif ( $count < $limit ) {
- return [ $rows, $rows[$count - 1]->$column ]; // no more rows left
+
+ // Nothing to do
+ if ( !$rows ) {
+ return [ [], null ];
+ }
+
+ $lastValue = end( $rows )->$column;
+ if ( count( $rows ) < $limit ) {
+ return [ $rows, $lastValue ];
}
- $lastValue = $rows[$count - 1]->$column; // should be the highest
- for ( $i = $count - 1; $i >= 0; --$i ) {
- if ( $rows[$i]->$column === $lastValue ) {
- unset( $rows[$i] );
- } else {
+
+ for ( $i = count( $rows ) - 1; $i >= 0; --$i ) {
+ if ( $rows[$i]->$column !== $lastValue ) {
break;
}
+
+ unset( $rows[$i] );
+ }
+
+ // No more rows left
+ if ( !$rows ) {
+ return [ [], null ];
}
- $lastValueLeft = count( $rows ) ? $rows[count( $rows ) - 1]->$column : null;
- return [ $rows, $lastValueLeft ];
+ return [ $rows, end( $rows )->$column ];
}
}