Skip to content

Commit b6e1d63

Browse files
committed
#2 улучшение подсветки в IDE возвращаемого значения
1 parent 424d4fb commit b6e1d63

File tree

6 files changed

+73
-36
lines changed

6 files changed

+73
-36
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PHPSTORM_META {
4+
5+
override(\PTS\DataTransformer\DataTransformerInterface::toModel(0), map([
6+
"" => "@",
7+
]));
8+
}

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
$dataTransformer = new DataTransformer;
2929
$dataTransformer->getMapsManager()->setMapDir(UserModel::class, __DIR__ . '/data');
3030

31-
$model = $dataTransformer->toModel([
31+
$model = $dataTransformer->toModel(UserModel::class, [
3232
'id' => 1,
3333
'creAt' => new \DateTime,
3434
'name' => 'Alex',
3535
'active' => 1,
36-
], UserModel::class);
36+
]);
3737

3838
$dto = $dataTransformer->toDTO($model);
3939
```
@@ -82,7 +82,7 @@ return [
8282
];
8383

8484
// code file
85-
$model = $dataTransformer->toModel([
85+
$model = $dataTransformer->toModel(UserModel::class, [
8686
'id' => 1,
8787
'creAt' => new \DateTime,
8888
'name' => 'Alex',
@@ -91,9 +91,9 @@ $model = $dataTransformer->toModel([
9191
'id' => 2,
9292
'name' => 'refModel',
9393
]
94-
], UserModel::class, 'deepDto');
94+
], 'deepDto');
9595

96-
$model2 = $dataTransformer->toModel([
96+
$model2 = $dataTransformer->toModel(UserModel::class, [
9797
'id' => 1,
9898
'creAt' => new \DateTime,
9999
'name' => 'Alex',
@@ -108,5 +108,5 @@ $model2 = $dataTransformer->toModel([
108108
'name' => 'refModel',
109109
]
110110
]
111-
], UserModel::class, 'deepDto');
111+
], 'deepDto');
112112
```

src/DataTransformer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public function __construct(HydratorService $hydratorService = null, MapsManager
1717
$this->mapsManager = $mapsManager ?? new MapsManager;
1818
}
1919

20-
public function toModel(array $dto, string $class, string $mapName = 'dto')
20+
public function toModel(string $class, array $dto, string $mapName = 'dto'): object
2121
{
2222
$rules = $this->mapsManager->getMap($class, $mapName);
2323
$dto = $this->resolveRefHydrate($dto, $rules);
2424
return $this->hydratorService->hydrate($dto, $class, $rules);
2525
}
2626

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

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

40-
public function fillModel(array $dto, object $model, string $mapName = 'dto'): void
40+
public function fillModel(object $model, array $dto, string $mapName = 'dto'): object
4141
{
4242
$rules = $this->mapsManager->getMap(\get_class($model), $mapName);
4343
$dto = $this->resolveRefHydrate($dto, $rules);
4444
$this->hydratorService->hydrateModel($dto, $model, $rules);
45+
46+
return $model;
4547
}
4648

4749
protected function resolveRefHydrate(array $dto, array $rules): array

src/DataTransformerInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ interface DataTransformerInterface
88
public function toDTO(object $model, string $mapType = 'dto', array $excludeFields = []): array;
99
public function toDtoCollection(array $models, string $mapName = 'dto', array $excludeFields = []): array;
1010

11-
public function toModel(array $dto, string $model, string $mapType = 'dto');
12-
public function toModelsCollection(array $dtoCollection, string $model, string $mapType = 'dto'): array;
11+
public function toModel(string $model, array $dto, string $mapType = 'dto'): object;
12+
public function toModelsCollection(string $model, array $dtoCollection, string $mapType = 'dto'): array;
1313

14-
public function fillModel(array $data, object $model, string $mapType = 'dto');
14+
public function fillModel(object $model, array $data, string $mapType = 'dto'): object;
1515
}

test/unit/DataTransformerTest.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,36 @@ public function testGetMapsManager(): void
5050
$this->assertInstanceOf(MapsManager::class, $mapsManager);
5151
}
5252

53+
/**
54+
* @throws Exception
55+
*/
5356
public function testToModel(): void
5457
{
55-
/** @var UserModel $model */
56-
$model = $this->dataTransformer->toModel([
58+
$model = $this->dataTransformer->toModel(UserModel::class, [
5759
'id' => 1,
58-
'creAt' => new \DateTime,
60+
'creAt' => new DateTime,
5961
'name' => 'Alex',
6062
'active' => 1,
61-
], UserModel::class);
63+
]);
6264

6365
$this->assertInstanceOf(UserModel::class, $model);
6466
$this->assertEquals(true, $model->isActive());
6567
$this->assertEquals(1, $model->getId());
6668
$this->assertEquals('Alex', $model->getName());
6769
}
6870

71+
/**
72+
* @throws Exception
73+
*/
6974
public function testFillModel(): void
7075
{
7176
$model = new UserModel;
72-
$this->dataTransformer->fillModel([
77+
$model = $this->dataTransformer->fillModel($model, [
7378
'id' => 1,
74-
'creAt' => new \DateTime,
79+
'creAt' => new DateTime,
7580
'name' => 'Alex',
7681
'active' => 1,
77-
], $model);
82+
]);
7883

7984
$this->assertInstanceOf(UserModel::class, $model);
8085
$this->assertEquals(true, $model->isActive());
@@ -145,25 +150,28 @@ public function testToDtoCollection(): void
145150
], $dtoCollection[1]);
146151
}
147152

153+
/**
154+
* @throws Exception
155+
*/
148156
public function testToModelsCollection(): void
149157
{
150158
$dtoCollection = [
151159
[
152160
'id' => 1,
153-
'creAt' => new \DateTime,
161+
'creAt' => new DateTime,
154162
'name' => 'Alex',
155163
'active' => true,
156164
],
157165
[
158166
'id' => 2,
159-
'creAt' => new \DateTime,
167+
'creAt' => new DateTime,
160168
'name' => 'Bob',
161169
'active' => false,
162170
]
163171
];
164172

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

169177
$this->assertInstanceOf(UserModel::class, $models[0]);

test/unit/RefTest.php

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function setUp(): void
3636
$this->faker = \Faker\Factory::create();
3737
}
3838

39+
/**
40+
* @return UserModel
41+
* @throws Exception
42+
*/
3943
protected function createUser(): UserModel
4044
{
4145
$user = new UserModel;
@@ -48,58 +52,67 @@ protected function createUser(): UserModel
4852
return $user;
4953
}
5054

55+
/**
56+
* @throws Exception
57+
*/
5158
public function testFillModelRefModel(): void
5259
{
5360
$model = new UserModel;
54-
$this->dataTransformer->fillModel([
61+
$this->dataTransformer->fillModel($model, [
5562
'id' => 1,
5663
'name' => 'Alex',
5764
'refModel' => [
5865
'id' => 2,
59-
'creAt' => new \DateTime,
66+
'creAt' => new DateTime,
6067
'name' => 'Alex2',
6168
'active' => true,
6269
]
63-
], $model, 'withRefs.map');
70+
], 'withRefs.map');
6471

6572
$this->assertInstanceOf(UserModel::class, $model);
6673
$this->assertInstanceOf(UserModel::class, $model->refModel);
6774
$this->assertEquals(2, $model->refModel->getId());
6875
}
6976

77+
/**
78+
* @throws Exception
79+
*/
7080
public function testFillModelRefModelWithExtraFields(): void
7181
{
7282
$model = new UserModel;
73-
$this->dataTransformer->fillModel([
83+
$this->dataTransformer->fillModel($model, [
7484
'id' => 1,
7585
'name' => 'Alex',
7686
'extraFieldWithoutDeclarateTransform' => 3,
7787
'refModel' => [
7888
'id' => 2,
79-
'creAt' => new \DateTime,
89+
'creAt' => new DateTime,
8090
'name' => 'Alex2',
8191
'active' => true,
8292
]
83-
], $model, 'withRefs.map');
93+
], 'withRefs.map');
8494

8595
$this->assertInstanceOf(UserModel::class, $model);
8696
$this->assertInstanceOf(UserModel::class, $model->refModel);
8797
$this->assertEquals(2, $model->refModel->getId());
8898
}
8999

100+
/**
101+
* @throws Exception
102+
*/
90103
public function testToModelRefModel(): void
91104
{
92105
/** @var UserModel $model */
93-
$model = $this->dataTransformer->toModel([
106+
$model = $this->dataTransformer->toModel(UserModel::class, [
94107
'id' => 1,
95108
'name' => 'Alex',
96109
'refModel' => [
97110
'id' => 2,
98-
'creAt' => new \DateTime,
111+
'creAt' => new DateTime,
99112
'name' => 'Alex2',
100113
'active' => true,
101114
]
102-
], UserModel::class, 'withRefs.map');
115+
], 'withRefs.map');
103116

104117
$this->assertInstanceOf(UserModel::class, $model);
105118
$this->assertInstanceOf(UserModel::class, $model->refModel);
@@ -109,7 +122,7 @@ public function testToModelRefModel(): void
109122
public function testToModelRefModels(): void
110123
{
111124
/** @var UserModel $model */
112-
$model = $this->dataTransformer->toModel([
125+
$model = $this->dataTransformer->toModel(UserModel::class, [
113126
'id' => 1,
114127
'name' => 'Alex',
115128
'refModels' => [
@@ -122,7 +135,7 @@ public function testToModelRefModels(): void
122135
'name' => 'Alex3',
123136
]
124137
]
125-
], UserModel::class, 'withRefs.map');
138+
], 'withRefs.map');
126139

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

134-
public function testToDtoRefModel()
147+
/**
148+
* @throws Exception
149+
*/
150+
public function testToDtoRefModel(): void
135151
{
136152
$model = $this->createUser();
137153
$model2= $this->createUser();
@@ -149,7 +165,10 @@ public function testToDtoRefModel()
149165
]);
150166
}
151167

152-
public function testToDtoRefModels()
168+
/**
169+
* @throws Exception
170+
*/
171+
public function testToDtoRefModels(): void
153172
{
154173
$model = $this->createUser();
155174
$model2= $this->createUser();
@@ -178,4 +197,4 @@ public function testToDtoRefModels()
178197
]
179198
]);
180199
}
181-
}
200+
}

0 commit comments

Comments
 (0)