Skip to content

Commit 8b1cf2d

Browse files
committed
Add default dir for map
1 parent 2e39cc0 commit 8b1cf2d

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

benchmark/bechmark.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
$iterations++;
1616

1717
$service = new DataTransformer;
18-
$service->getMapsManager()->setMapDir(UserModel::class, __DIR__ . '/maps/' . UserModel::class);
19-
$service->getMapsManager()->setMapDir(ChildModel::class, __DIR__ . '/maps/' . ChildModel::class);
18+
$service->getMapsManager()->setDefaultMapDir(__DIR__ . '/maps/');
2019

2120
$collectionDto = [];
2221
while ($iterations--) {

src/MapsManager.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ class MapsManager
77
{
88
/** @var array */
99
protected $cache = [];
10-
/** @var array */
10+
/** @var string[] */
1111
protected $mapsDirs = [];
1212
/** @var NormalizerInterface */
1313
protected $normalizer;
14+
/** @var string */
15+
protected $defaultMapsDirs = '';
1416

15-
public function __construct(NormalizerInterface $normalizer = null)
17+
public function __construct(NormalizerInterface $normalizer = null, string $dir = '')
1618
{
1719
$this->normalizer = $normalizer ?? new Normalizer;
20+
$this->setDefaultMapDir($dir);
21+
}
22+
23+
public function setDefaultMapDir(string $dir): void
24+
{
25+
$this->defaultMapsDirs = $dir;
1826
}
1927

2028
public function setMapDir(string $entityName, string $dir): void
@@ -26,14 +34,27 @@ public function getMap(string $entityName, string $mapName = 'dto'): array
2634
{
2735
$map = $this->cache[$entityName][$mapName] ?? null;
2836
if ($map === null) {
29-
$dir = $this->mapsDirs[$entityName];
30-
$rules = require $dir.'/'.$mapName.'.php';
37+
$dir = $this->getMapDir($entityName);
38+
$rules = require $dir.DIRECTORY_SEPARATOR.$mapName.'.php';
3139
$this->setMap($rules, $entityName, $mapName);
3240
}
3341

3442
return $this->cache[$entityName][$mapName];
3543
}
3644

45+
public function getMapDir(string $entityName): string
46+
{
47+
$dir = $this->mapsDirs[$entityName] ?? null;
48+
49+
if (!$dir) {
50+
$parts = explode('\\', $entityName);
51+
$class = array_pop($parts);
52+
$dir = $this->defaultMapsDirs . DIRECTORY_SEPARATOR . $class;
53+
}
54+
55+
return $dir;
56+
}
57+
3758
public function setMap(array $rules, string $entityName, $mapName = 'dto'): void
3859
{
3960
$this->cache[$entityName][$mapName] = $this->normalizer->normalize($rules);

test/unit/MapsManagerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ public function testGetMapWithCache(): void
4444
self::assertCount(0, $map2['refs']);
4545
self::assertSame($map, $map2);
4646
}
47+
48+
public function testGetMapFromDefaultDir(): void
49+
{
50+
$this->manager->setDefaultMapDir(__DIR__ . '/data');
51+
$map = $this->manager->getMap('Namespace\UserModel');
52+
self::assertCount(3, $map);
53+
self::assertCount(6, $map['rules']);
54+
self::assertCount(2, $map['pipe']);
55+
self::assertCount(0, $map['refs']);
56+
}
4757
}

test/unit/data/UserModel/dto.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
return [
4+
'id' => [],
5+
'creAt' => [],
6+
'name' => [],
7+
'login' => [],
8+
'active' => [
9+
'pipe' => [
10+
[
11+
'populate' => 'boolval',
12+
'extract' => 'boolval',
13+
]
14+
]
15+
],
16+
'email' => [
17+
'pipe' => [
18+
'strtolower'
19+
]
20+
],
21+
];

0 commit comments

Comments
 (0)