Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
376 changes: 342 additions & 34 deletions system/Helpers/Array/ArrayHelper.php

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions system/Helpers/array_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,72 @@ function dot_array_search(string $index, array $array)
}
}

if (! function_exists('dot_array_has')) {
/**
* Checks if an array key exists using dot syntax.
*
* @param array<array-key, mixed> $array
*/
function dot_array_has(string $index, array $array): bool
{
return ArrayHelper::dotHas($index, $array);
}
}

if (! function_exists('dot_array_set')) {
/**
* Sets an array value using dot syntax.
*
* @param array<array-key, mixed> $array
*/
function dot_array_set(array &$array, string $index, mixed $value): void
{
ArrayHelper::dotSet($array, $index, $value);
}
}

if (! function_exists('dot_array_unset')) {
/**
* Unsets an array value using dot syntax.
*
* @param array<array-key, mixed> $array
*/
function dot_array_unset(array &$array, string $index): bool
{
return ArrayHelper::dotUnset($array, $index);
}
}

if (! function_exists('dot_array_only')) {
/**
* Gets only the specified keys using dot syntax.
*
* @param array<array-key, mixed> $array
* @param list<string>|string $indexes
*
* @return array<array-key, mixed>
*/
function dot_array_only(array $array, array|string $indexes): array
{
return ArrayHelper::dotOnly($array, $indexes);
}
}

if (! function_exists('dot_array_except')) {
/**
* Gets all keys except the specified ones using dot syntax.
*
* @param array<array-key, mixed> $array
* @param list<string>|string $indexes
*
* @return array<array-key, mixed>
*/
function dot_array_except(array $array, array|string $indexes): array
{
return ArrayHelper::dotExcept($array, $indexes);
}
}

if (! function_exists('array_deep_search')) {
/**
* Returns the value of an element at a key in an array of uncertain depth.
Expand Down
2 changes: 1 addition & 1 deletion system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public function field_exists(
?string $field = null,
): bool {
if (str_contains($field, '.')) {
return ArrayHelper::dotKeyExists($field, $data);
return ArrayHelper::dotHas($field, $data);
}

return array_key_exists($field, $data);
Expand Down
6 changes: 3 additions & 3 deletions system/Validation/StrictRules/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function differs(
}

if (str_contains($field, '.')) {
if (! ArrayHelper::dotKeyExists($field, $data)) {
if (! ArrayHelper::dotHas($field, $data)) {
return false;
}
} elseif (! array_key_exists($field, $data)) {
Expand Down Expand Up @@ -245,7 +245,7 @@ public function matches(
}

if (str_contains($field, '.')) {
if (! ArrayHelper::dotKeyExists($field, $data)) {
if (! ArrayHelper::dotHas($field, $data)) {
return false;
}
} elseif (! array_key_exists($field, $data)) {
Expand Down Expand Up @@ -386,7 +386,7 @@ public function field_exists(
?string $field = null,
): bool {
if (str_contains($field, '.')) {
return ArrayHelper::dotKeyExists($field, $data);
return ArrayHelper::dotHas($field, $data);
}

return array_key_exists($field, $data);
Expand Down
74 changes: 74 additions & 0 deletions tests/system/Helpers/Array/ArrayHelperDotHasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Helpers\Array;

use CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\Test\CIUnitTestCase;
use PHPUnit\Framework\Attributes\Group;

/**
* @internal
*/
#[Group('Others')]
final class ArrayHelperDotHasTest extends CIUnitTestCase
{
private array $array = [
'contacts' => [
'friends' => [
['name' => 'Fred Flinstone', 'age' => 20],
['age' => 21], // 'name' key does not exist
],
],
];

public function testDotHas(): void
{
$this->assertFalse(ArrayHelper::dotHas('', $this->array));
$this->assertTrue(ArrayHelper::dotHas('contacts', $this->array));
$this->assertFalse(ArrayHelper::dotHas('not', $this->array));
$this->assertTrue(ArrayHelper::dotHas('contacts.friends', $this->array));
$this->assertFalse(ArrayHelper::dotHas('not.friends', $this->array));
$this->assertTrue(ArrayHelper::dotHas('contacts.friends.0.name', $this->array));
$this->assertFalse(ArrayHelper::dotHas('contacts.friends.1.name', $this->array));
}

public function testDotHasWithEndingWildCard(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('You must set key right after "*". Invalid index: "contacts.*"');

$this->assertTrue(ArrayHelper::dotHas('contacts.*', $this->array));
}

public function testDotHasWithDoubleWildCard(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('You must set key right after "*". Invalid index: "contacts.*.*.age"');

$this->assertTrue(ArrayHelper::dotHas('contacts.*.*.age', $this->array));
}

public function testDotHasWithWildCard(): void
{
$this->assertTrue(ArrayHelper::dotHas('*.friends', $this->array));
$this->assertTrue(ArrayHelper::dotHas('contacts.friends.*.age', $this->array));
$this->assertFalse(ArrayHelper::dotHas('contacts.friends.*.name', $this->array));
$this->assertTrue(ArrayHelper::dotHas('*.friends.*.age', $this->array));
$this->assertFalse(ArrayHelper::dotHas('*.friends.*.name', $this->array));
$this->assertTrue(ArrayHelper::dotHas('contacts.*.0.age', $this->array));
$this->assertTrue(ArrayHelper::dotHas('contacts.*.1.age', $this->array));
$this->assertTrue(ArrayHelper::dotHas('contacts.*.0.name', $this->array));
$this->assertFalse(ArrayHelper::dotHas('contacts.*.1.name', $this->array));
}
}
74 changes: 0 additions & 74 deletions tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php

This file was deleted.

Loading
Loading