Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| DecimalCodeGenerator | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
1 / 1 |
| run | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadataBuilder\CodeGenerators; |
| 3 | |
| 4 | use Apie\Core\Identifiers\KebabCaseSlug; |
| 5 | use Apie\Core\ValueObjects\Decimal; |
| 6 | use Apie\StorageMetadata\Attributes\DecimalPropertyAttribute; |
| 7 | use Apie\StorageMetadataBuilder\Interfaces\RunGeneratedCodeContextInterface; |
| 8 | use Apie\StorageMetadataBuilder\Mediators\GeneratedCodeContext; |
| 9 | use Apie\TypeConverter\Utils\ReflectionTypeUtil; |
| 10 | |
| 11 | class DecimalCodeGenerator implements RunGeneratedCodeContextInterface |
| 12 | { |
| 13 | public function run(GeneratedCodeContext $generatedCodeContext): void |
| 14 | { |
| 15 | $property = $generatedCodeContext->getCurrentProperty(); |
| 16 | $propertyType = $property?->getType(); |
| 17 | |
| 18 | $table = $generatedCodeContext->getCurrentTable(); |
| 19 | if ($property === null || $table === null || $propertyType === null) { |
| 20 | return; |
| 21 | } |
| 22 | $type = ReflectionTypeUtil::toClass($propertyType); |
| 23 | if ($type === null || !$type->isSubclassOf(Decimal::class)) { |
| 24 | return; |
| 25 | } |
| 26 | $propertyName = 'apie_' |
| 27 | . str_replace('-', '_', (string) KebabCaseSlug::fromClass($property->getDeclaringClass())) |
| 28 | . '_' |
| 29 | . str_replace('-', '_', (string) KebabCaseSlug::fromClass($property)); |
| 30 | |
| 31 | $declaredProperty = $table->addProperty($propertyName)->setType('string')->setNullable($property->getType()->allowsNull()); |
| 32 | $declaredProperty->addAttribute( |
| 33 | DecimalPropertyAttribute::class, |
| 34 | [ |
| 35 | $property->name, |
| 36 | $property->getDeclaringClass()->name, |
| 37 | $type->getMethod('getNumberOfDecimals')->invoke(null) |
| 38 | ] |
| 39 | ); |
| 40 | } |
| 41 | } |