diff --git a/benchmark/ChildModel.php b/benchmark/ChildModel.php new file mode 100644 index 0000000..fbdaf04 --- /dev/null +++ b/benchmark/ChildModel.php @@ -0,0 +1,103 @@ +creAt = new DateTime; + } + + public function __toString(): string + { + return (string)$this->id; + } + + public function getEmail(): string + { + return $this->email; + } + + public function setEmail(string $email): void + { + $this->email = $email; + } + + /** @var string */ + protected $email; + + public function getId() + { + return $this->id; + } + + public function setId($id): void + { + $this->id = $id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): void + { + $this->name = $name; + } + + public function getLogin(): ?string + { + return $this->login; + } + + public function setLogin(string $login): void + { + $this->login = $login; + } + + public function getCreAt(): DateTime + { + return $this->creAt; + } + + public function setCreAt(DateTime $creAt): void + { + $this->creAt = $creAt; + } + + public function isActive(): bool + { + return $this->active; + } + + public function setActive(bool $active): void + { + $this->active = $active; + } + + public function getCreAtTimestamp(): int + { + return $this->creAt->getTimestamp(); + } + + public function getTitleName(string $title): string + { + return $title . ' ' . $this->name; + } + + public function setTitleName(string $name, string $suffix): void + { + $this->name = $name . ' ' . $suffix; + } +} diff --git a/benchmark/UserModel.php b/benchmark/UserModel.php index 663f151..4b83214 100644 --- a/benchmark/UserModel.php +++ b/benchmark/UserModel.php @@ -8,16 +8,16 @@ class UserModel protected $name; /** @var string */ protected $login; - /** @var \DateTime */ + /** @var DateTime */ protected $creAt; /** @var bool */ protected $active; - /** @var UserModel|null */ - protected $childModel; + /** @var ChildModel|null */ + protected $child; public function __construct() { - $this->creAt = new \DateTime; + $this->creAt = new DateTime; } public function __toString(): string @@ -68,12 +68,12 @@ public function setLogin(string $login): void $this->login = $login; } - public function getCreAt(): \DateTime + public function getCreAt(): DateTime { return $this->creAt; } - public function setCreAt(\DateTime $creAt): void + public function setCreAt(DateTime $creAt): void { $this->creAt = $creAt; } diff --git a/benchmark/bechmark-collections.php b/benchmark/bechmark-collections.php index 3e45ccf..c88c2f0 100644 --- a/benchmark/bechmark-collections.php +++ b/benchmark/bechmark-collections.php @@ -7,6 +7,7 @@ require_once __DIR__ .'/../vendor/autoload.php'; require_once 'UserModel.php'; +require_once 'ChildModel.php'; $faker = Faker::create(); $iterations = $argv[1] ?? 1000; @@ -14,18 +15,19 @@ $iterations++; $service = new DataTransformer; -$service->getMapsManager()->setMapDir(UserModel::class, __DIR__); +$service->getMapsManager()->setMapDir(UserModel::class, __DIR__ . '/maps/' . UserModel::class); +$service->getMapsManager()->setMapDir(ChildModel::class, __DIR__ . '/maps/' . ChildModel::class); $collectionDto = []; while ($iterations--) { $collectionDto[] = [ 'id' => $faker->randomDigit, 'creAt' => $faker->unixTime(), - 'name' => $faker->name, - 'login' => $faker->name, + 'name' => $faker->name(), + 'login' => $faker->name(), 'active' => $faker->numberBetween(0, 2), 'email' => $faker->email, - 'childModel' => [ + 'child' => [ 'id' => $faker->randomDigit, 'creAt' => time(), 'name' => $faker->unixTime(), @@ -46,8 +48,8 @@ $collectionDto = $service->toDtoCollection($models); $diff = (microtime(true) - $startTime) * 1000; -echo sprintf('%2.3f ms', $diff); -echo "\n" . memory_get_peak_usage()/1024; +echo PHP_EOL . 'transformer: ' . sprintf('%2.3f ms', $diff); +echo PHP_EOL . memory_get_peak_usage()/1024; if ($blackfire) { $client->endProbe($probe); diff --git a/benchmark/bechmark.php b/benchmark/bechmark.php index 05df582..28447c9 100644 --- a/benchmark/bechmark.php +++ b/benchmark/bechmark.php @@ -7,6 +7,7 @@ require_once __DIR__ .'/../vendor/autoload.php'; require_once 'UserModel.php'; +require_once 'ChildModel.php'; $faker = Faker::create(); $iterations = $argv[1] ?? 1000; @@ -14,7 +15,8 @@ $iterations++; $service = new DataTransformer; -$service->getMapsManager()->setMapDir(UserModel::class, __DIR__); +$service->getMapsManager()->setMapDir(UserModel::class, __DIR__ . '/maps/' . UserModel::class); +$service->getMapsManager()->setMapDir(ChildModel::class, __DIR__ . '/maps/' . ChildModel::class); $collectionDto = []; while ($iterations--) { @@ -25,7 +27,7 @@ 'login' => $faker->name, 'active' => $faker->numberBetween(0, 2), 'email' => $faker->email, - 'childModel' => [ + 'child' => [ 'id' => $faker->randomDigit, 'creAt' => time(), 'name' => $faker->unixTime(), diff --git a/benchmark/maps/ChildModel/dto.php b/benchmark/maps/ChildModel/dto.php new file mode 100644 index 0000000..162a986 --- /dev/null +++ b/benchmark/maps/ChildModel/dto.php @@ -0,0 +1,11 @@ + [], + 'creAt' => [], + 'name' => [], + 'login' => [], + 'active' => [], + 'email' => [], +]; diff --git a/benchmark/dto.php b/benchmark/maps/UserModel/dto.php similarity index 88% rename from benchmark/dto.php rename to benchmark/maps/UserModel/dto.php index 996e117..944b062 100644 --- a/benchmark/dto.php +++ b/benchmark/maps/UserModel/dto.php @@ -20,9 +20,9 @@ ] ] ], - 'childModel' => [ + 'child' => [ 'ref' => [ - 'model' => 'UserModel', + 'model' => 'ChildModel', 'map' => 'dto' ] ] diff --git a/composer.lock b/composer.lock index be91adb..7446dab 100644 --- a/composer.lock +++ b/composer.lock @@ -8,25 +8,25 @@ "packages": [ { "name": "alexpts/php-hydrator", - "version": "3.0.0", + "version": "3.0.2", "source": { "type": "git", "url": "https://github.com/alexpts/php-hydrator.git", - "reference": "f63b1da8ebd4ac313d2275da2a3f0ea1de0e334b" + "reference": "755844f243720af2532f8fdde88cfdd852e49165" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/alexpts/php-hydrator/zipball/f63b1da8ebd4ac313d2275da2a3f0ea1de0e334b", - "reference": "f63b1da8ebd4ac313d2275da2a3f0ea1de0e334b", + "url": "https://api.github.com/repos/alexpts/php-hydrator/zipball/755844f243720af2532f8fdde88cfdd852e49165", + "reference": "755844f243720af2532f8fdde88cfdd852e49165", "shasum": "" }, "require": { "php": "^7.2" }, "require-dev": { - "blackfire/php-sdk": "^1.17", - "fzaninotto/faker": "^1.8", - "phpunit/phpunit": "^7.5" + "blackfire/php-sdk": "^1.21", + "fzaninotto/faker": "^1.9", + "phpunit/phpunit": "^8.5" }, "type": "library", "autoload": { @@ -54,7 +54,7 @@ "model", "transform" ], - "time": "2019-01-16T19:56:08+00:00" + "time": "2020-02-16T09:33:31+00:00" } ], "packages-dev": [ diff --git a/src/DataTransformer.php b/src/DataTransformer.php index 6b6e54f..a735b6d 100644 --- a/src/DataTransformer.php +++ b/src/DataTransformer.php @@ -3,6 +3,8 @@ namespace PTS\DataTransformer; use PTS\Hydrator\HydratorService; +use function get_class; +use function is_callable; class DataTransformer implements DataTransformerInterface { @@ -28,8 +30,8 @@ public function getMapsManager(): MapsManager public function toModel(string $class, array $dto, string $mapName = 'dto'): object { $map = $this->mapsManager->getMap($class, $mapName); - $dto = $this->resolveRefPopulate($dto, $map['refs']); - $dto = $this->applyPipes($dto, $map['pipe']); + $dto = $map['refs'] ? $this->resolveRefPopulate($dto, $map['refs']) : $dto; + $dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe']) : $dto; return $this->hydratorService->hydrate($dto, $class, $map['rules']); } @@ -39,8 +41,8 @@ public function toModelsCollection(string $class, array $dtoCollection, string $ $models = []; foreach ($dtoCollection as $dto) { - $dto = $this->resolveRefPopulate($dto, $map['refs']); - $dto = $this->applyPipes($dto, $map['pipe']); + $dto = $map['refs'] ? $this->resolveRefPopulate($dto, $map['refs']) : $dto; + $dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe']) : $dto; $models[] = $this->hydratorService->hydrate($dto, $class, $map['rules']); } @@ -49,9 +51,9 @@ public function toModelsCollection(string $class, array $dtoCollection, string $ public function fillModel(object $model, array $dto, string $mapName = 'dto'): object { - $map = $this->mapsManager->getMap(\get_class($model), $mapName); - $dto = $this->resolveRefPopulate($dto, $map['refs']); - $dto = $this->applyPipes($dto, $map['pipe']); + $map = $this->mapsManager->getMap(get_class($model), $mapName); + $dto = $map['refs'] ? $this->resolveRefPopulate($dto, $map['refs']) : $dto; + $dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe']) : $dto; $this->hydratorService->hydrateModel($dto, $model, $map['rules']); return $model; @@ -70,7 +72,7 @@ public function toDtoCollection(array $models, string $mapName = 'dto', array $o public function toDTO(object $model, string $mapName = 'dto', array $options = []): array { - $map = $this->mapsManager->getMap(\get_class($model), $mapName); + $map = $this->mapsManager->getMap(get_class($model), $mapName); $excludeRules = $options['excludeFields'] ?? []; foreach ($excludeRules as $name) { @@ -78,8 +80,8 @@ public function toDTO(object $model, string $mapName = 'dto', array $options = [ } $dto = $this->hydratorService->extract($model, $map['rules']); - $dto = $this->applyPipes($dto, $map['pipe'], self::FILTER_TYPE_EXTRACT); - return $this->resolveRefExtract($dto, $map['refs']); + $dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe'], self::FILTER_TYPE_EXTRACT) : $dto; + return $map['refs'] ? $this->resolveRefExtract($dto, $map['refs']) : $dto; } protected function resolveRefExtract(array $dto, array $refsRules): array @@ -116,7 +118,7 @@ protected function resolveRefPopulate(array $dto, array $refsRules): array return $dto; } - protected function applyPipes(array $dto, array $pipes, $type = self::FILTER_TYPE_POPULATE): array + protected function applyPipes(array $dto, array $pipes, string $type = self::FILTER_TYPE_POPULATE): array { $fieldsPipes = array_intersect_key($pipes, $dto); foreach ($fieldsPipes as $name => $filters) { @@ -127,10 +129,10 @@ protected function applyPipes(array $dto, array $pipes, $type = self::FILTER_TYP return $dto; } - protected function applyFilters($value, array $filters, $type) + protected function applyFilters($value, array $filters, string $type) { foreach ($filters as $filter) { - if (\is_callable($filter)) { + if (is_callable($filter)) { $value = $filter($value); continue; }