Skip to content

Commit

Permalink
#2 улучшение подсветки в IDE возвращаемого значения
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpts committed Jan 7, 2019
1 parent 424d4fb commit b6e1d63
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 36 deletions.
8 changes: 8 additions & 0 deletions .phpstorm.meta.php/autocomplete.meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace PHPSTORM_META {

override(\PTS\DataTransformer\DataTransformerInterface::toModel(0), map([
"" => "@",
]));
}
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
$dataTransformer = new DataTransformer;
$dataTransformer->getMapsManager()->setMapDir(UserModel::class, __DIR__ . '/data');

$model = $dataTransformer->toModel([
$model = $dataTransformer->toModel(UserModel::class, [
'id' => 1,
'creAt' => new \DateTime,
'name' => 'Alex',
'active' => 1,
], UserModel::class);
]);

$dto = $dataTransformer->toDTO($model);
```
Expand Down Expand Up @@ -82,7 +82,7 @@ return [
];

// code file
$model = $dataTransformer->toModel([
$model = $dataTransformer->toModel(UserModel::class, [
'id' => 1,
'creAt' => new \DateTime,
'name' => 'Alex',
Expand All @@ -91,9 +91,9 @@ $model = $dataTransformer->toModel([
'id' => 2,
'name' => 'refModel',
]
], UserModel::class, 'deepDto');
], 'deepDto');

$model2 = $dataTransformer->toModel([
$model2 = $dataTransformer->toModel(UserModel::class, [
'id' => 1,
'creAt' => new \DateTime,
'name' => 'Alex',
Expand All @@ -108,5 +108,5 @@ $model2 = $dataTransformer->toModel([
'name' => 'refModel',
]
]
], UserModel::class, 'deepDto');
], 'deepDto');
```
8 changes: 5 additions & 3 deletions src/DataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public function __construct(HydratorService $hydratorService = null, MapsManager
$this->mapsManager = $mapsManager ?? new MapsManager;
}

public function toModel(array $dto, string $class, string $mapName = 'dto')
public function toModel(string $class, array $dto, string $mapName = 'dto'): object
{
$rules = $this->mapsManager->getMap($class, $mapName);
$dto = $this->resolveRefHydrate($dto, $rules);
return $this->hydratorService->hydrate($dto, $class, $rules);
}

public function toModelsCollection(array $dtoCollection, string $class, string $mapName = 'dto'): array
public function toModelsCollection(string $class, array $dtoCollection, string $mapName = 'dto'): array
{
$rules = $this->mapsManager->getMap($class, $mapName);

Expand All @@ -37,11 +37,13 @@ public function toModelsCollection(array $dtoCollection, string $class, string $
return $models;
}

public function fillModel(array $dto, object $model, string $mapName = 'dto'): void
public function fillModel(object $model, array $dto, string $mapName = 'dto'): object
{
$rules = $this->mapsManager->getMap(\get_class($model), $mapName);
$dto = $this->resolveRefHydrate($dto, $rules);
$this->hydratorService->hydrateModel($dto, $model, $rules);

return $model;
}

protected function resolveRefHydrate(array $dto, array $rules): array
Expand Down
6 changes: 3 additions & 3 deletions src/DataTransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ interface DataTransformerInterface
public function toDTO(object $model, string $mapType = 'dto', array $excludeFields = []): array;
public function toDtoCollection(array $models, string $mapName = 'dto', array $excludeFields = []): array;

public function toModel(array $dto, string $model, string $mapType = 'dto');
public function toModelsCollection(array $dtoCollection, string $model, string $mapType = 'dto'): array;
public function toModel(string $model, array $dto, string $mapType = 'dto'): object;
public function toModelsCollection(string $model, array $dtoCollection, string $mapType = 'dto'): array;

public function fillModel(array $data, object $model, string $mapType = 'dto');
public function fillModel(object $model, array $data, string $mapType = 'dto'): object;
}
28 changes: 18 additions & 10 deletions test/unit/DataTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,36 @@ public function testGetMapsManager(): void
$this->assertInstanceOf(MapsManager::class, $mapsManager);
}

/**
* @throws Exception
*/
public function testToModel(): void
{
/** @var UserModel $model */
$model = $this->dataTransformer->toModel([
$model = $this->dataTransformer->toModel(UserModel::class, [
'id' => 1,
'creAt' => new \DateTime,
'creAt' => new DateTime,
'name' => 'Alex',
'active' => 1,
], UserModel::class);
]);

$this->assertInstanceOf(UserModel::class, $model);
$this->assertEquals(true, $model->isActive());
$this->assertEquals(1, $model->getId());
$this->assertEquals('Alex', $model->getName());
}

/**
* @throws Exception
*/
public function testFillModel(): void
{
$model = new UserModel;
$this->dataTransformer->fillModel([
$model = $this->dataTransformer->fillModel($model, [
'id' => 1,
'creAt' => new \DateTime,
'creAt' => new DateTime,
'name' => 'Alex',
'active' => 1,
], $model);
]);

$this->assertInstanceOf(UserModel::class, $model);
$this->assertEquals(true, $model->isActive());
Expand Down Expand Up @@ -145,25 +150,28 @@ public function testToDtoCollection(): void
], $dtoCollection[1]);
}

/**
* @throws Exception
*/
public function testToModelsCollection(): void
{
$dtoCollection = [
[
'id' => 1,
'creAt' => new \DateTime,
'creAt' => new DateTime,
'name' => 'Alex',
'active' => true,
],
[
'id' => 2,
'creAt' => new \DateTime,
'creAt' => new DateTime,
'name' => 'Bob',
'active' => false,
]
];

/** @var UserModel[] $models */
$models = $this->dataTransformer->toModelsCollection($dtoCollection, UserModel::class);
$models = $this->dataTransformer->toModelsCollection(UserModel::class, $dtoCollection);
$this->assertCount(2, $models);

$this->assertInstanceOf(UserModel::class, $models[0]);
Expand Down
47 changes: 33 additions & 14 deletions test/unit/RefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function setUp(): void
$this->faker = \Faker\Factory::create();
}

/**
* @return UserModel
* @throws Exception
*/
protected function createUser(): UserModel
{
$user = new UserModel;
Expand All @@ -48,58 +52,67 @@ protected function createUser(): UserModel
return $user;
}

/**
* @throws Exception
*/
public function testFillModelRefModel(): void
{
$model = new UserModel;
$this->dataTransformer->fillModel([
$this->dataTransformer->fillModel($model, [
'id' => 1,
'name' => 'Alex',
'refModel' => [
'id' => 2,
'creAt' => new \DateTime,
'creAt' => new DateTime,
'name' => 'Alex2',
'active' => true,
]
], $model, 'withRefs.map');
], 'withRefs.map');

$this->assertInstanceOf(UserModel::class, $model);
$this->assertInstanceOf(UserModel::class, $model->refModel);
$this->assertEquals(2, $model->refModel->getId());
}

/**
* @throws Exception
*/
public function testFillModelRefModelWithExtraFields(): void
{
$model = new UserModel;
$this->dataTransformer->fillModel([
$this->dataTransformer->fillModel($model, [
'id' => 1,
'name' => 'Alex',
'extraFieldWithoutDeclarateTransform' => 3,
'refModel' => [
'id' => 2,
'creAt' => new \DateTime,
'creAt' => new DateTime,
'name' => 'Alex2',
'active' => true,
]
], $model, 'withRefs.map');
], 'withRefs.map');

$this->assertInstanceOf(UserModel::class, $model);
$this->assertInstanceOf(UserModel::class, $model->refModel);
$this->assertEquals(2, $model->refModel->getId());
}

/**
* @throws Exception
*/
public function testToModelRefModel(): void
{
/** @var UserModel $model */
$model = $this->dataTransformer->toModel([
$model = $this->dataTransformer->toModel(UserModel::class, [
'id' => 1,
'name' => 'Alex',
'refModel' => [
'id' => 2,
'creAt' => new \DateTime,
'creAt' => new DateTime,
'name' => 'Alex2',
'active' => true,
]
], UserModel::class, 'withRefs.map');
], 'withRefs.map');

$this->assertInstanceOf(UserModel::class, $model);
$this->assertInstanceOf(UserModel::class, $model->refModel);
Expand All @@ -109,7 +122,7 @@ public function testToModelRefModel(): void
public function testToModelRefModels(): void
{
/** @var UserModel $model */
$model = $this->dataTransformer->toModel([
$model = $this->dataTransformer->toModel(UserModel::class, [
'id' => 1,
'name' => 'Alex',
'refModels' => [
Expand All @@ -122,7 +135,7 @@ public function testToModelRefModels(): void
'name' => 'Alex3',
]
]
], UserModel::class, 'withRefs.map');
], 'withRefs.map');

$this->assertCount(2, $model->refModels);
$this->assertInstanceOf(UserModel::class, $model->refModels[0]);
Expand All @@ -131,7 +144,10 @@ public function testToModelRefModels(): void
$this->assertEquals(3, $model->refModels[1]->getId());
}

public function testToDtoRefModel()
/**
* @throws Exception
*/
public function testToDtoRefModel(): void
{
$model = $this->createUser();
$model2= $this->createUser();
Expand All @@ -149,7 +165,10 @@ public function testToDtoRefModel()
]);
}

public function testToDtoRefModels()
/**
* @throws Exception
*/
public function testToDtoRefModels(): void
{
$model = $this->createUser();
$model2= $this->createUser();
Expand Down Expand Up @@ -178,4 +197,4 @@ public function testToDtoRefModels()
]
]);
}
}
}

0 comments on commit b6e1d63

Please sign in to comment.