Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
DecimalCodeGenerator
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
1 / 1
 run
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2namespace Apie\StorageMetadataBuilder\CodeGenerators;
3
4use Apie\Core\Identifiers\KebabCaseSlug;
5use Apie\Core\ValueObjects\Decimal;
6use Apie\StorageMetadata\Attributes\DecimalPropertyAttribute;
7use Apie\StorageMetadataBuilder\Interfaces\RunGeneratedCodeContextInterface;
8use Apie\StorageMetadataBuilder\Mediators\GeneratedCodeContext;
9use Apie\TypeConverter\Utils\ReflectionTypeUtil;
10
11class 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}