|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PBaszak\UltraMapper\Mapper\Domain\Service; |
| 6 | + |
| 7 | +use PBaszak\UltraMapper\Blueprint\Application\Model\Assets\ClassBlueprint; |
| 8 | +use PBaszak\UltraMapper\Blueprint\Application\Model\Assets\PropertyBlueprint; |
| 9 | +use PBaszak\UltraMapper\Blueprint\Application\Model\Blueprint; |
| 10 | +use PBaszak\UltraMapper\Mapper\Application\Attribute\Ignore; |
| 11 | +use PBaszak\UltraMapper\Mapper\Application\Attribute\MaxDepth; |
| 12 | +use PBaszak\UltraMapper\Mapper\Domain\Model\Process; |
| 13 | + |
| 14 | +class LoopDetector |
| 15 | +{ |
| 16 | + protected Blueprint $root; |
| 17 | + |
| 18 | + /** |
| 19 | + * @param Blueprint $blueprint to check |
| 20 | + * |
| 21 | + * @throws \RuntimeException if loop is detected |
| 22 | + */ |
| 23 | + public function checkBlueprint(Blueprint $blueprint, Process $process): void |
| 24 | + { |
| 25 | + $this->root = $blueprint; |
| 26 | + foreach ($blueprint->blueprints as $index => $classBlueprint) { |
| 27 | + foreach ($process->processes as $processType) { |
| 28 | + $this->checkClassBlueprint($classBlueprint, $classBlueprint->name, $processType); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + protected function checkClassBlueprint(ClassBlueprint $blueprint, string $actualCheckedClass, string $processType): void |
| 34 | + { |
| 35 | + /** @var PropertyBlueprint $property */ |
| 36 | + foreach ($blueprint->properties as $property) { |
| 37 | + $types = $property->type->getAllClassTypes(); |
| 38 | + if (empty($types)) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + foreach ($types as $classType) { |
| 43 | + if ($actualCheckedClass == $classType) { |
| 44 | + if ( |
| 45 | + $this->hasAttribute($property, Ignore::class, $processType) |
| 46 | + || $this->hasAttribute($property, MaxDepth::class, $processType) |
| 47 | + ) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + throw new \RuntimeException('Loop detected'); |
| 52 | + } |
| 53 | + |
| 54 | + foreach ($this->root->blueprints as $classBlueprint) { |
| 55 | + if ($classType == $classBlueprint->name) { |
| 56 | + $this->checkClassBlueprint($classBlueprint, $actualCheckedClass, $processType); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + protected function hasAttribute(PropertyBlueprint $property, string $attr, string $processType): bool |
| 64 | + { |
| 65 | + return !empty($property->attributes[$attr]); |
| 66 | + |
| 67 | + foreach ($property->attributes[$attr] ?? [] as $attribute) { |
| 68 | + /** @var MaxDepth|Ignore $instance */ |
| 69 | + $instance = $attribute->newInstance(); |
| 70 | + |
| 71 | + $binaryProcessType = $instance::PROCESS_TYPE_MAP[$processType]; |
| 72 | + if (($instance->processType & $binaryProcessType) === $binaryProcessType) { |
| 73 | + return $instance; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return null; |
| 78 | + } |
| 79 | +} |
0 commit comments