diff --git a/src/Helper.php b/src/Helper.php index ab8539e..4dcf57b 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -282,7 +282,7 @@ public static function call($cb, ...$args) // className::method if (strpos($cb, '::') > 0) { $cb = explode('::', $cb, 2); - // function + // function } elseif (function_exists($cb)) { return $cb(...$args); } diff --git a/src/ValidationTrait.php b/src/ValidationTrait.php index 04a2925..bdf93c5 100644 --- a/src/ValidationTrait.php +++ b/src/ValidationTrait.php @@ -636,32 +636,34 @@ protected function getByWildcard(string $path, $default = null, array $data = [] $result = []; - // eg: "companies.*.departments.*.employees.*.name" => "departments.*.employees.*.name" + // eg: "companies.*.departments.*.employees.*.name" => $subPath: "departments.*.employees.*.name" if (strpos($subPath, '.*') > 0) { foreach ($recently as $item) { if (is_array($item)) { - // $result[] = $this->getByWildcard($subPath, $this->_arrayNotKeyValue, $item); $result[] = $this->getByWildcard($subPath, $this->_arrayNotKeyValue, $item); } } // return $result; + // expand all sub-values one dimensional array. eg: [[1, 2], [3, 4]] => [1, 2, 3, 4] return array_merge(...$result); } - // eg: "companies.0.departments.*.employees.0.manage" => "employees.0.manage" + // eg: "companies.0.departments.*.employees.0.manage" => $subPath: "employees.0.manage" if (strpos($subPath, '.') > 0) { foreach ($recently as $item) { if (is_array($item)) { $result[] = Helper::getValueOfArray($item, $subPath, $this->_arrayNotKeyValue); } } - } else { - // eg: 'users.*.id' => 'id' - foreach ($recently as $item) { - if (is_array($item)) { - $result[] = $item[$subPath] ?? $this->_arrayNotKeyValue; - } + + return $result; + } + + // eg: 'users.*.id' => $subPath: 'id' + foreach ($recently as $item) { + if (is_array($item)) { + $result[] = $item[$subPath] ?? $this->_arrayNotKeyValue; } } diff --git a/test/ValidationTraitTest.php b/test/ValidationTraitTest.php index 91590ce..2ba6efe 100644 --- a/test/ValidationTraitTest.php +++ b/test/ValidationTraitTest.php @@ -34,7 +34,17 @@ public function testGetByPath(): void 'attr' => [ 'wid' => 1 ] - ] + ], + [ + 'attr' => [ + 'wid' => 2, + ] + ], + [ + 'attr' => [ + 'wid' => 3, + ] + ], ], 'users' => [ ['id' => 1,], @@ -55,61 +65,31 @@ public function testGetByPath(): void $this->assertSame(1, $val); $val = $v->getByPath('prod.*.attr'); - $this->assertSame([['wid' => 1]], $val); + $this->assertSame([['wid' => 1], ['wid' => 2], ['wid' => 3],], $val); - // $val = $v->getByPath('prod.*.attr.wid'); - // $this->assertSame([1], $val); - } - - public function testBeforeAndAfter(): void - { - $v = Validation::make(['name' => 'inhere'], [ - ['name', 'string', 'min' => 3, 'filter' => 'trim|upper'] - ]); - - $v->onBeforeValidate(function (Validation $v) { - $this->assertSame('inhere', $v->getRaw('name')); - $this->assertNull($v->getSafe('name')); - - return true; - }); - - $v->onAfterValidate(function (Validation $v) { - $this->assertSame('INHERE', $v->getRaw('name')); - $this->assertSame('INHERE', $v->getSafe('name')); - }); - - $v->validate(); - - $this->assertTrue($v->isOk()); - $this->assertTrue($v->isValidated()); - - $v->validate(); - - $this->assertTrue($v->isValidated()); - } - - public function testRuleBeforeAndAfter(): void - { - $v = Validation::make(['name' => 'inhere'], [ - [ - 'name', - 'string', - 'min' => 3, - 'before' => function ($value) { - return $value === 'inhere'; - }, - 'after' => function ($value) { - $this->assertSame('inhere', $value); - return true; - } - ] - ]); - - $v->validate(); - $this->assertTrue($v->isOk()); + $val = $v->getByPath('prod.*.attr.wid'); + $this->assertSame([1, 2, 3], $val); } + // TODO key is must exists on data. + // public function testIndexedArrayGetByPath(): void + // { + // $v = Validation::make([ + // ['attr' => ['wid' => 1]], + // ['attr' => ['wid' => 2]], + // ['attr' => ['wid' => 3]], + // ]); + // + // $val = $v->GetByPath('0.attr'); + // $this->assertSame(['wid' => 1], $val); + // + // $val = $v->getByPath('0.attr.wid'); + // $this->assertSame(1, $val); + // } + + /** + * @var \array[][] see PR https://github.com/inhere/php-validate/pull/19 + */ public $deepData = [ 'companies' => [ [ @@ -208,4 +188,53 @@ public function testMultidimensionalArray3(): void $val = $v->getByPath('companies.*.departments.*.employees.*.name'); $this->assertSame(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'xxx', 'yyy'], $val); } + + public function testBeforeAndAfter(): void + { + $v = Validation::make(['name' => 'inhere'], [ + ['name', 'string', 'min' => 3, 'filter' => 'trim|upper'] + ]); + + $v->onBeforeValidate(function (Validation $v) { + $this->assertSame('inhere', $v->getRaw('name')); + $this->assertNull($v->getSafe('name')); + + return true; + }); + + $v->onAfterValidate(function (Validation $v) { + $this->assertSame('INHERE', $v->getRaw('name')); + $this->assertSame('INHERE', $v->getSafe('name')); + }); + + $v->validate(); + + $this->assertTrue($v->isOk()); + $this->assertTrue($v->isValidated()); + + $v->validate(); + + $this->assertTrue($v->isValidated()); + } + + public function testRuleBeforeAndAfter(): void + { + $v = Validation::make(['name' => 'inhere'], [ + [ + 'name', + 'string', + 'min' => 3, + 'before' => function ($value) { + return $value === 'inhere'; + }, + 'after' => function ($value) { + $this->assertSame('inhere', $value); + return true; + } + ] + ]); + + $v->validate(); + $this->assertTrue($v->isOk()); + } }