diff --git a/composer.json b/composer.json index 192a8d6..b744af1 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ }, "scripts": { "test": "vendor/bin/pest", - "analyse": "vendor/bin/phpstan analyse src --level=9" + "analyse": "vendor/bin/phpstan analyse src --level=9", + "tmp": "php tmp.php" } } diff --git a/src/Attribute/AttributeHandlerInterface.php b/src/Attribute/AttributeHandlerInterface.php new file mode 100644 index 0000000..0dfa439 --- /dev/null +++ b/src/Attribute/AttributeHandlerInterface.php @@ -0,0 +1,8 @@ +getProperty($property->getName()); + $value = $propertyFrom->getValue($from); + if(is_countable($value)) { + $property->setValue($to, count($value)); + } else { + $property->setValue($to, 0); + } + } +} diff --git a/src/Attribute/Synonyms.php b/src/Attribute/Synonyms.php index 5ffbfe8..28e97f9 100644 --- a/src/Attribute/Synonyms.php +++ b/src/Attribute/Synonyms.php @@ -6,7 +6,7 @@ use Assert\Assertion; #[\Attribute(\Attribute::TARGET_PROPERTY)] -class Synonyms +class Synonyms extends BaseAttribute { /** * @param array $synonyms diff --git a/src/Attribute/SynonymsHandler.php b/src/Attribute/SynonymsHandler.php new file mode 100644 index 0000000..e2f6012 --- /dev/null +++ b/src/Attribute/SynonymsHandler.php @@ -0,0 +1,45 @@ +synonyms as $synonym) { + $this->setPropertyValue($reflectionFrom, $property, $to, $from, $synonym); + } + } + + /** + * @param \ReflectionClass $reflectionFrom + * @param \ReflectionProperty $property + * @param object $to + * @param object $from + * @param string $propertyName + * + * @return void + */ + public function setPropertyValue(\ReflectionClass $reflectionFrom, \ReflectionProperty $property, object $to, object $from, string $propertyName = ''): void + { + $propertyName = ($propertyName === '') ? $property->getName() : $propertyName; + + try { + if (!$reflectionFrom->hasProperty($propertyName)) { + return; + } + + $propertyFrom = $reflectionFrom->getProperty($propertyName); + $property->setValue($to, $propertyFrom->getValue($from)); + } catch (\ReflectionException|\TypeError $typeError) { + throw new TransformException($typeError->getMessage()); + } + } +} diff --git a/src/AutoTransformer.php b/src/AutoTransformer.php index 9abaf9e..ffb0ba2 100644 --- a/src/AutoTransformer.php +++ b/src/AutoTransformer.php @@ -2,6 +2,8 @@ namespace Tibisoft\AutoTransformer; +use Tibisoft\AutoTransformer\Attribute\AttributeHandlerInterface; +use Tibisoft\AutoTransformer\Attribute\BaseAttribute; use Tibisoft\AutoTransformer\Attribute\Count; use Tibisoft\AutoTransformer\Attribute\Synonyms; use Tibisoft\AutoTransformer\Exception\TransformException; @@ -16,25 +18,23 @@ public function transform(object $from, string $to): object foreach ($reflectionTo->getProperties() as $property) { //look for attribute - $attributes = $property->getAttributes(Synonyms::class); - if (count($attributes) > 0) { - //find the first match! - foreach ($attributes[0]->getArguments()[0] as $synonym) { - $this->setPropertyValue($reflectionFrom, $property, $to, $from, $synonym); - continue 2; - } - } + $attributes = $property->getAttributes(); - $attributes = $property->getAttributes(Count::class); - if (count($attributes) > 0) { - $propertyFrom = $reflectionFrom->getProperty($property->getName()); - $value = $propertyFrom->getValue($from); - if(is_countable($value)) { - $property->setValue($to, count($value)); - } else { - $property->setValue($to, 0); + var_dump($attributes); + + /** @var \ReflectionAttribute $attribute */ + foreach($attributes as $attribute) { + $attribute = $attribute->newInstance(); + if(!($attribute instanceof BaseAttribute)) { + continue; + } + if (class_exists($attribute->isHandledBy())) { + /** @var AttributeHandlerInterface $handler */ + $handler = $attribute->isHandledBy(); + $handler = new $handler(); + $handler->handle($attribute, $reflectionFrom, $property, $to, $from); + continue 2; } - continue; } //do default @@ -49,6 +49,7 @@ public function transform(object $from, string $to): object * @param \ReflectionProperty $property * @param object $to * @param object $from + * @param string $propertyName * * @return void */ diff --git a/tmp.php b/tmp.php new file mode 100644 index 0000000..d5bad92 --- /dev/null +++ b/tmp.php @@ -0,0 +1,25 @@ +transform($user, UserDTO::class); + +var_dump($userDTO); +exit;