diff --git a/src/Schemator.php b/src/Schemator.php index 8e151be..fb9d80b 100644 --- a/src/Schemator.php +++ b/src/Schemator.php @@ -36,15 +36,18 @@ public function __construct(string $pathDelimiter = '.') * Converts input data with using schema * @param array $source input data to convert * @param array $schema schema for converting - * @return array converted data + * @return array|mixed converted data * @throws SchematorException */ - public function exec(array $source, array $schema): array + public function exec(array $source, array $schema) { $result = []; foreach($schema as $key => $schemaItem) { $this->saveByPath($result, $key, $this->getValue($source, $schemaItem)); + if($key === '') { + break; + } } return $result; @@ -186,6 +189,11 @@ protected function runFilter(array $filterConfig, $source, array $rootSource) */ protected function saveByPath(array &$source, string $path, $value): self { + if($path === '') { + $source = $value; + return $this; + } + $arPath = explode($this->pathDelimiter, $path); $temp = &$source; foreach($arPath as $key) { diff --git a/tests/unit/MassSchematorTest.php b/tests/unit/MassSchematorTest.php index 5053904..d38d448 100644 --- a/tests/unit/MassSchematorTest.php +++ b/tests/unit/MassSchematorTest.php @@ -81,7 +81,7 @@ public function testNested() $result[] = $item; } - $exprectedResult = [ + $expectedResult = [ [ 'my_city_id' => 100, 'my_country' => [ @@ -98,7 +98,19 @@ public function testNested() ], ]; - $this->assertEquals($exprectedResult, $result); + $this->assertEquals($expectedResult, $result); + } + + public function testExtra() + { + $massSchemator = new MassSchemator(new Schemator()); + $cities = $this->getCities(); + + $result = $massSchemator->exec($cities, [ + '' => 'name', + ]); + + $this->assertEquals(['Novgorod', 'Moscow'], $result); } /**