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
b7d6f614
Commit
b7d6f614
authored
Apr 07, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added `isAssociative()` and `isIndexed()` to `yii\helpers\ArrayHelper`
parent
75154d35
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
107 additions
and
0 deletions
+107
-0
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
BaseActiveRecord.php
framework/db/BaseActiveRecord.php
+22
-0
BaseArrayHelper.php
framework/helpers/BaseArrayHelper.php
+67
-0
ArrayHelperTest.php
tests/unit/framework/helpers/ArrayHelperTest.php
+17
-0
No files found.
framework/CHANGELOG.md
View file @
b7d6f614
...
@@ -188,6 +188,7 @@ Yii Framework 2 Change Log
...
@@ -188,6 +188,7 @@ Yii Framework 2 Change Log
-
Enh: Implemented Oracle column comment reading from another schema (gureedo, samdark)
-
Enh: Implemented Oracle column comment reading from another schema (gureedo, samdark)
-
Enh: Added support to allow an event handler to be inserted at the beginning of the existing event handler list (qiangxue)
-
Enh: Added support to allow an event handler to be inserted at the beginning of the existing event handler list (qiangxue)
-
Enh: Improved action filter and action execution flow by supporting installing action filters at controller, module and application levels (qiangxue)
-
Enh: Improved action filter and action execution flow by supporting installing action filters at controller, module and application levels (qiangxue)
-
Enh: Added
`isAssociative()`
and
`isIndexed()`
to
`yii\helpers\ArrayHelper`
(qiangxue)
-
Chg #47: Changed Markdown library to cebe/markdown and adjusted Markdown helper API (cebe)
-
Chg #47: Changed Markdown library to cebe/markdown and adjusted Markdown helper API (cebe)
-
Chg #735: Added back
`ActiveField::hiddenInput()`
(qiangxue)
-
Chg #735: Added back
`ActiveField::hiddenInput()`
(qiangxue)
-
Chg #1186: Changed
`Sort`
to use comma to separate multiple sort fields and use negative sign to indicate descending sort (qiangxue)
-
Chg #1186: Changed
`Sort`
to use comma to separate multiple sort fields and use negative sign to indicate descending sort (qiangxue)
...
...
framework/db/BaseActiveRecord.php
View file @
b7d6f614
...
@@ -98,6 +98,28 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
...
@@ -98,6 +98,28 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
{
{
$query
=
static
::
find
();
$query
=
static
::
find
();
if
(
is_array
(
$condition
))
{
if
(
is_array
(
$condition
))
{
// hash condition
return
$query
->
andWhere
(
$condition
)
->
one
();
}
else
{
// query by primary key
$primaryKey
=
static
::
primaryKey
();
if
(
isset
(
$primaryKey
[
0
]))
{
return
$query
->
andWhere
([
$primaryKey
[
0
]
=>
$condition
])
->
one
();
}
else
{
throw
new
InvalidConfigException
(
get_called_class
()
.
' must have a primary key.'
);
}
}
}
/**
* @inheritdoc
*/
public
static
function
findAll
(
$condition
)
{
$query
=
static
::
find
();
if
(
is_array
(
$condition
))
{
// hash condition
return
$query
->
andWhere
(
$condition
)
->
one
();
return
$query
->
andWhere
(
$condition
)
->
one
();
}
else
{
}
else
{
// query by primary key
// query by primary key
...
...
framework/helpers/BaseArrayHelper.php
View file @
b7d6f614
...
@@ -490,4 +490,71 @@ class BaseArrayHelper
...
@@ -490,4 +490,71 @@ class BaseArrayHelper
return
$d
;
return
$d
;
}
}
/**
* Returns a value indicating whether the given array is an associative array.
*
* An array is associative if all its keys are strings. If `$allStrings` is false,
* then an array will be treated as associative if at least one of its keys is a string.
*
* Note that an empty array will NOT be considered associative.
*
* @param array $array the array being checked
* @param boolean $allStrings whether the array keys must be all strings in order for
* the array to be treated as associative.
* @return boolean whether the array is associative
*/
public
static
function
isAssociative
(
array
$array
,
$allStrings
=
true
)
{
if
(
empty
(
$array
))
{
return
false
;
}
if
(
$allStrings
)
{
foreach
(
$array
as
$key
=>
$value
)
{
if
(
!
is_string
(
$key
))
{
return
false
;
}
}
return
true
;
}
else
{
foreach
(
$array
as
$key
=>
$value
)
{
if
(
is_string
(
$key
))
{
return
true
;
}
}
return
false
;
}
}
/**
* Returns a value indicating whether the given array is an indexed array.
*
* An array is indexed if all its keys are integers. If `$consecutive` is true,
* then the array keys must be a consecutive sequence starting from 0.
*
* Note that an empty array will be considered indexed.
*
* @param array $array the array being checked
* @param boolean $consecutive whether the array keys must be a consecutive sequence
* in order for the array to be treated as indexed.
* @return boolean whether the array is associative
*/
public
static
function
isIndexed
(
array
$array
,
$consecutive
=
false
)
{
if
(
empty
(
$array
))
{
return
true
;
}
if
(
$consecutive
)
{
return
array_keys
(
$array
)
===
range
(
0
,
count
(
$array
)
-
1
);
}
else
{
foreach
(
$array
as
$key
=>
$value
)
{
if
(
!
is_integer
(
$key
))
{
return
false
;
}
}
return
true
;
}
}
}
}
tests/unit/framework/helpers/ArrayHelperTest.php
View file @
b7d6f614
...
@@ -377,4 +377,21 @@ class ArrayHelperTest extends TestCase
...
@@ -377,4 +377,21 @@ class ArrayHelperTest extends TestCase
$this
->
assertEquals
(
$expected
,
ArrayHelper
::
getValue
(
$array
,
$key
,
$default
));
$this
->
assertEquals
(
$expected
,
ArrayHelper
::
getValue
(
$array
,
$key
,
$default
));
}
}
public
function
testIsAssociative
()
{
$this
->
assertFalse
(
ArrayHelper
::
isAssociative
([]));
$this
->
assertFalse
(
ArrayHelper
::
isAssociative
([
1
,
2
,
3
]));
$this
->
assertTrue
(
ArrayHelper
::
isAssociative
([
'name'
=>
1
,
'value'
=>
'test'
]));
$this
->
assertFalse
(
ArrayHelper
::
isAssociative
([
'name'
=>
1
,
'value'
=>
'test'
,
3
]));
$this
->
assertTrue
(
ArrayHelper
::
isAssociative
([
'name'
=>
1
,
'value'
=>
'test'
,
3
],
false
));
}
public
function
testIsIndexed
()
{
$this
->
assertTrue
(
ArrayHelper
::
isIndexed
([]));
$this
->
assertTrue
(
ArrayHelper
::
isIndexed
([
1
,
2
,
3
]));
$this
->
assertTrue
(
ArrayHelper
::
isIndexed
([
2
=>
'a'
,
3
=>
'b'
]));
$this
->
assertFalse
(
ArrayHelper
::
isIndexed
([
2
=>
'a'
,
3
=>
'b'
],
true
));
}
}
}
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