Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
RootObjectCodeGenerator
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
11
100.00% covered (success)
100.00%
1 / 1
 run
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
11
1<?php
2namespace Apie\StorageMetadataBuilder\CodeGenerators;
3
4use Apie\Core\Attributes\SearchFilterOption;
5use Apie\Core\Context\ApieContext;
6use Apie\Core\Metadata\Fields\GetterMethod;
7use Apie\Core\Metadata\Fields\PublicProperty;
8use Apie\Core\Metadata\MetadataFactory;
9use Apie\StorageMetadata\Attributes\GetSearchIndexAttribute;
10use Apie\StorageMetadataBuilder\Factories\ClassTypeFactory;
11use Apie\StorageMetadataBuilder\Interfaces\RootObjectInterface;
12use Apie\StorageMetadataBuilder\Interfaces\RunGeneratedCodeContextInterface;
13use Apie\StorageMetadataBuilder\Mediators\GeneratedCodeContext;
14
15/**
16 * Creates the root entities.
17 * - Has class RootObjectInterface
18 * - Has columns starting with search_ for search on columns.
19 */
20final class RootObjectCodeGenerator implements RunGeneratedCodeContextInterface
21{
22    public function run(GeneratedCodeContext $generatedCodeContext): void
23    {
24        $currentObject = $generatedCodeContext->getCurrentObject();
25        if (null === $currentObject || null !== $generatedCodeContext->getCurrentProperty()) {
26            return;
27        }
28        $tableName = $generatedCodeContext->getPrefix('apie_resource_');
29        $table = ClassTypeFactory::createStorageTable($tableName, $currentObject);
30        $metadata = MetadataFactory::getResultMetadata($currentObject, new ApieContext());
31        foreach ($metadata->getHashmap() as $fieldName => $fieldDefinition) {
32            if ($fieldDefinition instanceof GetterMethod || $fieldDefinition instanceof PublicProperty) {
33                $searchProperty = $table->addProperty('search_' . $fieldName);
34                $searchProperty->setType('array');
35                $enabled = true;
36                foreach ($fieldDefinition->getAttributes(SearchFilterOption::class) as $attr) {
37                    $enabled = $enabled && $attr->enabled;
38                }
39                if (!$enabled) {
40                    $table->removeProperty($searchProperty->getName());
41                    continue;
42                }
43                if ($fieldDefinition instanceof GetterMethod) {
44                    $searchProperty->addAttribute(GetSearchIndexAttribute::class, [$fieldDefinition->getReflectionMethod()->name]);
45                } elseif ($fieldDefinition instanceof PublicProperty) {
46                    $searchProperty->addAttribute(GetSearchIndexAttribute::class, [$fieldName]);
47                }
48            }
49        }
50
51        $table->addImplement(RootObjectInterface::class);
52        $generatedCodeContext->iterateOverTable($table);
53    }
54}