Skip to content

Commit d2784ba

Browse files
committed
fix: Code fixed - recursive loop checker work correctly
1 parent 0296a8b commit d2784ba

16 files changed

+182
-134
lines changed

src/Mapper/Application/Attribute/Accessor.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ class Accessor implements AttributeInterface
2020
public function __construct(
2121
public readonly ?string $getter = null,
2222
public readonly ?string $setter = null,
23-
public bool $useForDenormalization = true,
24-
public bool $useForMapping = false,
25-
public bool $useForNormalization = true,
23+
public readonly int $processType = self::DENORMALIZATION | self::NORMALIZATION | self::TRANSFORMATION | self::MAPPING,
2624
public readonly array $options = []
2725
) {
2826
}
@@ -31,4 +29,9 @@ public function validate(\ReflectionProperty|\ReflectionParameter|\ReflectionCla
3129
{
3230
// todo implement
3331
}
32+
33+
public function getProcessType(): int
34+
{
35+
return $this->processType;
36+
}
3437
}

src/Mapper/Application/Attribute/ApplyToCollectionItem.php

+5
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ public function validate(\ReflectionProperty|\ReflectionParameter|\ReflectionCla
5050
// }
5151
// }
5252
}
53+
54+
public function getProcessType(): int
55+
{
56+
return self::MAPPING | self::DENORMALIZATION | self::NORMALIZATION | self::TRANSFORMATION;
57+
}
5358
}

src/Mapper/Application/Attribute/Callback.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ public function __construct(
9595
* - Stage 4 Arbitrary callback to complete property mapping. Ex. logging
9696
*/
9797
public int $stage,
98-
public bool $useForDenormalization = true,
99-
public bool $useForMapping = false,
100-
public bool $useForNormalization = false,
98+
public int $processType,
10199
/**
102100
* @var int $priority The priority of the callback. The higher the value, the earlier the callback will be executed.
103101
*/
@@ -139,4 +137,9 @@ public function validate(\ReflectionProperty|\ReflectionParameter|\ReflectionCla
139137
// $this->throwException('You have to use {{var.name}} and {{source.getter}} placeholders in your callback string.', 5956, $reflection);
140138
// }
141139
}
140+
141+
public function getProcessType(): int
142+
{
143+
return $this->processType;
144+
}
142145
}

src/Mapper/Application/Attribute/Discriminator.php

+5
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ public function validate(\ReflectionProperty|\ReflectionParameter|\ReflectionCla
3737
{
3838
// todo implement
3939
}
40+
41+
public function getProcessType(): int
42+
{
43+
return self::MAPPING | self::DENORMALIZATION | self::NORMALIZATION | self::TRANSFORMATION;
44+
}
4045
}

src/Mapper/Application/Attribute/Groups.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ class Groups implements AttributeInterface
1212
{
1313
use ThrowAttributeValidationExceptionTrait;
1414

15+
/** @var string[] */
1516
public array $groups;
1617

1718
/**
18-
* @param string|array $groups the groups that the property should be included in
19+
* @param string|array<string> $groups the groups that the property should be included in
1920
* @param array<string, mixed> $options Options are for modificators of the mapping process. If You need them, You can use them.
2021
*/
2122
public function __construct(

src/Mapper/Application/Attribute/MaxDepth.php

-13
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,12 @@
66

77
use PBaszak\UltraMapper\Mapper\Application\Contract\AttributeInterface;
88
use PBaszak\UltraMapper\Mapper\Application\Exception\ThrowAttributeValidationExceptionTrait;
9-
use PBaszak\UltraMapper\Mapper\Domain\Model\Process;
109

1110
#[\Attribute(\Attribute::TARGET_PROPERTY)]
1211
class MaxDepth implements AttributeInterface
1312
{
1413
use ThrowAttributeValidationExceptionTrait;
1514

16-
public const DENORMALIZATION = 1; // 0001
17-
public const NORMALIZATION = 2; // 0010
18-
public const MAPPING = 4; // 0100
19-
public const TRANSFORMATION = 8; // 1000
20-
21-
public const PROCESS_TYPE_MAP = [
22-
Process::DENORMALIZATION_PROCESS => self::DENORMALIZATION,
23-
Process::NORMALIZATION_PROCESS => self::NORMALIZATION,
24-
Process::MAPPING_PROCESS => self::MAPPING,
25-
Process::TRANSFORMATION_PROCESS => self::TRANSFORMATION,
26-
];
27-
2815
/**
2916
* @param int $maxDepth the maximum depth of the object graph that should be mapped
3017
* @param mixed $fillWith the value that should be used to fill the property if the depth is exceeded

src/Mapper/Application/Attribute/SimpleObject.php

+5
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ public function validate(\ReflectionProperty|\ReflectionParameter|\ReflectionCla
3434
{
3535
// todo implement
3636
}
37+
38+
public function getProcessType(): int
39+
{
40+
return self::MAPPING | self::DENORMALIZATION | self::NORMALIZATION | self::TRANSFORMATION;
41+
}
3742
}

src/Mapper/Application/Attribute/TargetProperty.php

-48
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PBaszak\UltraMapper\Mapper\Application\Attribute;
6+
7+
use PBaszak\UltraMapper\Mapper\Application\Contract\AttributeInterface;
8+
use PBaszak\UltraMapper\Mapper\Application\Exception\ThrowAttributeValidationExceptionTrait;
9+
10+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER | \Attribute::IS_REPEATABLE)]
11+
class TargetPropertyName implements AttributeInterface
12+
{
13+
use ThrowAttributeValidationExceptionTrait;
14+
15+
/**
16+
* @param string $name the name of the target property
17+
* @param array<string, mixed> $options Options are for modificators of the mapping process. If You need them, You can use them.
18+
*/
19+
public function __construct(
20+
public readonly string $name,
21+
public readonly int $processType = self::DENORMALIZATION | self::NORMALIZATION | self::TRANSFORMATION | self::MAPPING,
22+
public readonly array $options = []
23+
) {
24+
}
25+
26+
public function validate(\ReflectionProperty|\ReflectionParameter|\ReflectionClass $reflection): void
27+
{
28+
// there cannot be two target properties with the same processType
29+
// todo implement
30+
}
31+
32+
public function getProcessType(): int
33+
{
34+
return $this->processType;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PBaszak\UltraMapper\Mapper\Application\Attribute;
6+
7+
use PBaszak\UltraMapper\Mapper\Application\Contract\AttributeInterface;
8+
use PBaszak\UltraMapper\Mapper\Application\Exception\ThrowAttributeValidationExceptionTrait;
9+
10+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
11+
class TargetPropertyPath implements AttributeInterface
12+
{
13+
use ThrowAttributeValidationExceptionTrait;
14+
15+
/**
16+
* @param ?string $path The path to the target property. If null, the name of the target property is used.
17+
* @param array<string, mixed> $options Options are for modificators of the mapping process. If You need them, You can use them.
18+
*/
19+
public function __construct(
20+
public readonly ?string $path = null,
21+
public readonly int $processType = self::DENORMALIZATION | self::NORMALIZATION | self::TRANSFORMATION | self::MAPPING,
22+
public readonly array $options = []
23+
) {
24+
}
25+
26+
public function validate(\ReflectionProperty|\ReflectionParameter|\ReflectionClass $reflection): void
27+
{
28+
// there cannot be two target properties with the same processType
29+
// todo implement
30+
}
31+
32+
public function getProcessType(): int
33+
{
34+
return $this->processType;
35+
}
36+
}

src/Mapper/Application/Exception/ThrowAttributeValidationExceptionTrait.php

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

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

7+
use PBaszak\UltraMapper\Mapper\Application\Exception\AttributeValidationException as AttributeException;
8+
79
trait ThrowAttributeValidationExceptionTrait
810
{
911
/**
@@ -14,6 +16,6 @@ protected function throwAttributeValidationException(string $message, int $code,
1416
$class = $reflection instanceof \ReflectionClass ? $reflection->getName() : $reflection->getDeclaringClass()->getName();
1517
$property = $reflection instanceof \ReflectionClass ? null : $reflection->getName();
1618

17-
throw new AttributeValidationException(sprintf('The %s attribute on %s%s is invalid. %s', (new \ReflectionClass($this))->getShortName(), $class, $property ? '::'.$property : '', $message), $code);
19+
throw new AttributeException(sprintf('The %s attribute on %s%s is invalid. %s', (new \ReflectionClass($this))->getShortName(), $class, $property ? '::'.$property : '', $message), $code);
1820
}
1921
}

src/Mapper/Domain/Modules/Checker/Contract/CheckerInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use PBaszak\UltraMapper\Blueprint\Application\Model\Blueprint;
88
use PBaszak\UltraMapper\Mapper\Application\Model\Context;
9-
use PBaszak\UltraMapper\Mapper\Domain\Checker\Exception\CheckerException;
109
use PBaszak\UltraMapper\Mapper\Domain\Model\Process;
10+
use PBaszak\UltraMapper\Mapper\Domain\Modules\Checker\Exception\CheckerException;
1111

1212
/**
1313
* Interface CheckerInterface is used to check if the blueprint

src/Mapper/Domain/Modules/Checker/Contract/CheckerStrategyInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use PBaszak\UltraMapper\Blueprint\Application\Model\Blueprint;
88
use PBaszak\UltraMapper\Mapper\Application\Model\Context;
9-
use PBaszak\UltraMapper\Mapper\Domain\Checker\Exception\CheckerException;
109
use PBaszak\UltraMapper\Mapper\Domain\Model\Process;
10+
use PBaszak\UltraMapper\Mapper\Domain\Modules\Checker\Exception\CheckerException;
1111

1212
interface CheckerStrategyInterface
1313
{

tests/Assets/pbaszak_ultramapper_tests_assets_dummysimplewithattribute.yaml

+8-5
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,15 @@ blueprints:
114114
defaultValue: null
115115
filesHashes:
116116
/app/tests/Assets/DummySimpleWithAttribute.php: 5de157db33ad5536dfb49fe841a058a3
117-
/app/src/Mapper/Application/Attribute/Callback.php: 99643441133906d4dcd15b67a24e47f7
118-
/app/src/Mapper/Application/Attribute/Ignore.php: e38c1b054ff976591a460c3084c22209
117+
/app/src/Mapper/Application/Attribute/Callback.php: 7ba81d4a5d36143504c6bcc2909e6a1c
118+
/app/src/Mapper/Application/Attribute/Ignore.php: 3ff46e0eb1d09e4a9d5b7c97302034de
119119
events:
120120
- 'Blueprint created. Root class: PBaszak\UltraMapper\Tests\Assets\DummySimpleWithAttribute.'
121121
- 'Class Blueprint PBaszak\UltraMapper\Tests\Assets\DummySimpleWithAttribute added. Blueprint name: pbaszak_ultramapper_tests_assets_dummysimplewithattribute.'
122122
- 'File hash added. File: /app/tests/Assets/DummySimpleWithAttribute.php, hash: 5de157db33ad5536dfb49fe841a058a3.'
123-
- 'File hash added. File: /app/src/Mapper/Application/Attribute/Callback.php, hash: 99643441133906d4dcd15b67a24e47f7.'
124-
- 'File hash already exists. File: /app/src/Mapper/Application/Attribute/Callback.php, hash: 99643441133906d4dcd15b67a24e47f7.'
125-
- 'File hash added. File: /app/src/Mapper/Application/Attribute/Ignore.php, hash: e38c1b054ff976591a460c3084c22209.'
123+
- 'File hash added. File: /app/src/Mapper/Application/Attribute/Callback.php, hash: 7ba81d4a5d36143504c6bcc2909e6a1c.'
124+
- 'File hash already exists. File: /app/src/Mapper/Application/Attribute/Callback.php, hash: 7ba81d4a5d36143504c6bcc2909e6a1c.'
125+
- 'File hash added. File: /app/src/Mapper/Application/Attribute/Ignore.php, hash: 3ff46e0eb1d09e4a9d5b7c97302034de.'
126+
- 'File hash already exists. File: /app/src/Mapper/Application/Attribute/Callback.php, hash: 7ba81d4a5d36143504c6bcc2909e6a1c.'
127+
- 'File hash already exists. File: /app/src/Mapper/Application/Attribute/Callback.php, hash: 7ba81d4a5d36143504c6bcc2909e6a1c.'
128+
- 'File hash already exists. File: /app/src/Mapper/Application/Attribute/Ignore.php, hash: 3ff46e0eb1d09e4a9d5b7c97302034de.'

tests/Mapper/Unit/Domain/Matcher/Checker/RecursiveLoopCheckerTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use PBaszak\UltraMapper\Mapper\Application\Attribute\Ignore;
1010
use PBaszak\UltraMapper\Mapper\Application\Attribute\MaxDepth;
1111
use PBaszak\UltraMapper\Mapper\Application\Model\Context;
12-
use PBaszak\UltraMapper\Mapper\Domain\Matcher\Checker\RecursiveLoopChecker;
13-
use PBaszak\UltraMapper\Mapper\Domain\Matcher\Exception\BlueprintCheckerException;
1412
use PBaszak\UltraMapper\Mapper\Domain\Model\Process;
13+
use PBaszak\UltraMapper\Mapper\Domain\Modules\Checker\Exception\CheckerException;
14+
use PBaszak\UltraMapper\Mapper\Domain\Modules\Checker\Strategy\RecursiveLoopChecker;
1515
use PHPUnit\Framework\Attributes\Group;
1616
use PHPUnit\Framework\Attributes\Test;
1717
use PHPUnit\Framework\TestCase;
@@ -26,7 +26,7 @@ public function shouldThrowExceptionOnLoopDetected(): void
2626
public self $property;
2727
};
2828

29-
$this->expectException(BlueprintCheckerException::class);
29+
$this->expectException(CheckerException::class);
3030
(new RecursiveLoopChecker())->check(
3131
Blueprint::create(get_class($class)),
3232
new Process([Process::DENORMALIZATION_PROCESS]),
@@ -42,7 +42,7 @@ public function shouldThrowExceptionOnLoopDetectedInTheGroupContext(): void
4242
public self $property;
4343
};
4444

45-
$this->expectException(BlueprintCheckerException::class);
45+
$this->expectException(CheckerException::class);
4646
(new RecursiveLoopChecker())->check(
4747
Blueprint::create(get_class($class)),
4848
new Process([Process::DENORMALIZATION_PROCESS]),

0 commit comments

Comments
 (0)