Skip to content

Commit

Permalink
Add default dir for map
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpts committed Feb 29, 2020
1 parent 2e39cc0 commit 8b1cf2d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
3 changes: 1 addition & 2 deletions benchmark/bechmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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--) {
Expand Down
29 changes: 25 additions & 4 deletions src/MapsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions test/unit/MapsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
21 changes: 21 additions & 0 deletions test/unit/data/UserModel/dto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

return [
'id' => [],
'creAt' => [],
'name' => [],
'login' => [],
'active' => [
'pipe' => [
[
'populate' => 'boolval',
'extract' => 'boolval',
]
]
],
'email' => [
'pipe' => [
'strtolower'
]
],
];

0 comments on commit 8b1cf2d

Please sign in to comment.