From 90b9ed87bd0265b2a645c6828635ab922fa7fd7c Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Wed, 31 Aug 2016 14:03:34 +0300 Subject: [PATCH] Added `yii\data\Sort::setAttributeOrders()` --- framework/CHANGELOG.md | 1 + framework/data/Sort.php | 28 ++++++++++++++++++++++- tests/framework/data/SortTest.php | 48 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 1a0d6d0..ad4caf5 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -30,6 +30,7 @@ Yii Framework 2 Change Log - Bug #11541: Fixed default MySQL integer display width for unsigned primary key (h311ion, rob006, cebe) - Bug #12143: Fixed `yii\db\BaseActiveRecord::updateAttributes()` change `isNewRecord` state for the new model (klimov-paul) - Enh #9708: Added `yii\console\controllers\AssetController::deleteSource` option allowing deletion of the source asset files after compression (pana1990, klimov-paul) +- Enh #10243: Added `yii\data\Sort::setAttributeOrders()` method allowing manual setup of current sort (klimov-paul) - Enh #10583: Do not silence session errors in debug mode (samdark) - Enh #11658: Added argument to `yii\grid\ActionColumn::urlCreator` callback, which holds reference to the column instance (klimov-paul) - Enh #11804: Added `yii\behaviors\AttributeTypecastBehavior` for maintaining of strict ActiveRecord attribute types (klimov-paul) diff --git a/framework/data/Sort.php b/framework/data/Sort.php index f8abac5..0779c8c 100644 --- a/framework/data/Sort.php +++ b/framework/data/Sort.php @@ -67,7 +67,7 @@ use yii\web\Request; * 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. * @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. * @@ -266,6 +266,32 @@ class Sort extends Object } /** + * Sets up the currently sort information. + * @param array|null $attributeOrders sort directions indexed by attribute names. + * Sort direction can be either `SORT_ASC` for ascending order or + * `SORT_DESC` for descending order. + * @param boolean $validate whether to validate given attribute orders against [[attributes]] and [[enableMultiSort]]. + * If validation is enabled incorrect entries will be removed. + * @since 2.0.10 + */ + public function setAttributeOrders($attributeOrders, $validate = true) + { + if ($attributeOrders === null || !$validate) { + $this->_attributeOrders = $attributeOrders; + } else { + $this->_attributeOrders = []; + foreach ($attributeOrders as $attribute => $order) { + if (isset($this->attributes[$attribute])) { + $this->_attributeOrders[$attribute] = $order; + if (!$this->enableMultiSort) { + break; + } + } + } + } + } + + /** * 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` diff --git a/tests/framework/data/SortTest.php b/tests/framework/data/SortTest.php index 11b18f6..965abd5 100644 --- a/tests/framework/data/SortTest.php +++ b/tests/framework/data/SortTest.php @@ -53,6 +53,9 @@ class SortTest extends TestCase $this->assertEquals(SORT_ASC, $orders['age']); } + /** + * @depends testGetOrders + */ public function testGetAttributeOrders() { $sort = new Sort([ @@ -80,6 +83,9 @@ class SortTest extends TestCase $this->assertEquals(SORT_ASC, $orders['age']); } + /** + * @depends testGetAttributeOrders + */ public function testGetAttributeOrder() { $sort = new Sort([ @@ -101,6 +107,45 @@ class SortTest extends TestCase $this->assertNull($sort->getAttributeOrder('xyz')); } + /** + * @depends testGetAttributeOrders + */ + public function testSetAttributeOrders() + { + $sort = new Sort([ + 'attributes' => [ + 'age', + 'name' => [ + 'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC], + 'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC], + ], + ], + 'params' => [ + 'sort' => 'age,-name' + ], + 'enableMultiSort' => true, + ]); + + $orders = [ + 'age' => SORT_DESC, + 'name' => SORT_ASC, + ]; + $sort->setAttributeOrders($orders); + $this->assertEquals($orders, $sort->getAttributeOrders()); + + $sort->enableMultiSort = false; + $sort->setAttributeOrders($orders); + $this->assertEquals(['age' => SORT_DESC], $sort->getAttributeOrders()); + $sort->setAttributeOrders($orders, false); + $this->assertEquals($orders, $sort->getAttributeOrders()); + + $orders = ['unexistingAttribute' => SORT_ASC]; + $sort->setAttributeOrders($orders); + $this->assertEquals([], $sort->getAttributeOrders()); + $sort->setAttributeOrders($orders, false); + $this->assertEquals($orders, $sort->getAttributeOrders()); + } + public function testCreateSortParam() { $sort = new Sort([ @@ -150,6 +195,9 @@ class SortTest extends TestCase $this->assertEquals('/index.php?r=site%2Findex&sort=name%2Cage', $sort->createUrl('name')); } + /** + * @depends testCreateUrl + */ public function testLink() { $this->mockApplication();