Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
9efe4465
Commit
9efe4465
authored
Nov 19, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #1236: removed Sort::ASC and DESC
parent
e09cc16c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
58 deletions
+48
-58
ActiveDataProvider.php
framework/yii/data/ActiveDataProvider.php
+2
-2
Sort.php
framework/yii/data/Sort.php
+23
-33
SortTest.php
tests/unit/framework/data/SortTest.php
+21
-21
ArrayHelperTest.php
tests/unit/framework/helpers/ArrayHelperTest.php
+2
-2
No files found.
framework/yii/data/ActiveDataProvider.php
View file @
9efe4465
...
@@ -172,8 +172,8 @@ class ActiveDataProvider extends BaseDataProvider
...
@@ -172,8 +172,8 @@ class ActiveDataProvider extends BaseDataProvider
$model
=
new
$this
->
query
->
modelClass
;
$model
=
new
$this
->
query
->
modelClass
;
foreach
(
$model
->
attributes
()
as
$attribute
)
{
foreach
(
$model
->
attributes
()
as
$attribute
)
{
$sort
->
attributes
[
$attribute
]
=
[
$sort
->
attributes
[
$attribute
]
=
[
'asc'
=>
[
$attribute
=>
S
ort
::
ASC
],
'asc'
=>
[
$attribute
=>
S
ORT_
ASC
],
'desc'
=>
[
$attribute
=>
S
ort
::
DESC
],
'desc'
=>
[
$attribute
=>
S
ORT_
DESC
],
'label'
=>
$model
->
getAttributeLabel
(
$attribute
),
'label'
=>
$model
->
getAttributeLabel
(
$attribute
),
];
];
}
}
...
...
framework/yii/data/Sort.php
View file @
9efe4465
...
@@ -29,9 +29,9 @@ use yii\helpers\Inflector;
...
@@ -29,9 +29,9 @@ use yii\helpers\Inflector;
* 'attributes' => [
* 'attributes' => [
* 'age',
* 'age',
* 'name' => [
* 'name' => [
* 'asc' => ['first_name' => S
ort::ASC, 'last_name' => Sort::
ASC],
* 'asc' => ['first_name' => S
ORT_ASC, 'last_name' => SORT_
ASC],
* 'desc' => ['first_name' => S
ort::DESC, 'last_name' => Sort::
DESC],
* 'desc' => ['first_name' => S
ORT_DESC, 'last_name' => SORT_
DESC],
* 'default' => S
ort::
DESC,
* 'default' => S
ORT_
DESC,
* 'label' => 'Name',
* 'label' => 'Name',
* ],
* ],
* ],
* ],
...
@@ -66,7 +66,7 @@ use yii\helpers\Inflector;
...
@@ -66,7 +66,7 @@ use yii\helpers\Inflector;
* that can lead to pages with the data sorted by the corresponding attributes.
* 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
* @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
* @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.
* passed to [[\yii\db\Query::orderBy()]] to construct a DB query. This property is read-only.
*
*
...
@@ -76,16 +76,6 @@ use yii\helpers\Inflector;
...
@@ -76,16 +76,6 @@ use yii\helpers\Inflector;
class
Sort
extends
Object
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.
* @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.
* Defaults to false, which means each time the data can only be sorted by one attribute.
*/
*/
...
@@ -99,9 +89,9 @@ class Sort extends Object
...
@@ -99,9 +89,9 @@ class Sort extends Object
* [
* [
* 'age',
* 'age',
* 'name' => [
* 'name' => [
* 'asc' => ['first_name' => S
ort::ASC, 'last_name' => Sort::
ASC],
* 'asc' => ['first_name' => S
ORT_ASC, 'last_name' => SORT_
ASC],
* 'desc' => ['first_name' => S
ort::DESC, 'last_name' => Sort::
DESC],
* 'desc' => ['first_name' => S
ORT_DESC, 'last_name' => SORT_
DESC],
* 'default' => S
ort::
DESC,
* 'default' => S
ORT_
DESC,
* 'label' => 'Name',
* 'label' => 'Name',
* ],
* ],
* ]
* ]
...
@@ -112,9 +102,9 @@ class Sort extends Object
...
@@ -112,9 +102,9 @@ class Sort extends Object
*
*
* ~~~
* ~~~
* 'age' => [
* 'age' => [
* 'asc' => ['age' => S
ort::
ASC],
* 'asc' => ['age' => S
ORT_
ASC],
* 'desc' => ['age' => S
ort::
DESC],
* 'desc' => ['age' => S
ORT_
DESC],
* 'default' => S
ort::
ASC,
* 'default' => S
ORT_
ASC,
* 'label' => Inflector::camel2words('age'),
* 'label' => Inflector::camel2words('age'),
* ]
* ]
* ~~~
* ~~~
...
@@ -153,8 +143,8 @@ class Sort extends Object
...
@@ -153,8 +143,8 @@ class Sort extends Object
*
*
* ~~~
* ~~~
* [
* [
* 'name' => S
ort::
ASC,
* 'name' => S
ORT_
ASC,
* 'create_time' => S
ort::
DESC,
* 'create_time' => S
ORT_
DESC,
* ]
* ]
* ~~~
* ~~~
*
*
...
@@ -199,13 +189,13 @@ class Sort extends Object
...
@@ -199,13 +189,13 @@ class Sort extends Object
foreach
(
$this
->
attributes
as
$name
=>
$attribute
)
{
foreach
(
$this
->
attributes
as
$name
=>
$attribute
)
{
if
(
!
is_array
(
$attribute
))
{
if
(
!
is_array
(
$attribute
))
{
$attributes
[
$attribute
]
=
[
$attributes
[
$attribute
]
=
[
'asc'
=>
[
$attribute
=>
self
::
ASC
],
'asc'
=>
[
$attribute
=>
SORT_
ASC
],
'desc'
=>
[
$attribute
=>
self
::
DESC
],
'desc'
=>
[
$attribute
=>
SORT_
DESC
],
];
];
}
elseif
(
!
isset
(
$attribute
[
'asc'
],
$attribute
[
'desc'
]))
{
}
elseif
(
!
isset
(
$attribute
[
'asc'
],
$attribute
[
'desc'
]))
{
$attributes
[
$name
]
=
array_merge
([
$attributes
[
$name
]
=
array_merge
([
'asc'
=>
[
$name
=>
self
::
ASC
],
'asc'
=>
[
$name
=>
SORT_
ASC
],
'desc'
=>
[
$name
=>
self
::
DESC
],
'desc'
=>
[
$name
=>
SORT_
DESC
],
],
$attribute
);
],
$attribute
);
}
else
{
}
else
{
$attributes
[
$name
]
=
$attribute
;
$attributes
[
$name
]
=
$attribute
;
...
@@ -226,7 +216,7 @@ class Sort extends Object
...
@@ -226,7 +216,7 @@ class Sort extends Object
$orders
=
[];
$orders
=
[];
foreach
(
$attributeOrders
as
$attribute
=>
$direction
)
{
foreach
(
$attributeOrders
as
$attribute
=>
$direction
)
{
$definition
=
$this
->
attributes
[
$attribute
];
$definition
=
$this
->
attributes
[
$attribute
];
$columns
=
$definition
[
$direction
===
self
::
ASC
?
'asc'
:
'desc'
];
$columns
=
$definition
[
$direction
===
SORT_
ASC
?
'asc'
:
'desc'
];
foreach
(
$columns
as
$name
=>
$dir
)
{
foreach
(
$columns
as
$name
=>
$dir
)
{
$orders
[
$name
]
=
$dir
;
$orders
[
$name
]
=
$dir
;
}
}
...
@@ -243,8 +233,8 @@ class Sort extends Object
...
@@ -243,8 +233,8 @@ class Sort extends Object
* Returns the currently requested sort information.
* Returns the currently requested sort information.
* @param boolean $recalculate whether to recalculate the sort directions
* @param boolean $recalculate whether to recalculate the sort directions
* @return array sort directions indexed by attribute names.
* @return array sort directions indexed by attribute names.
* Sort direction can be either
[[Sort::ASC]]
for ascending order or
* Sort direction can be either
`SORT_ASC`
for ascending order or
*
[[Sort::DESC]]
for descending order.
*
`SORT_DESC`
for descending order.
*/
*/
public
function
getAttributeOrders
(
$recalculate
=
false
)
public
function
getAttributeOrders
(
$recalculate
=
false
)
{
{
...
@@ -262,7 +252,7 @@ class Sort extends Object
...
@@ -262,7 +252,7 @@ class Sort extends Object
}
}
if
(
isset
(
$this
->
attributes
[
$attribute
]))
{
if
(
isset
(
$this
->
attributes
[
$attribute
]))
{
$this
->
_attributeOrders
[
$attribute
]
=
$descending
;
$this
->
_attributeOrders
[
$attribute
]
=
$descending
?
SORT_DESC
:
SORT_ASC
;
if
(
!
$this
->
enableMultiSort
)
{
if
(
!
$this
->
enableMultiSort
)
{
return
$this
->
_attributeOrders
;
return
$this
->
_attributeOrders
;
}
}
...
@@ -279,8 +269,8 @@ class Sort extends Object
...
@@ -279,8 +269,8 @@ class Sort extends Object
/**
/**
* Returns the sort direction of the specified attribute in the current request.
* Returns the sort direction of the specified attribute in the current request.
* @param string $attribute the attribute name
* @param string $attribute the attribute name
* @return boolean|null Sort direction of the attribute. Can be either
[[Sort::ASC]]
* @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
* for ascending order or
`SORT_DESC`
for descending order. Null is returned
* if the attribute is invalid or does not need to be sorted.
* if the attribute is invalid or does not need to be sorted.
*/
*/
public
function
getAttributeOrder
(
$attribute
)
public
function
getAttributeOrder
(
$attribute
)
...
@@ -305,7 +295,7 @@ class Sort extends Object
...
@@ -305,7 +295,7 @@ class Sort extends Object
public
function
link
(
$attribute
,
$options
=
[])
public
function
link
(
$attribute
,
$options
=
[])
{
{
if
((
$direction
=
$this
->
getAttributeOrder
(
$attribute
))
!==
null
)
{
if
((
$direction
=
$this
->
getAttributeOrder
(
$attribute
))
!==
null
)
{
$class
=
$direction
?
'desc'
:
'asc'
;
$class
=
$direction
===
SORT_DESC
?
'desc'
:
'asc'
;
if
(
isset
(
$options
[
'class'
]))
{
if
(
isset
(
$options
[
'class'
]))
{
$options
[
'class'
]
.=
' '
.
$class
;
$options
[
'class'
]
.=
' '
.
$class
;
}
else
{
}
else
{
...
...
tests/unit/framework/data/SortTest.php
View file @
9efe4465
...
@@ -25,8 +25,8 @@ class SortTest extends TestCase
...
@@ -25,8 +25,8 @@ class SortTest extends TestCase
'attributes'
=>
[
'attributes'
=>
[
'age'
,
'age'
,
'name'
=>
[
'name'
=>
[
'asc'
=>
[
'first_name'
=>
S
ort
::
ASC
,
'last_name'
=>
Sort
::
ASC
],
'asc'
=>
[
'first_name'
=>
S
ORT_ASC
,
'last_name'
=>
SORT_
ASC
],
'desc'
=>
[
'first_name'
=>
S
ort
::
DESC
,
'last_name'
=>
Sort
::
DESC
],
'desc'
=>
[
'first_name'
=>
S
ORT_DESC
,
'last_name'
=>
SORT_
DESC
],
],
],
],
],
'params'
=>
[
'params'
=>
[
...
@@ -37,14 +37,14 @@ class SortTest extends TestCase
...
@@ -37,14 +37,14 @@ class SortTest extends TestCase
$orders
=
$sort
->
getOrders
();
$orders
=
$sort
->
getOrders
();
$this
->
assertEquals
(
3
,
count
(
$orders
));
$this
->
assertEquals
(
3
,
count
(
$orders
));
$this
->
assertEquals
(
S
ort
::
ASC
,
$orders
[
'age'
]);
$this
->
assertEquals
(
S
ORT_
ASC
,
$orders
[
'age'
]);
$this
->
assertEquals
(
S
ort
::
DESC
,
$orders
[
'first_name'
]);
$this
->
assertEquals
(
S
ORT_
DESC
,
$orders
[
'first_name'
]);
$this
->
assertEquals
(
S
ort
::
DESC
,
$orders
[
'last_name'
]);
$this
->
assertEquals
(
S
ORT_
DESC
,
$orders
[
'last_name'
]);
$sort
->
enableMultiSort
=
false
;
$sort
->
enableMultiSort
=
false
;
$orders
=
$sort
->
getOrders
(
true
);
$orders
=
$sort
->
getOrders
(
true
);
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
S
ort
::
ASC
,
$orders
[
'age'
]);
$this
->
assertEquals
(
S
ORT_
ASC
,
$orders
[
'age'
]);
}
}
public
function
testGetAttributeOrders
()
public
function
testGetAttributeOrders
()
...
@@ -53,8 +53,8 @@ class SortTest extends TestCase
...
@@ -53,8 +53,8 @@ class SortTest extends TestCase
'attributes'
=>
[
'attributes'
=>
[
'age'
,
'age'
,
'name'
=>
[
'name'
=>
[
'asc'
=>
[
'first_name'
=>
S
ort
::
ASC
,
'last_name'
=>
Sort
::
ASC
],
'asc'
=>
[
'first_name'
=>
S
ORT_ASC
,
'last_name'
=>
SORT_
ASC
],
'desc'
=>
[
'first_name'
=>
S
ort
::
DESC
,
'last_name'
=>
Sort
::
DESC
],
'desc'
=>
[
'first_name'
=>
S
ORT_DESC
,
'last_name'
=>
SORT_
DESC
],
],
],
],
],
'params'
=>
[
'params'
=>
[
...
@@ -65,13 +65,13 @@ class SortTest extends TestCase
...
@@ -65,13 +65,13 @@ class SortTest extends TestCase
$orders
=
$sort
->
getAttributeOrders
();
$orders
=
$sort
->
getAttributeOrders
();
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
S
ort
::
ASC
,
$orders
[
'age'
]);
$this
->
assertEquals
(
S
ORT_
ASC
,
$orders
[
'age'
]);
$this
->
assertEquals
(
S
ort
::
DESC
,
$orders
[
'name'
]);
$this
->
assertEquals
(
S
ORT_
DESC
,
$orders
[
'name'
]);
$sort
->
enableMultiSort
=
false
;
$sort
->
enableMultiSort
=
false
;
$orders
=
$sort
->
getAttributeOrders
(
true
);
$orders
=
$sort
->
getAttributeOrders
(
true
);
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
S
ort
::
ASC
,
$orders
[
'age'
]);
$this
->
assertEquals
(
S
ORT_
ASC
,
$orders
[
'age'
]);
}
}
public
function
testGetAttributeOrder
()
public
function
testGetAttributeOrder
()
...
@@ -80,8 +80,8 @@ class SortTest extends TestCase
...
@@ -80,8 +80,8 @@ class SortTest extends TestCase
'attributes'
=>
[
'attributes'
=>
[
'age'
,
'age'
,
'name'
=>
[
'name'
=>
[
'asc'
=>
[
'first_name'
=>
S
ort
::
ASC
,
'last_name'
=>
Sort
::
ASC
],
'asc'
=>
[
'first_name'
=>
S
ORT_ASC
,
'last_name'
=>
SORT_
ASC
],
'desc'
=>
[
'first_name'
=>
S
ort
::
DESC
,
'last_name'
=>
Sort
::
DESC
],
'desc'
=>
[
'first_name'
=>
S
ORT_DESC
,
'last_name'
=>
SORT_
DESC
],
],
],
],
],
'params'
=>
[
'params'
=>
[
...
@@ -90,8 +90,8 @@ class SortTest extends TestCase
...
@@ -90,8 +90,8 @@ class SortTest extends TestCase
'enableMultiSort'
=>
true
,
'enableMultiSort'
=>
true
,
]);
]);
$this
->
assertEquals
(
S
ort
::
ASC
,
$sort
->
getAttributeOrder
(
'age'
));
$this
->
assertEquals
(
S
ORT_
ASC
,
$sort
->
getAttributeOrder
(
'age'
));
$this
->
assertEquals
(
S
ort
::
DESC
,
$sort
->
getAttributeOrder
(
'name'
));
$this
->
assertEquals
(
S
ORT_
DESC
,
$sort
->
getAttributeOrder
(
'name'
));
$this
->
assertNull
(
$sort
->
getAttributeOrder
(
'xyz'
));
$this
->
assertNull
(
$sort
->
getAttributeOrder
(
'xyz'
));
}
}
...
@@ -101,8 +101,8 @@ class SortTest extends TestCase
...
@@ -101,8 +101,8 @@ class SortTest extends TestCase
'attributes'
=>
[
'attributes'
=>
[
'age'
,
'age'
,
'name'
=>
[
'name'
=>
[
'asc'
=>
[
'first_name'
=>
S
ort
::
ASC
,
'last_name'
=>
Sort
::
ASC
],
'asc'
=>
[
'first_name'
=>
S
ORT_ASC
,
'last_name'
=>
SORT_
ASC
],
'desc'
=>
[
'first_name'
=>
S
ort
::
DESC
,
'last_name'
=>
Sort
::
DESC
],
'desc'
=>
[
'first_name'
=>
S
ORT_DESC
,
'last_name'
=>
SORT_
DESC
],
],
],
],
],
'params'
=>
[
'params'
=>
[
...
@@ -127,8 +127,8 @@ class SortTest extends TestCase
...
@@ -127,8 +127,8 @@ class SortTest extends TestCase
'attributes'
=>
[
'attributes'
=>
[
'age'
,
'age'
,
'name'
=>
[
'name'
=>
[
'asc'
=>
[
'first_name'
=>
S
ort
::
ASC
,
'last_name'
=>
Sort
::
ASC
],
'asc'
=>
[
'first_name'
=>
S
ORT_ASC
,
'last_name'
=>
SORT_
ASC
],
'desc'
=>
[
'first_name'
=>
S
ort
::
DESC
,
'last_name'
=>
Sort
::
DESC
],
'desc'
=>
[
'first_name'
=>
S
ORT_DESC
,
'last_name'
=>
SORT_
DESC
],
],
],
],
],
'params'
=>
[
'params'
=>
[
...
@@ -155,8 +155,8 @@ class SortTest extends TestCase
...
@@ -155,8 +155,8 @@ class SortTest extends TestCase
'attributes'
=>
[
'attributes'
=>
[
'age'
,
'age'
,
'name'
=>
[
'name'
=>
[
'asc'
=>
[
'first_name'
=>
S
ort
::
ASC
,
'last_name'
=>
Sort
::
ASC
],
'asc'
=>
[
'first_name'
=>
S
ORT_ASC
,
'last_name'
=>
SORT_
ASC
],
'desc'
=>
[
'first_name'
=>
S
ort
::
DESC
,
'last_name'
=>
Sort
::
DESC
],
'desc'
=>
[
'first_name'
=>
S
ORT_DESC
,
'last_name'
=>
SORT_
DESC
],
],
],
],
],
'params'
=>
[
'params'
=>
[
...
...
tests/unit/framework/helpers/ArrayHelperTest.php
View file @
9efe4465
...
@@ -147,7 +147,7 @@ class ArrayHelperTest extends TestCase
...
@@ -147,7 +147,7 @@ class ArrayHelperTest extends TestCase
// single key
// single key
$sort
=
new
Sort
([
$sort
=
new
Sort
([
'attributes'
=>
[
'name'
,
'age'
],
'attributes'
=>
[
'name'
,
'age'
],
'defaultOrder'
=>
[
'name'
=>
S
ort
::
ASC
],
'defaultOrder'
=>
[
'name'
=>
S
ORT_
ASC
],
]);
]);
$orders
=
$sort
->
getOrders
();
$orders
=
$sort
->
getOrders
();
...
@@ -164,7 +164,7 @@ class ArrayHelperTest extends TestCase
...
@@ -164,7 +164,7 @@ class ArrayHelperTest extends TestCase
// multiple keys
// multiple keys
$sort
=
new
Sort
([
$sort
=
new
Sort
([
'attributes'
=>
[
'name'
,
'age'
],
'attributes'
=>
[
'name'
,
'age'
],
'defaultOrder'
=>
[
'name'
=>
S
ort
::
ASC
,
'age'
=>
Sort
::
DESC
],
'defaultOrder'
=>
[
'name'
=>
S
ORT_ASC
,
'age'
=>
SORT_
DESC
],
]);
]);
$orders
=
$sort
->
getOrders
();
$orders
=
$sort
->
getOrders
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment