Browse Source

Fixes #1236: removed Sort::ASC and DESC

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
9efe446545
  1. 4
      framework/yii/data/ActiveDataProvider.php
  2. 56
      framework/yii/data/Sort.php
  3. 42
      tests/unit/framework/data/SortTest.php
  4. 4
      tests/unit/framework/helpers/ArrayHelperTest.php

4
framework/yii/data/ActiveDataProvider.php

@ -172,8 +172,8 @@ class ActiveDataProvider extends BaseDataProvider
$model = new $this->query->modelClass;
foreach ($model->attributes() as $attribute) {
$sort->attributes[$attribute] = [
'asc' => [$attribute => Sort::ASC],
'desc' => [$attribute => Sort::DESC],
'asc' => [$attribute => SORT_ASC],
'desc' => [$attribute => SORT_DESC],
'label' => $model->getAttributeLabel($attribute),
];
}

56
framework/yii/data/Sort.php

@ -29,9 +29,9 @@ use yii\helpers\Inflector;
* 'attributes' => [
* 'age',
* 'name' => [
* 'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
* 'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
* 'default' => Sort::DESC,
* 'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
* 'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
* 'default' => SORT_DESC,
* 'label' => 'Name',
* ],
* ],
@ -66,7 +66,7 @@ use yii\helpers\Inflector;
* that can lead to pages with the data sorted by the corresponding attributes.
*
* @property array $attributeOrders Sort directions indexed by attribute names. Sort direction can be either
* [[Sort::ASC]] for ascending order or [[Sort::DESC]] for descending order. This property is read-only.
* `SORT_ASC` for ascending order or `SORT_DESC` for descending order. This property is read-only.
* @property array $orders The columns (keys) and their corresponding sort directions (values). This can be
* passed to [[\yii\db\Query::orderBy()]] to construct a DB query. This property is read-only.
*
@ -76,16 +76,6 @@ use yii\helpers\Inflector;
class Sort extends Object
{
/**
* Sort ascending
*/
const ASC = false;
/**
* Sort descending
*/
const DESC = true;
/**
* @var boolean whether the sorting can be applied to multiple attributes simultaneously.
* Defaults to false, which means each time the data can only be sorted by one attribute.
*/
@ -99,9 +89,9 @@ class Sort extends Object
* [
* 'age',
* 'name' => [
* 'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
* 'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
* 'default' => Sort::DESC,
* 'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
* 'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
* 'default' => SORT_DESC,
* 'label' => 'Name',
* ],
* ]
@ -112,9 +102,9 @@ class Sort extends Object
*
* ~~~
* 'age' => [
* 'asc' => ['age' => Sort::ASC],
* 'desc' => ['age' => Sort::DESC],
* 'default' => Sort::ASC,
* 'asc' => ['age' => SORT_ASC],
* 'desc' => ['age' => SORT_DESC],
* 'default' => SORT_ASC,
* 'label' => Inflector::camel2words('age'),
* ]
* ~~~
@ -153,8 +143,8 @@ class Sort extends Object
*
* ~~~
* [
* 'name' => Sort::ASC,
* 'create_time' => Sort::DESC,
* 'name' => SORT_ASC,
* 'create_time' => SORT_DESC,
* ]
* ~~~
*
@ -199,13 +189,13 @@ class Sort extends Object
foreach ($this->attributes as $name => $attribute) {
if (!is_array($attribute)) {
$attributes[$attribute] = [
'asc' => [$attribute => self::ASC],
'desc' => [$attribute => self::DESC],
'asc' => [$attribute => SORT_ASC],
'desc' => [$attribute => SORT_DESC],
];
} elseif (!isset($attribute['asc'], $attribute['desc'])) {
$attributes[$name] = array_merge([
'asc' => [$name => self::ASC],
'desc' => [$name => self::DESC],
'asc' => [$name => SORT_ASC],
'desc' => [$name => SORT_DESC],
], $attribute);
} else {
$attributes[$name] = $attribute;
@ -226,7 +216,7 @@ class Sort extends Object
$orders = [];
foreach ($attributeOrders as $attribute => $direction) {
$definition = $this->attributes[$attribute];
$columns = $definition[$direction === self::ASC ? 'asc' : 'desc'];
$columns = $definition[$direction === SORT_ASC ? 'asc' : 'desc'];
foreach ($columns as $name => $dir) {
$orders[$name] = $dir;
}
@ -243,8 +233,8 @@ class Sort extends Object
* Returns the currently requested sort information.
* @param boolean $recalculate whether to recalculate the sort directions
* @return array sort directions indexed by attribute names.
* Sort direction can be either [[Sort::ASC]] for ascending order or
* [[Sort::DESC]] for descending order.
* Sort direction can be either `SORT_ASC` for ascending order or
* `SORT_DESC` for descending order.
*/
public function getAttributeOrders($recalculate = false)
{
@ -262,7 +252,7 @@ class Sort extends Object
}
if (isset($this->attributes[$attribute])) {
$this->_attributeOrders[$attribute] = $descending;
$this->_attributeOrders[$attribute] = $descending ? SORT_DESC : SORT_ASC;
if (!$this->enableMultiSort) {
return $this->_attributeOrders;
}
@ -279,8 +269,8 @@ class Sort extends Object
/**
* Returns the sort direction of the specified attribute in the current request.
* @param string $attribute the attribute name
* @return boolean|null Sort direction of the attribute. Can be either [[Sort::ASC]]
* for ascending order or [[Sort::DESC]] for descending order. Null is returned
* @return boolean|null Sort direction of the attribute. Can be either `SORT_ASC`
* for ascending order or `SORT_DESC` for descending order. Null is returned
* if the attribute is invalid or does not need to be sorted.
*/
public function getAttributeOrder($attribute)
@ -305,7 +295,7 @@ class Sort extends Object
public function link($attribute, $options = [])
{
if (($direction = $this->getAttributeOrder($attribute)) !== null) {
$class = $direction ? 'desc' : 'asc';
$class = $direction === SORT_DESC ? 'desc' : 'asc';
if (isset($options['class'])) {
$options['class'] .= ' ' . $class;
} else {

42
tests/unit/framework/data/SortTest.php

@ -25,8 +25,8 @@ class SortTest extends TestCase
'attributes' => [
'age',
'name' => [
'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
],
],
'params' => [
@ -37,14 +37,14 @@ class SortTest extends TestCase
$orders = $sort->getOrders();
$this->assertEquals(3, count($orders));
$this->assertEquals(Sort::ASC, $orders['age']);
$this->assertEquals(Sort::DESC, $orders['first_name']);
$this->assertEquals(Sort::DESC, $orders['last_name']);
$this->assertEquals(SORT_ASC, $orders['age']);
$this->assertEquals(SORT_DESC, $orders['first_name']);
$this->assertEquals(SORT_DESC, $orders['last_name']);
$sort->enableMultiSort = false;
$orders = $sort->getOrders(true);
$this->assertEquals(1, count($orders));
$this->assertEquals(Sort::ASC, $orders['age']);
$this->assertEquals(SORT_ASC, $orders['age']);
}
public function testGetAttributeOrders()
@ -53,8 +53,8 @@ class SortTest extends TestCase
'attributes' => [
'age',
'name' => [
'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
],
],
'params' => [
@ -65,13 +65,13 @@ class SortTest extends TestCase
$orders = $sort->getAttributeOrders();
$this->assertEquals(2, count($orders));
$this->assertEquals(Sort::ASC, $orders['age']);
$this->assertEquals(Sort::DESC, $orders['name']);
$this->assertEquals(SORT_ASC, $orders['age']);
$this->assertEquals(SORT_DESC, $orders['name']);
$sort->enableMultiSort = false;
$orders = $sort->getAttributeOrders(true);
$this->assertEquals(1, count($orders));
$this->assertEquals(Sort::ASC, $orders['age']);
$this->assertEquals(SORT_ASC, $orders['age']);
}
public function testGetAttributeOrder()
@ -80,8 +80,8 @@ class SortTest extends TestCase
'attributes' => [
'age',
'name' => [
'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
],
],
'params' => [
@ -90,8 +90,8 @@ class SortTest extends TestCase
'enableMultiSort' => true,
]);
$this->assertEquals(Sort::ASC, $sort->getAttributeOrder('age'));
$this->assertEquals(Sort::DESC, $sort->getAttributeOrder('name'));
$this->assertEquals(SORT_ASC, $sort->getAttributeOrder('age'));
$this->assertEquals(SORT_DESC, $sort->getAttributeOrder('name'));
$this->assertNull($sort->getAttributeOrder('xyz'));
}
@ -101,8 +101,8 @@ class SortTest extends TestCase
'attributes' => [
'age',
'name' => [
'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
],
],
'params' => [
@ -127,8 +127,8 @@ class SortTest extends TestCase
'attributes' => [
'age',
'name' => [
'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
],
],
'params' => [
@ -155,8 +155,8 @@ class SortTest extends TestCase
'attributes' => [
'age',
'name' => [
'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
],
],
'params' => [

4
tests/unit/framework/helpers/ArrayHelperTest.php

@ -147,7 +147,7 @@ class ArrayHelperTest extends TestCase
// single key
$sort = new Sort([
'attributes' => ['name', 'age'],
'defaultOrder' => ['name' => Sort::ASC],
'defaultOrder' => ['name' => SORT_ASC],
]);
$orders = $sort->getOrders();
@ -164,7 +164,7 @@ class ArrayHelperTest extends TestCase
// multiple keys
$sort = new Sort([
'attributes' => ['name', 'age'],
'defaultOrder' => ['name' => Sort::ASC, 'age' => Sort::DESC],
'defaultOrder' => ['name' => SORT_ASC, 'age' => SORT_DESC],
]);
$orders = $sort->getOrders();

Loading…
Cancel
Save