Repository navigation
feat: support for custom message generation in ZddMessageConfigInterface
#44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
353504a
feat: support for custom message generation in `ZddMessageConfigInter…
B-Galati e1408e4
refactor: extract `MessageGenerator` from `ZddMessageFactory`
B-Galati 72eaa1d
docs: update README wording and fix `message_config_service` registra…
B-Galati 0497be5
feat: extract `generateCustomMessage` into optional `CustomMessageGen…
B-Galati d1a74f6
doc: add phpstan target to Makefile help message
B-Galati 912a5ff
chore: remove useless PHPStan ignore
B-Galati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
| <?php | ||
|
|
||
| namespace Yousign\ZddMessageBundle\Config; | ||
|
|
||
| /** | ||
| * @experimental | ||
| */ | ||
| interface CustomMessageGeneratorInterface | ||
| { | ||
| /** | ||
| * If you need full control over how a specific message instance is created, | ||
| * use this method to return a fully instantiated message object. | ||
| * This is useful when the default instantiation (using reflection and property injection) | ||
| * is not sufficient or when your message requires specific constructor logic. | ||
| * | ||
| * WARNING: The object must be instantiated with minimum requirements (i.e., nullable properties | ||
| * must be set as null) in order to ensure a good ZDD test. | ||
| * | ||
| * @template T of object | ||
| * | ||
| * @param class-string<T> $className | ||
| * | ||
| * @return ?T The fully instantiated message object, or null if the default instantiation should be used | ||
| */ | ||
| public function generateCustomMessage(string $className): ?object; | ||
| } |
This file contains hidden or 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,79 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yousign\ZddMessageBundle\Factory; | ||
|
|
||
| use Yousign\ZddMessageBundle\Config\CustomMessageGeneratorInterface; | ||
| use Yousign\ZddMessageBundle\Config\ZddMessageConfigInterface; | ||
| use Yousign\ZddMessageBundle\Exceptions\MissingValueForTypeException; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| final class MessageGenerator | ||
| { | ||
| private readonly ZddPropertyExtractor $propertyExtractor; | ||
|
|
||
| public function __construct(private readonly ZddMessageConfigInterface $config) | ||
| { | ||
| $this->propertyExtractor = new ZddPropertyExtractor(); | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string $className | ||
| */ | ||
| public function generate(string $className): object | ||
| { | ||
| if ($this->config instanceof CustomMessageGeneratorInterface) { | ||
| $message = $this->config->generateCustomMessage($className); | ||
| if (null !== $message) { | ||
| return $message; | ||
| } | ||
| } | ||
|
|
||
| $message = (new \ReflectionClass($className))->newInstanceWithoutConstructor(); | ||
| $propertyList = $this->propertyExtractor->extractPropertiesFromClass($className); | ||
|
|
||
| foreach ($propertyList->getProperties() as $property) { | ||
| $value = $property->isNullable ? null : $this->generateValueForProperty($property); | ||
| $this->forcePropertyValue($message, $property->name, $value); | ||
| } | ||
|
|
||
| return $message; | ||
| } | ||
|
|
||
| /** | ||
| * @throws MissingValueForTypeException | ||
| */ | ||
| private function generateValueForProperty(Property $property): mixed | ||
| { | ||
| $value = $this->config->generateValueForCustomPropertyType($property->type); | ||
| if (null !== $value) { | ||
| return $value; | ||
| } | ||
|
|
||
| return match ($property->type) { | ||
| 'string' => 'Hello World!', | ||
| 'int' => 42, | ||
| 'float' => 42.42, | ||
| 'bool' => true, | ||
| 'array' => ['PHP', 'For The Win'], | ||
| default => throw MissingValueForTypeException::missingValue($property->type, $this->config), | ||
| }; | ||
| } | ||
|
|
||
| private function forcePropertyValue(object $object, string $property, mixed $value): void | ||
| { | ||
| $reflectionClass = new \ReflectionClass($object); | ||
| $reflectionProperty = $reflectionClass->getProperty($property); | ||
|
|
||
| // Readonly properties can only be set on the declaring class in case of inheritance | ||
| if ($reflectionProperty->isReadOnly()) { | ||
| $reflectionProperty = $reflectionProperty->getDeclaringClass()->getProperty($property); | ||
| } | ||
|
|
||
| $reflectionProperty->setAccessible(true); | ||
| $reflectionProperty->setValue($object, $value); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.