Commit b7d6f614 by Qiang Xue

Added `isAssociative()` and `isIndexed()` to `yii\helpers\ArrayHelper`

parent 75154d35
...@@ -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)
......
...@@ -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
......
...@@ -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;
}
}
} }
...@@ -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));
}
} }
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