Passed
Pull Request — 1.x (#12)
by
unknown
14:21
created

RelationMapper::mapWithNode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 3
nop 2
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