Skip to content

Commit

Permalink
Merge pull request #306 from Wixel/multidimensional
Browse files Browse the repository at this point in the history
Support multidimensional arrays validation
  • Loading branch information
filisko authored Mar 17, 2020
2 parents 519a179 + 01a60a6 commit 26526b9
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 112 deletions.
260 changes: 157 additions & 103 deletions gump.class.php

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions src/ArrayHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace GUMP;

class ArrayHelpers
{
public static function data_get($target, $key, $default = null)
{
if (is_null($key)) {
return $target;
}

$key = is_array($key) ? $key : explode('.', $key);

while (! is_null($segment = array_shift($key))) {
if ($segment === '*') {
if (! is_array($target)) {
return $default;
}

$result = [];

foreach ($target as $item) {
$result[] = self::data_get($item, $key);
}

return in_array('*', $key) ? self::collapse($result) : $result;
}

if (self::accessible($target) && self::exists($target, $segment)) {
$target = $target[$segment];
} elseif (is_object($target) && isset($target->{$segment})) {
$target = $target->{$segment};
} else {
return $default;
}
}

return $target;
}

/**
* Determine whether the given value is array accessible.
*
* @param mixed $value
* @return bool
*/
public static function accessible($value)
{
return is_array($value) || $value instanceof ArrayAccess;
}

/**
* Determine if the given key exists in the provided array.
*
* @param \ArrayAccess|array $array
* @param string|int $key
* @return bool
*/
public static function exists($array, $key)
{
if ($array instanceof ArrayAccess) {
return $array->offsetExists($key);
}

return array_key_exists($key, $array);
}

/**
* Collapse an array of arrays into a single array.
*
* @param array $array
* @return array
*/
public static function collapse($array)
{
$results = [];

foreach ($array as $values) {
if (! is_array($values)) {
continue;
}

$results[] = $values;
}

return array_merge([], ...$results);
}
}
39 changes: 39 additions & 0 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,43 @@ public function testItForwardsParametersToNativePHPFunctions()

$this->assertTrue(password_verify('my_password', $result['field']));
}

// public function testNestedArrays()
// {
// $data = [
// 'field0' => [' asd ', ''],
// 'field1' => [
// 'name' => ' test123 '
// ],
// 'field2' => [
// [
// 'name' => ' 123 '
// ],
// [
// 'name' => ' test '
// ],
// ]
// ];
//
// $result = $this->gump->filter($data, [
// 'field0' => 'trim',
// 'field1.name' => 'trim',
// 'field2.*.name' => 'trim',
// ]);
//
// $this->assertEquals([
// 'field0' => ['asd', ''],
// 'field1' => [
// 'name' => 'test123'
// ],
// 'field2' => [
// [
// 'name' => '123'
// ],
// [
// 'name' => 'test'
// ],
// ]
// ], $result);
// }
}
10 changes: 6 additions & 4 deletions tests/GetErrorsArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ public function testErrorMessageSplitsArrayParameterWithCommas()

public function testCustomFieldsErrorMessages()
{
$this->gump->set_fields_error_messages([
'test_number' => [
'between_len' => '{field} length MUST be between {param[0]} and {param[1]} !!!'
]
]);

$this->gump->validate([
'test_number' => '123'
], [
'test_number' => 'between_len,1;2'
], [
'test_number' => [
'between_len' => '{field} length MUST be between {param[0]} and {param[1]} !!!'
]
]);

$this->assertEquals([
Expand Down
10 changes: 6 additions & 4 deletions tests/GetReadableErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@ public function testWhenGumpInstanceIsCastedToStringItReturnsReadableErrorsInStr

public function testCustomFieldsErrorMessages()
{
$this->gump->set_fields_error_messages([
'test_number' => [
'between_len' => '{field} length MUST be between {param[0]} and {param[1]} !!!'
]
]);

$this->gump->validate([
'test_number' => '123'
], [
'test_number' => 'between_len,1;2'
], [
'test_number' => [
'between_len' => '{field} length MUST be between {param[0]} and {param[1]} !!!'
]
]);

$this->assertEquals([
Expand Down
57 changes: 57 additions & 0 deletions tests/RunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,61 @@ public function testOnSuccessReturnsArrayWithFiltersAppliedWithCheckFieldsSet()
'params' => []
]], $this->gump->errors());
}

public function testNestedArrays()
{
$this->gump->validation_rules([
'field0' => ['required'],
'field1.name' => ['required', 'alpha'],
'field2.*.name' => ['required', 'alpha_numeric'],
]);

GUMP::set_field_name('field1.name', 'Object Name');

$this->gump->set_fields_error_messages([
'field1.name' => ['alpha' => '{field} must contain alpha characters']
]);

$result = $this->gump->run([
'field0' => ['asd', ''],
'field1' => [
'name' => 'test123'
],
'field2' => [
[
'name' => '123'
],
[
'name' => ''
],
]
]);

$this->assertEquals([
[
'field' => 'field0',
'value' => ['asd', ''],
'rule' => 'required',
'params' => []
],
[
'field' => 'field1.name',
'value' => 'test123',
'rule' => 'alpha',
'params' => []
],
[
'field' => 'field2.*.name',
'value' => [ '123', '' ],
'rule' => 'required',
'params' => []
]
], $this->gump->errors());

$this->assertEquals([
'field0' => 'The Field0 field is required',
'field1.name' => 'Object Name must contain alpha characters',
'field2.*.name' => 'The Field2.*.name field is required'
], $this->gump->get_errors_array());
}
}
2 changes: 1 addition & 1 deletion tests/Validators/MinAgeValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function testOneDayBeforeBirthdayFails()

public function testWhenInputIsEmptyAndNotRequiredIsSuccess()
{
$this->helpersMock = m::mock('overload:GUMP\Helpers');
$this->helpersMock = m::mock('overload:GUMP\EnvHelpers');

$this->helpersMock->shouldReceive('date')
->once()
Expand Down

0 comments on commit 26526b9

Please sign in to comment.