-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from patchlevel/hooks
add pre extract and post hydrate hooks
- Loading branch information
Showing
10 changed files
with
278 additions
and
1 deletion.
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD)] | ||
final class PostHydrate | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD)] | ||
final class PreExtract | ||
{ | ||
} |
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Metadata; | ||
|
||
use ReflectionMethod; | ||
|
||
/** | ||
* @psalm-type serialized = array{ | ||
* className: class-string, | ||
* method: string, | ||
* } | ||
*/ | ||
final class CallbackMetadata | ||
{ | ||
public function __construct( | ||
private readonly ReflectionMethod $reflection, | ||
) { | ||
} | ||
|
||
public function reflection(): ReflectionMethod | ||
{ | ||
return $this->reflection; | ||
} | ||
|
||
public function methodName(): string | ||
{ | ||
return $this->reflection->getName(); | ||
} | ||
|
||
public function invoke(object $object): void | ||
{ | ||
$this->reflection->invoke($object); | ||
} | ||
|
||
/** @return serialized */ | ||
public function __serialize(): array | ||
{ | ||
return [ | ||
'className' => $this->reflection->getDeclaringClass()->getName(), | ||
'method' => $this->reflection->getName(), | ||
]; | ||
} | ||
|
||
/** @param serialized $data */ | ||
public function __unserialize(array $data): void | ||
{ | ||
$this->reflection = new ReflectionMethod($data['className'], $data['method']); | ||
} | ||
} |
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
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Tests\Unit\Fixture; | ||
|
||
use Patchlevel\Hydrator\Attribute\PostHydrate; | ||
use Patchlevel\Hydrator\Attribute\PreExtract; | ||
|
||
final class DtoWithHooks | ||
{ | ||
public bool $postHydrateCalled = false; | ||
|
||
public bool $preExtractCalled = false; | ||
|
||
#[PostHydrate] | ||
private function postHydrate(): void | ||
{ | ||
$this->postHydrateCalled = true; | ||
} | ||
|
||
#[PreExtract] | ||
private function preExtract(): void | ||
{ | ||
$this->preExtractCalled = true; | ||
} | ||
} |
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
Oops, something went wrong.