Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.55% covered (success)
93.55%
29 / 31
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchIndex
93.55% covered (success)
93.55%
29 / 31
33.33% covered (danger)
33.33%
1 / 3
3.00
0.00% covered (danger)
0.00%
0 / 1
 __toString
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createFor
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\DoctrineEntityConverter\Entities;
3
4use Doctrine\ORM\Mapping\Column;
5use Doctrine\ORM\Mapping\Entity;
6use Doctrine\ORM\Mapping\GeneratedValue;
7use Doctrine\ORM\Mapping\Id;
8use Doctrine\ORM\Mapping\Index;
9use Doctrine\ORM\Mapping\JoinColumn;
10use Doctrine\ORM\Mapping\ManyToOne;
11use Doctrine\ORM\Mapping\MappedSuperclass;
12use JsonSerializable;
13use Nette\PhpGenerator\ClassType;
14use Stringable;
15
16#[MappedSuperclass]
17abstract class SearchIndex implements JsonSerializable, Stringable
18{
19    #[Column()]
20    public string $value;
21
22    public function __toString(): string
23    {
24        return $this->value;
25    }
26
27    public function jsonSerialize(): string
28    {
29        return $this->value;
30    }
31
32    final public static function createFor(
33        string $indexTableName,
34        string $originalTableName,
35        string $fieldName
36    ): ClassType {
37        $table = new ClassType($indexTableName);
38        $table->addAttribute(Entity::class);
39        $idProperty = $table->addProperty('id')->setPublic()->setType('int');
40        $idProperty->addAttribute(
41            Id::class
42        );
43        $idProperty->addAttribute(
44            Column::class
45        );
46        $idProperty->addAttribute(
47            GeneratedValue::class
48        );
49        $parentProperty = $table->addProperty('parent')->setPublic()->setType($originalTableName);
50        $parentProperty->addAttribute(
51            ManyToOne::class,
52            [
53                'targetEntity' => $originalTableName,
54                'inversedBy' => $fieldName,
55            ]
56        );
57        $parentProperty->addAttribute(
58            JoinColumn::class,
59            [
60                'onDelete' => 'CASCADE'
61            ]
62        );
63        $table->setExtends(SearchIndex::class);
64        $table->addAttribute(Index::class, ['columns' => ['parent_id', 'value']]);
65        return $table;
66    }
67}