-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored the attributes to use there own handler classes
- Loading branch information
1 parent
f076519
commit c9139a6
Showing
9 changed files
with
128 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Tibisoft\AutoTransformer\Attribute; | ||
|
||
interface AttributeHandlerInterface | ||
{ | ||
public function handle(BaseAttribute $attribute, \ReflectionClass $reflectionFrom, \ReflectionProperty $property, object $to, object $from): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Tibisoft\AutoTransformer\Attribute; | ||
|
||
abstract class BaseAttribute | ||
{ | ||
public function isHandledBy(): string | ||
{ | ||
return static::class . 'Handler'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Tibisoft\AutoTransformer\Attribute; | ||
|
||
class CountHandler implements AttributeHandlerInterface | ||
{ | ||
public function handle(BaseAttribute $attribute, \ReflectionClass $reflectionFrom, \ReflectionProperty $property, object $to, object $from): void | ||
{ | ||
$propertyFrom = $reflectionFrom->getProperty($property->getName()); | ||
$value = $propertyFrom->getValue($from); | ||
if(is_countable($value)) { | ||
$property->setValue($to, count($value)); | ||
} else { | ||
$property->setValue($to, 0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Tibisoft\AutoTransformer\Attribute; | ||
|
||
use Tibisoft\AutoTransformer\Exception\TransformException; | ||
|
||
class SynonymsHandler implements AttributeHandlerInterface | ||
{ | ||
public function handle(BaseAttribute $attribute, \ReflectionClass $reflectionFrom, \ReflectionProperty $property, object $to, object $from): void | ||
{ | ||
if(!($attribute instanceof Synonyms)) { | ||
return; | ||
} | ||
|
||
//find the first match! | ||
foreach ($attribute->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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
class User { | ||
public function __construct( | ||
private string $email, | ||
private array $comments) { | ||
} | ||
} | ||
|
||
class UserDTO { | ||
public function __construct( | ||
private string $email, | ||
#[\Tibisoft\AutoTransformer\Attribute\Count] | ||
private int $comments) { | ||
} | ||
} | ||
|
||
$user = new User('[email protected]', ['Comment #1', 'Comment #2', 'Comment #3', 'Comment #4']); | ||
|
||
$transformer = new \Tibisoft\AutoTransformer\AutoTransformer(); | ||
$userDTO = $transformer->transform($user, UserDTO::class); | ||
|
||
var_dump($userDTO); | ||
exit; |