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