Skip to content

Commit 6e5d65c

Browse files
committed
optimizations
1 parent 484c585 commit 6e5d65c

File tree

8 files changed

+157
-37
lines changed

8 files changed

+157
-37
lines changed

benchmark/ChildModel.php

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
class ChildModel
5+
{
6+
protected $id;
7+
/** @var string */
8+
protected $name;
9+
/** @var string */
10+
protected $login;
11+
/** @var DateTime */
12+
protected $creAt;
13+
/** @var bool */
14+
protected $active;
15+
16+
public function __construct()
17+
{
18+
$this->creAt = new DateTime;
19+
}
20+
21+
public function __toString(): string
22+
{
23+
return (string)$this->id;
24+
}
25+
26+
public function getEmail(): string
27+
{
28+
return $this->email;
29+
}
30+
31+
public function setEmail(string $email): void
32+
{
33+
$this->email = $email;
34+
}
35+
36+
/** @var string */
37+
protected $email;
38+
39+
public function getId()
40+
{
41+
return $this->id;
42+
}
43+
44+
public function setId($id): void
45+
{
46+
$this->id = $id;
47+
}
48+
49+
public function getName(): ?string
50+
{
51+
return $this->name;
52+
}
53+
54+
public function setName(string $name): void
55+
{
56+
$this->name = $name;
57+
}
58+
59+
public function getLogin(): ?string
60+
{
61+
return $this->login;
62+
}
63+
64+
public function setLogin(string $login): void
65+
{
66+
$this->login = $login;
67+
}
68+
69+
public function getCreAt(): DateTime
70+
{
71+
return $this->creAt;
72+
}
73+
74+
public function setCreAt(DateTime $creAt): void
75+
{
76+
$this->creAt = $creAt;
77+
}
78+
79+
public function isActive(): bool
80+
{
81+
return $this->active;
82+
}
83+
84+
public function setActive(bool $active): void
85+
{
86+
$this->active = $active;
87+
}
88+
89+
public function getCreAtTimestamp(): int
90+
{
91+
return $this->creAt->getTimestamp();
92+
}
93+
94+
public function getTitleName(string $title): string
95+
{
96+
return $title . ' ' . $this->name;
97+
}
98+
99+
public function setTitleName(string $name, string $suffix): void
100+
{
101+
$this->name = $name . ' ' . $suffix;
102+
}
103+
}

benchmark/UserModel.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ class UserModel
88
protected $name;
99
/** @var string */
1010
protected $login;
11-
/** @var \DateTime */
11+
/** @var DateTime */
1212
protected $creAt;
1313
/** @var bool */
1414
protected $active;
15-
/** @var UserModel|null */
16-
protected $childModel;
15+
/** @var ChildModel|null */
16+
protected $child;
1717

1818
public function __construct()
1919
{
20-
$this->creAt = new \DateTime;
20+
$this->creAt = new DateTime;
2121
}
2222

2323
public function __toString(): string
@@ -68,12 +68,12 @@ public function setLogin(string $login): void
6868
$this->login = $login;
6969
}
7070

71-
public function getCreAt(): \DateTime
71+
public function getCreAt(): DateTime
7272
{
7373
return $this->creAt;
7474
}
7575

76-
public function setCreAt(\DateTime $creAt): void
76+
public function setCreAt(DateTime $creAt): void
7777
{
7878
$this->creAt = $creAt;
7979
}

benchmark/bechmark-collections.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,27 @@
77

88
require_once __DIR__ .'/../vendor/autoload.php';
99
require_once 'UserModel.php';
10+
require_once 'ChildModel.php';
1011
$faker = Faker::create();
1112

1213
$iterations = $argv[1] ?? 1000;
1314
$blackfire = $argv[2] ?? false;
1415
$iterations++;
1516

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

1921
$collectionDto = [];
2022
while ($iterations--) {
2123
$collectionDto[] = [
2224
'id' => $faker->randomDigit,
2325
'creAt' => $faker->unixTime(),
24-
'name' => $faker->name,
25-
'login' => $faker->name,
26+
'name' => $faker->name(),
27+
'login' => $faker->name(),
2628
'active' => $faker->numberBetween(0, 2),
2729
'email' => $faker->email,
28-
'childModel' => [
30+
'child' => [
2931
'id' => $faker->randomDigit,
3032
'creAt' => time(),
3133
'name' => $faker->unixTime(),
@@ -46,8 +48,8 @@
4648
$collectionDto = $service->toDtoCollection($models);
4749

4850
$diff = (microtime(true) - $startTime) * 1000;
49-
echo sprintf('%2.3f ms', $diff);
50-
echo "\n" . memory_get_peak_usage()/1024;
51+
echo PHP_EOL . 'transformer: ' . sprintf('%2.3f ms', $diff);
52+
echo PHP_EOL . memory_get_peak_usage()/1024;
5153

5254
if ($blackfire) {
5355
$client->endProbe($probe);

benchmark/bechmark.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77

88
require_once __DIR__ .'/../vendor/autoload.php';
99
require_once 'UserModel.php';
10+
require_once 'ChildModel.php';
1011
$faker = Faker::create();
1112

1213
$iterations = $argv[1] ?? 1000;
1314
$blackfire = $argv[2] ?? false;
1415
$iterations++;
1516

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

1921
$collectionDto = [];
2022
while ($iterations--) {
@@ -25,7 +27,7 @@
2527
'login' => $faker->name,
2628
'active' => $faker->numberBetween(0, 2),
2729
'email' => $faker->email,
28-
'childModel' => [
30+
'child' => [
2931
'id' => $faker->randomDigit,
3032
'creAt' => time(),
3133
'name' => $faker->unixTime(),

benchmark/maps/ChildModel/dto.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
return [
5+
'id' => [],
6+
'creAt' => [],
7+
'name' => [],
8+
'login' => [],
9+
'active' => [],
10+
'email' => [],
11+
];

benchmark/dto.php benchmark/maps/UserModel/dto.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
]
2121
]
2222
],
23-
'childModel' => [
23+
'child' => [
2424
'ref' => [
25-
'model' => 'UserModel',
25+
'model' => 'ChildModel',
2626
'map' => 'dto'
2727
]
2828
]

composer.lock

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DataTransformer.php

+15-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace PTS\DataTransformer;
44

55
use PTS\Hydrator\HydratorService;
6+
use function get_class;
7+
use function is_callable;
68

79
class DataTransformer implements DataTransformerInterface
810
{
@@ -28,8 +30,8 @@ public function getMapsManager(): MapsManager
2830
public function toModel(string $class, array $dto, string $mapName = 'dto'): object
2931
{
3032
$map = $this->mapsManager->getMap($class, $mapName);
31-
$dto = $this->resolveRefPopulate($dto, $map['refs']);
32-
$dto = $this->applyPipes($dto, $map['pipe']);
33+
$dto = $map['refs'] ? $this->resolveRefPopulate($dto, $map['refs']) : $dto;
34+
$dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe']) : $dto;
3335
return $this->hydratorService->hydrate($dto, $class, $map['rules']);
3436
}
3537

@@ -39,8 +41,8 @@ public function toModelsCollection(string $class, array $dtoCollection, string $
3941

4042
$models = [];
4143
foreach ($dtoCollection as $dto) {
42-
$dto = $this->resolveRefPopulate($dto, $map['refs']);
43-
$dto = $this->applyPipes($dto, $map['pipe']);
44+
$dto = $map['refs'] ? $this->resolveRefPopulate($dto, $map['refs']) : $dto;
45+
$dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe']) : $dto;
4446
$models[] = $this->hydratorService->hydrate($dto, $class, $map['rules']);
4547
}
4648

@@ -49,9 +51,9 @@ public function toModelsCollection(string $class, array $dtoCollection, string $
4951

5052
public function fillModel(object $model, array $dto, string $mapName = 'dto'): object
5153
{
52-
$map = $this->mapsManager->getMap(\get_class($model), $mapName);
53-
$dto = $this->resolveRefPopulate($dto, $map['refs']);
54-
$dto = $this->applyPipes($dto, $map['pipe']);
54+
$map = $this->mapsManager->getMap(get_class($model), $mapName);
55+
$dto = $map['refs'] ? $this->resolveRefPopulate($dto, $map['refs']) : $dto;
56+
$dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe']) : $dto;
5557
$this->hydratorService->hydrateModel($dto, $model, $map['rules']);
5658

5759
return $model;
@@ -70,16 +72,16 @@ public function toDtoCollection(array $models, string $mapName = 'dto', array $o
7072

7173
public function toDTO(object $model, string $mapName = 'dto', array $options = []): array
7274
{
73-
$map = $this->mapsManager->getMap(\get_class($model), $mapName);
75+
$map = $this->mapsManager->getMap(get_class($model), $mapName);
7476
$excludeRules = $options['excludeFields'] ?? [];
7577

7678
foreach ($excludeRules as $name) {
7779
unset($map['pipe'][$name], $map['rules'][$name], $map['refs'][$name]);
7880
}
7981

8082
$dto = $this->hydratorService->extract($model, $map['rules']);
81-
$dto = $this->applyPipes($dto, $map['pipe'], self::FILTER_TYPE_EXTRACT);
82-
return $this->resolveRefExtract($dto, $map['refs']);
83+
$dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe'], self::FILTER_TYPE_EXTRACT) : $dto;
84+
return $map['refs'] ? $this->resolveRefExtract($dto, $map['refs']) : $dto;
8385
}
8486

8587
protected function resolveRefExtract(array $dto, array $refsRules): array
@@ -116,7 +118,7 @@ protected function resolveRefPopulate(array $dto, array $refsRules): array
116118
return $dto;
117119
}
118120

119-
protected function applyPipes(array $dto, array $pipes, $type = self::FILTER_TYPE_POPULATE): array
121+
protected function applyPipes(array $dto, array $pipes, string $type = self::FILTER_TYPE_POPULATE): array
120122
{
121123
$fieldsPipes = array_intersect_key($pipes, $dto);
122124
foreach ($fieldsPipes as $name => $filters) {
@@ -127,10 +129,10 @@ protected function applyPipes(array $dto, array $pipes, $type = self::FILTER_TYP
127129
return $dto;
128130
}
129131

130-
protected function applyFilters($value, array $filters, $type)
132+
protected function applyFilters($value, array $filters, string $type)
131133
{
132134
foreach ($filters as $filter) {
133-
if (\is_callable($filter)) {
135+
if (is_callable($filter)) {
134136
$value = $filter($value);
135137
continue;
136138
}

0 commit comments

Comments
 (0)