Commit d5b643d2 authored by Ghislain Loas's avatar Ghislain Loas

refactor order by parsing implementation

parent 9ed4853c
......@@ -24,7 +24,6 @@ THE SOFTWARE.
*/
class Storm_Model_PersistenceStrategy_Volatile extends Storm_Model_PersistenceStrategy_Abstract {
protected
$_instances = [],
......@@ -131,48 +130,26 @@ class Storm_Model_PersistenceStrategy_Volatile extends Storm_Model_PersistenceS
protected function compareFunction($order) {
$fields = explode(' ', strtolower($order));
$field = $fields[0];
$reverse = sizeof($fields) > 1 && $fields[1] == 'desc';
return function($a, $b) use ($field, $reverse) {
$first = (array_key_exists($field, $a)) ? $a[$field] : '';
$last = (array_key_exists($field, $b)) ? $b[$field] : '';
if ($reverse) {
$temp = $first;
$first = $last;
$last = $temp;
}
if (is_int($first))
return $this->int_compare((int)$first, (int)$last);
return $this->string_compare($first, $last);
};
}
$first_clause = Storm_Model_PersistenceStrategy_Volatile_OrderClause::parseOrderClause($order);
public function int_compare($a , $b) {
return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
}
public function string_compare($a, $b){
return strcmp($a, $b);
return function($a, $b) use ($first_clause) {
return $first_clause->compare($a, $b);
};
}
public function ordered($result, $select) {
if (!$select)
if (! ($select && $result) )
return $result;
if (!is_array($select))
$select = [$select];
foreach($select as $clause)
usort($result, $this->compareFunction($clause));
if (is_array($select))
$select = implode(',', $select);
usort($result, $this->compareFunction($select));
return $result;
}
private function limited($result, $limit) {
if ('' == $limit)
return $result;
......
<?php
/*
STORM is under the MIT License (MIT)
Copyright (c) 2010-2011 Agence Française Informatique http://www.afi-sa.fr
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
class Storm_Model_PersistenceStrategy_Volatile_OrderClause {
protected
$_fieldname,
$_descendant = false,
$_next_clause = null;
public static function parseOrderClause($order) {
$clauses = array_filter(explode(',', strtolower($order)));
$first_clause = new static(array_pop($clauses));
foreach($clauses as $clause) {
$first_clause = new static($clause, $first_clause);
}
return $first_clause;
}
public function __construct($order, $next_clause = null) {
$description = explode(' ', trim($order));
$this->_fieldname = trim(array_shift($description));
$this->_descendant = (array_shift($description) == 'desc');
$this->_next_clause = $next_clause;
}
public function compare($a, $b) {
$first = (array_key_exists($this->_fieldname, $a)) ? $a[$this->_fieldname] : '';
$last = (array_key_exists($this->_fieldname, $b)) ? $b[$this->_fieldname] : '';
if ($this->_descendant) {
$temp = $first;
$first = $last;
$last = $temp;
}
$result = (is_int($first))
? $this->int_compare((int)$first, (int)$last)
: $this->string_compare($first, $last);
return (($result === 0) && $this->_next_clause)
? $this->_next_clause->compare($a, $b)
: $result;
}
public function int_compare($a , $b) {
return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
}
public function string_compare($a, $b){
return strcmp($a, $b);
}
}
......@@ -513,6 +513,38 @@ class Storm_Test_LoaderVolatileTest extends Storm_Test_ModelTestCase {
}
/** @test */
public function findAllOrderByFooThenIdShouldReturnAlbertZoeHubert() {
$users = Storm_Test_VolatileUser::findAll(['order' => 'foo, id']);
$this->assertEquals(['albert', 'hubert', 'zoe'],
(new Storm_Model_Collection($users))->collect('login')->getArrayCopy());
}
/** @test */
public function findAllOrderByFooThenIdArrayFormatShouldReturnZoeHubertAlbert() {
$users = Storm_Test_VolatileUser::findAll(['order' => ['foo desc', 'id desc']]);
$this->assertEquals(['zoe', 'hubert', 'albert'],
(new Storm_Model_Collection($users))->collect('login')->getArrayCopy());
}
/** @test */
public function findAllOrderByFooThenIdDescArrayFormatShouldReturnAlbertZoeHubert() {
$users = Storm_Test_VolatileUser::findAll(['order' => ['foo', 'id desc']]);
$this->assertEquals(['albert', 'zoe', 'hubert'],
(new Storm_Model_Collection($users))->collect('login')->getArrayCopy());
}
/** @test */
public function findAllOrderByFooDescThenIdDescShouldReturnHubertZoeAlbert() {
$users = Storm_Test_VolatileUser::findAll(['order' => 'foo desc, id desc']);
$this->assertEquals(['zoe', 'hubert', 'albert'],
(new Storm_Model_Collection($users))->collect('login')->getArrayCopy());
}
/** @test */
public function deleteHubertFindAllShouldReturnAlbertAndZoe(){
$this->hubert->delete();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment