diff --git a/benchmark/bechmark.php b/benchmark/bechmark.php index 28447c9..b083fca 100644 --- a/benchmark/bechmark.php +++ b/benchmark/bechmark.php @@ -15,8 +15,7 @@ $iterations++; $service = new DataTransformer; -$service->getMapsManager()->setMapDir(UserModel::class, __DIR__ . '/maps/' . UserModel::class); -$service->getMapsManager()->setMapDir(ChildModel::class, __DIR__ . '/maps/' . ChildModel::class); +$service->getMapsManager()->setDefaultMapDir(__DIR__ . '/maps/'); $collectionDto = []; while ($iterations--) { diff --git a/src/MapsManager.php b/src/MapsManager.php index 6c0e6f2..be99db2 100644 --- a/src/MapsManager.php +++ b/src/MapsManager.php @@ -7,14 +7,22 @@ class MapsManager { /** @var array */ protected $cache = []; - /** @var array */ + /** @var string[] */ protected $mapsDirs = []; /** @var NormalizerInterface */ protected $normalizer; + /** @var string */ + protected $defaultMapsDirs = ''; - public function __construct(NormalizerInterface $normalizer = null) + public function __construct(NormalizerInterface $normalizer = null, string $dir = '') { $this->normalizer = $normalizer ?? new Normalizer; + $this->setDefaultMapDir($dir); + } + + public function setDefaultMapDir(string $dir): void + { + $this->defaultMapsDirs = $dir; } public function setMapDir(string $entityName, string $dir): void @@ -26,14 +34,27 @@ public function getMap(string $entityName, string $mapName = 'dto'): array { $map = $this->cache[$entityName][$mapName] ?? null; if ($map === null) { - $dir = $this->mapsDirs[$entityName]; - $rules = require $dir.'/'.$mapName.'.php'; + $dir = $this->getMapDir($entityName); + $rules = require $dir.DIRECTORY_SEPARATOR.$mapName.'.php'; $this->setMap($rules, $entityName, $mapName); } return $this->cache[$entityName][$mapName]; } + public function getMapDir(string $entityName): string + { + $dir = $this->mapsDirs[$entityName] ?? null; + + if (!$dir) { + $parts = explode('\\', $entityName); + $class = array_pop($parts); + $dir = $this->defaultMapsDirs . DIRECTORY_SEPARATOR . $class; + } + + return $dir; + } + public function setMap(array $rules, string $entityName, $mapName = 'dto'): void { $this->cache[$entityName][$mapName] = $this->normalizer->normalize($rules); diff --git a/test/unit/MapsManagerTest.php b/test/unit/MapsManagerTest.php index 94a5ff3..bd1d25e 100644 --- a/test/unit/MapsManagerTest.php +++ b/test/unit/MapsManagerTest.php @@ -44,4 +44,14 @@ public function testGetMapWithCache(): void self::assertCount(0, $map2['refs']); self::assertSame($map, $map2); } + + public function testGetMapFromDefaultDir(): void + { + $this->manager->setDefaultMapDir(__DIR__ . '/data'); + $map = $this->manager->getMap('Namespace\UserModel'); + self::assertCount(3, $map); + self::assertCount(6, $map['rules']); + self::assertCount(2, $map['pipe']); + self::assertCount(0, $map['refs']); + } } diff --git a/test/unit/data/UserModel/dto.php b/test/unit/data/UserModel/dto.php new file mode 100644 index 0000000..04a26d9 --- /dev/null +++ b/test/unit/data/UserModel/dto.php @@ -0,0 +1,21 @@ + [], + 'creAt' => [], + 'name' => [], + 'login' => [], + 'active' => [ + 'pipe' => [ + [ + 'populate' => 'boolval', + 'extract' => 'boolval', + ] + ] + ], + 'email' => [ + 'pipe' => [ + 'strtolower' + ] + ], +]; \ No newline at end of file