1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cycle\Schema\Renderer\MermaidRenderer; |
6
|
|
|
|
7
|
|
|
use Cycle\ORM\Relation; |
8
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Exception\RelationNotFoundException; |
9
|
|
|
|
10
|
|
|
final class RelationMapper |
11
|
|
|
{ |
12
|
|
|
private const RELATION_TYPE = 1; |
13
|
|
|
|
14
|
|
|
private const RELATIONS = [ |
15
|
|
|
Relation::HAS_ONE => ['has_one', '||--||'], |
16
|
|
|
Relation::HAS_MANY => ['has_many', '||--|{'], |
17
|
|
|
Relation::BELONGS_TO => ['belongs_to', '||--||'], |
18
|
|
|
Relation::MANY_TO_MANY => ['many_to_many', '||--|{'], |
19
|
|
|
Relation::REFERS_TO => ['refers_to', '||--||'], |
20
|
|
|
Relation::MORPHED_HAS_MANY => ['morphed_has_many', '||--|{'], |
21
|
|
|
Relation::MORPHED_HAS_ONE => ['morphed_has_one', '||--||'], |
22
|
|
|
Relation::BELONGS_TO_MORPHED => ['belongs_to_morphed', '||--||'], |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Map relation with node |
27
|
|
|
* |
28
|
|
|
* @param int $code |
29
|
|
|
* @param bool $isNullable |
30
|
|
|
* @return string[] |
31
|
|
|
* @throws RelationNotFoundException |
32
|
|
|
*/ |
33
|
|
|
public function mapWithNode(int $code, bool $isNullable = false): array |
34
|
|
|
{ |
35
|
|
|
if (!isset(self::RELATIONS[$code])) { |
36
|
|
|
throw new RelationNotFoundException(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$relation = self::RELATIONS[$code]; |
40
|
|
|
|
41
|
|
|
if ($isNullable) { |
42
|
|
|
$relation[self::RELATION_TYPE] = substr_replace($relation[self::RELATION_TYPE], 'o', -2, 1); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $relation; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|