Skip to content

Commit cdedb55

Browse files
committed
fix: code fixed
1 parent 4e8aecb commit cdedb55

File tree

13 files changed

+106
-65
lines changed

13 files changed

+106
-65
lines changed

src/Blueprint/Application/Model/Assets/PropertyBlueprint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class PropertyBlueprint implements Normalizable
1212
{
1313
/** @var array<string, mixed> */
1414
public array $options = [];
15-
15+
1616
public ClassBlueprint $parent;
1717
public string $originName;
1818

src/Build/Application/Contract/BuilderInterface.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
interface BuilderInterface
1313
{
14-
/**
14+
/*
1515
* Method that creates a Build object based on the provided parameters.
1616
*
1717
* @param string $name The name of the mapper (short name of the class)
@@ -24,11 +24,11 @@ interface BuilderInterface
2424
*
2525
* @throws BuilderException
2626
*/
27-
public function build(
28-
string $name,
29-
Blueprints $blueprints,
30-
TypeInterface $from,
31-
TypeInterface $to,
32-
bool $isCollection,
33-
): Build;
27+
// public function build(
28+
// string $name,
29+
// Blueprints $blueprints,
30+
// TypeInterface $from,
31+
// TypeInterface $to,
32+
// bool $isCollection,
33+
// ): Build;
3434
}

src/Build/Application/Model/Briefcase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class Briefcase
1111
{
12-
/** @var ClassCard[] */
12+
/** @var Assets\ClassCard[] */
1313
public array $classes = [];
1414

1515
public function __construct(

src/Build/Application/Service/BuilderService.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
class BuilderService implements BuilderInterface
1313
{
14-
public function build(string $name, Blueprints $blueprints, TypeInterface $from, TypeInterface $to, bool $isCollection): Build
15-
{
16-
// match properties into Class_ and Property_ objects
14+
// public function build(string $name, Blueprints $blueprints, TypeInterface $from, TypeInterface $to, bool $isCollection): Build
15+
// {
16+
// // match properties into Class_ and Property_ objects
1717

18-
throw new \LogicException('Method not implemented yet.');
19-
}
18+
// throw new \LogicException('Method not implemented yet.');
19+
// }
2020
}

src/Mapper/Application/Contract/MapperInterface.php

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
interface MapperInterface
1010
{
11+
public const BLUEPRINT_PROCESS_USE = 'origin';
12+
public const FROM_PROCESS_USE = 'source';
13+
public const TO_PROCESS_USE = 'target';
14+
1115
/**
1216
* The method maps the data from a array or object to another array or object.
1317
*

src/Mapper/Application/Contract/ModificatorInterface.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,31 @@
44

55
namespace PBaszak\UltraMapper\Mapper\Application\Contract;
66

7+
use PBaszak\UltraMapper\Blueprint\Application\Model\Blueprint;
8+
79
/**
810
* The interface is used to handle modifiers to be used to create
911
* the mapping class. The list of modifiers is provided in the constructor.
1012
* If you are looking for an interface denoting a single modifier, @see ModifierInterface.
1113
*/
1214
interface ModificatorInterface
1315
{
14-
public function prepareBlueprints(): void;
16+
/**
17+
* Get the list of modifiers. It's required by MapperService
18+
* to create hash of mapping class unique for the given modifiers.
19+
*
20+
* @return ModifierInterface[]
21+
*/
22+
public function getModifiers(): array;
23+
24+
/**
25+
* Prepare the blueprint for the mapping class. Called before
26+
* matching properties.
27+
*
28+
* @param string<"normalization"|"denormalization"|"transformation"|"mapping"> $processType
29+
* @param string<"origin"|"source"|"target"> $processUse
30+
*/
31+
public function prepareBlueprint(Blueprint $blueprint, string $processType, string $processUse): void;
1532

1633
public function modifyBlueprints(): void;
1734
}

src/Mapper/Application/Service/Mapper.php

+25-19
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
namespace PBaszak\UltraMapper\Mapper\Application\Service;
66

77
use PBaszak\UltraMapper\Blueprint\Application\Contract\BlueprintInterface;
8+
use PBaszak\UltraMapper\Blueprint\Application\Model\Blueprint;
89
use PBaszak\UltraMapper\Build\Application\Contract\BuilderInterface;
9-
use PBaszak\UltraMapper\Build\Application\Model\Blueprints;
1010
use PBaszak\UltraMapper\Mapper\Application\Contract\MapperInterface;
1111
use PBaszak\UltraMapper\Mapper\Application\Contract\ModificatorInterface;
1212
use PBaszak\UltraMapper\Mapper\Application\Contract\TypeInterface;
1313
use PBaszak\UltraMapper\Mapper\Application\Model\Envelope;
1414
use PBaszak\UltraMapper\Mapper\Domain\Contract\ClassMapperInterface;
1515
use PBaszak\UltraMapper\Mapper\Domain\Resolver\MapperResolver;
16+
use PBaszak\UltraMapper\Mapper\Domain\Resolver\ProcessResolver;
17+
use PBaszak\UltraMapper\Mapper\Domain\Service\Matcher;
1618

1719
class Mapper implements MapperInterface
1820
{
@@ -44,6 +46,7 @@ public function __construct(
4446

4547
public function map(
4648
mixed $data,
49+
mixed &$output,
4750
string $blueprintClass,
4851
TypeInterface $from,
4952
TypeInterface $to,
@@ -64,30 +67,33 @@ protected function getMapper(
6467
TypeInterface $to,
6568
bool $isCollection = false
6669
): ClassMapperInterface {
67-
$shortName = $this->mapperResolver->getMapperShortClassName(...func_get_args());
70+
$shortName = $this->mapperResolver->getMapperShortClassName(...func_get_args(), ...$this->modificator->getModifiers());
6871

6972
// if the blueprint files were changed or the mapper does not exist
7073
if (
7174
($this->checkHashesOfDependentFiles && $this->blueprint->checkIfBlueprintFilesWasChanged($blueprintClass))
7275
|| null === $mapper = $this->mapperResolver->resolve($shortName)
7376
) {
7477
$blueprints = $this->createBlueprints($blueprintClass, $from, $to);
78+
$processType = (new ProcessResolver())->resolve($from, $to);
7579

76-
// prepare blueprints
80+
foreach ($blueprints as $processUse => $blueprint) {
81+
$this->modificator->prepareBlueprint($blueprint, $processType, $processUse);
82+
}
7783

78-
// match blueprints
84+
(new Matcher())->matchBlueprints($processType, ...$blueprints);
7985

8086
// modify blueprints
8187

82-
$build = $this->build->build(
83-
$shortName,
84-
$blueprints,
85-
$from,
86-
$to,
87-
$isCollection,
88-
);
88+
// $build = $this->build->build(
89+
// $shortName,
90+
// $blueprints,
91+
// $from,
92+
// $to,
93+
// $isCollection,
94+
// );
8995

90-
$this->mapperResolver->save($shortName, $build->getMapperFileBody());
96+
// $this->mapperResolver->save($shortName, $build->getMapperFileBody());
9197
$mapper = $this->mapperResolver->resolve($shortName);
9298
}
9399

@@ -97,23 +103,23 @@ protected function getMapper(
97103
/**
98104
* Create blueprints for the build.
99105
*
100-
* @return {'origin': BlueprintInterface, 'source': BlueprintInterface, 'target': BlueprintInterface}
106+
* @return array{'origin': Blueprint, 'source': Blueprint, 'target': Blueprint}
101107
*/
102108
protected function createBlueprints(
103109
string $blueprintClass,
104110
TypeInterface $from,
105111
TypeInterface $to
106-
): Blueprints {
112+
): array {
107113
$originBlueprint = $this->blueprint->createBlueprint($blueprintClass);
108114

109-
return new Blueprints(
110-
$originBlueprint,
111-
$from->getOverriddenBlueprintClass()
115+
return [
116+
self::BLUEPRINT_PROCESS_USE => $originBlueprint,
117+
self::FROM_PROCESS_USE => $from->getOverriddenBlueprintClass()
112118
? $this->blueprint->createBlueprint($from->getOverriddenBlueprintClass())
113119
: clone $originBlueprint,
114-
$to->getOverriddenBlueprintClass()
120+
self::TO_PROCESS_USE => $to->getOverriddenBlueprintClass()
115121
? $this->blueprint->createBlueprint($to->getOverriddenBlueprintClass())
116122
: clone $originBlueprint,
117-
);
123+
];
118124
}
119125
}

src/Mapper/Application/Service/Modificator.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PBaszak\UltraMapper\Mapper\Application\Service;
66

7+
use PBaszak\UltraMapper\Blueprint\Application\Model\Blueprint;
78
use PBaszak\UltraMapper\Mapper\Application\Contract\ModificatorInterface;
89
use PBaszak\UltraMapper\Mapper\Application\Contract\ModifierInterface;
910

@@ -15,7 +16,12 @@ public function __construct(
1516
) {
1617
}
1718

18-
public function prepareBlueprints(): void
19+
public function getModifiers(): array
20+
{
21+
return $this->modifiers;
22+
}
23+
24+
public function prepareBlueprint(Blueprint $blueprint, string $processType, string $processUse): void
1925
{
2026
foreach ($this->modifiers as $modifier) {
2127
// $modifier->prepareBlueprints();

src/Mapper/Domain/Contract/MatcherInterface.php

+4-15
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,20 @@
44

55
namespace PBaszak\UltraMapper\Mapper\Domain\Contract;
66

7-
use PBaszak\UltraMapper\Blueprint\Application\Model\Assets\ClassBlueprint;
8-
use PBaszak\UltraMapper\Blueprint\Application\Model\Assets\PropertyBlueprint;
97
use PBaszak\UltraMapper\Blueprint\Application\Model\Blueprint;
8+
use PBaszak\UltraMapper\Mapper\Application\Contract\MapperInterface;
109

1110
interface MatcherInterface
1211
{
1312
public const OPTION_ID = 'id';
1413
public const OPTION_PATH = 'path';
1514
public const OPTION_MIRROR = 'mirror';
16-
public const OPTION_ORIGIN = 'origin';
17-
public const OPTION_SOURCE = 'source';
18-
public const OPTION_TARGET = 'target';
15+
public const OPTION_ORIGIN = MapperInterface::BLUEPRINT_PROCESS_USE;
16+
public const OPTION_SOURCE = MapperInterface::FROM_PROCESS_USE;
17+
public const OPTION_TARGET = MapperInterface::TO_PROCESS_USE;
1918

2019
/**
2120
* @param string<"normalization"|"denormalization"|"transformation"|"mapping"> $processType
2221
*/
2322
public function matchBlueprints(string $processType, Blueprint $origin, Blueprint $source, Blueprint $target): void;
24-
25-
// /**
26-
// * @param string<"normalization"|"denormalization"|"transformation"|"mapping"> $processType
27-
// */
28-
// public function matchClassBlueprints(string $processType, ClassBlueprint $originClass, Blueprint $source, Blueprint $target): void;
29-
30-
// /**
31-
// * @param string<"normalization"|"denormalization"|"transformation"|"mapping"> $processType
32-
// */
33-
// public function matchProperties(string $processType, PropertyBlueprint $originProperty, Blueprint $source, Blueprint $target): void;
3423
}

src/Mapper/Domain/Resolver/ProcessResolver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ProcessResolver
1414
* @param TypeInterface $from the type of the source data
1515
* @param TypeInterface $to the type of the target data
1616
*
17-
* @return "normalization"|"denormalization"|"mapping"|"transformation"
17+
* @return string<"normalization"|"denormalization"|"mapping"|"transformation">
1818
*
1919
* @throws \LogicException If the process could not be resolved. - Should never happen.
2020
*/

src/Mapper/Domain/Service/Matcher.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,29 @@ public function matchBlueprints(string $processType, Blueprint $origin, Blueprin
1818
{
1919
$this->addLinks($origin, $source, $target);
2020
$rootBlueprints = array_map(
21-
fn(Blueprint $blueprint) => $blueprint->blueprints[$blueprint->root],
21+
fn (Blueprint $blueprint) => $blueprint->blueprints[$blueprint->root],
2222
[$origin, $source, $target]
2323
);
2424
$this->matchClassBlueprints($processType, ...$rootBlueprints);
2525
}
2626

27-
public function matchClassBlueprints(string $processType, ClassBlueprint $originClass, ClassBlueprint $source, ClassBlueprint $target): void
27+
protected function matchClassBlueprints(string $processType, ClassBlueprint $originClass, ClassBlueprint $source, ClassBlueprint $target): void
2828
{
2929
$this->addLinks($originClass, $source, $target);
3030

31+
/** @var PropertyBlueprint $property */
3132
foreach ($originClass->properties->assets as $property) {
3233
$this->matchProperties($processType, $property, $source, $target);
3334
}
3435
}
3536

36-
public function matchProperties(string $processType, PropertyBlueprint $originProperty, ClassBlueprint $source, ClassBlueprint $target): void
37+
protected function matchProperties(string $processType, PropertyBlueprint $originProperty, ClassBlueprint $source, ClassBlueprint $target): void
3738
{
38-
3939
}
4040

4141
protected function searchForPropertyWithSameName(PropertyBlueprint $originProperty, ClassBlueprint $blueprint): ?PropertyBlueprint
4242
{
43+
/** @var PropertyBlueprint $property */
4344
foreach ($blueprint->properties->assets as $property) {
4445
if ($property->originName === $originProperty->originName) {
4546
return $property;
@@ -49,7 +50,7 @@ protected function searchForPropertyWithSameName(PropertyBlueprint $originProper
4950
return null;
5051
}
5152

52-
protected function searchForPropertyBasedOnTargetPropertyAttribute(PropertyBlueprint $originProperty, ClassBlueprint $blueprint, ): ?PropertyBlueprint
53+
protected function searchForPropertyBasedOnTargetPropertyAttribute(PropertyBlueprint $originProperty, ClassBlueprint $blueprint): ?PropertyBlueprint
5354
{
5455
return null;
5556
}
@@ -59,17 +60,21 @@ protected function hasTargetPropertyAttribute(PropertyBlueprint|ParameterBluepri
5960
return isset($blueprint->attributes->assets[TargetProperty::class]) && count($blueprint->attributes->assets[TargetProperty::class]) > 0;
6061
}
6162

62-
protected function addLinks(object $origin, object $source, object $target): void
63-
{
63+
protected function addLinks(
64+
Blueprint|PropertyBlueprint|ClassBlueprint $origin,
65+
Blueprint|PropertyBlueprint|ClassBlueprint $source,
66+
Blueprint|PropertyBlueprint|ClassBlueprint $target
67+
): void {
68+
$blueprints = [$origin, $source, $target];
6469
array_walk(
65-
[$origin, $source, $target],
70+
$blueprints,
6671
function (object $object) {
6772
$object->options[self::OPTION_ID] = Uuid::v4()->toRfc4122();
6873
}
6974
);
7075

7176
array_walk(
72-
[$origin, $source, $target],
77+
$blueprints,
7378
function (object $object) use ($origin, $source, $target) {
7479
$object->options = array_replace_recursive(
7580
$object->options,

tests/Mapper/Unit/Domain/Service/MatcherTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88

99
class MatcherTest extends TestCase
1010
{
11-
1211
}

tools/phpstan/fpm-baseline.neon

+18-3
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,17 @@ parameters:
8686
path: ../../src/Mapper/Application/Attribute/TargetProperty.php
8787

8888
-
89-
message: "#^Method PBaszak\\\\UltraMapper\\\\Mapper\\\\Application\\\\Service\\\\Mapper\\:\\:getMapper\\(\\) should return PBaszak\\\\UltraMapper\\\\Mapper\\\\Domain\\\\Contract\\\\ClassMapperInterface but returns PBaszak\\\\UltraMapper\\\\Mapper\\\\Domain\\\\Contract\\\\ClassMapperInterface\\|null\\.$#"
89+
message: "#^PHPDoc tag @param for parameter \\$processType contains unresolvable type\\.$#"
9090
count: 1
91-
path: ../../src/Mapper/Application/Service/Mapper.php
91+
path: ../../src/Mapper/Application/Contract/ModificatorInterface.php
92+
93+
-
94+
message: "#^PHPDoc tag @param for parameter \\$processUse contains unresolvable type\\.$#"
95+
count: 1
96+
path: ../../src/Mapper/Application/Contract/ModificatorInterface.php
9297

9398
-
94-
message: "#^PHPDoc tag @return has invalid value \\(\\{'origin'\\: BlueprintInterface, 'source'\\: BlueprintInterface, 'target'\\: BlueprintInterface\\}\\)\\: Unexpected token \"\\{\", expected type at offset 66$#"
99+
message: "#^Method PBaszak\\\\UltraMapper\\\\Mapper\\\\Application\\\\Service\\\\Mapper\\:\\:getMapper\\(\\) should return PBaszak\\\\UltraMapper\\\\Mapper\\\\Domain\\\\Contract\\\\ClassMapperInterface but returns PBaszak\\\\UltraMapper\\\\Mapper\\\\Domain\\\\Contract\\\\ClassMapperInterface\\|null\\.$#"
95100
count: 1
96101
path: ../../src/Mapper/Application/Service/Mapper.php
97102

@@ -120,6 +125,11 @@ parameters:
120125
count: 1
121126
path: ../../src/Mapper/Domain/Abstract/AbstractMapper.php
122127

128+
-
129+
message: "#^PHPDoc tag @param for parameter \\$processType contains unresolvable type\\.$#"
130+
count: 1
131+
path: ../../src/Mapper/Domain/Contract/MatcherInterface.php
132+
123133
-
124134
message: "#^Method PBaszak\\\\UltraMapper\\\\Mapper\\\\Domain\\\\Resolver\\\\MapperResolver\\:\\:resolve\\(\\) should return PBaszak\\\\UltraMapper\\\\Mapper\\\\Domain\\\\Contract\\\\ClassMapperInterface\\|null but returns object\\.$#"
125135
count: 1
@@ -129,3 +139,8 @@ parameters:
129139
message: "#^Property PBaszak\\\\UltraMapper\\\\Mapper\\\\Domain\\\\Resolver\\\\MapperResolver\\:\\:\\$mappers \\(array\\<string, PBaszak\\\\UltraMapper\\\\Mapper\\\\Domain\\\\Contract\\\\ClassMapperInterface\\>\\) does not accept array\\<string, object\\>\\.$#"
130140
count: 1
131141
path: ../../src/Mapper/Domain/Resolver/MapperResolver.php
142+
143+
-
144+
message: "#^PHPDoc tag @return contains unresolvable type\\.$#"
145+
count: 1
146+
path: ../../src/Mapper/Domain/Resolver/ProcessResolver.php

0 commit comments

Comments
 (0)