packages/sql-fixture/src/Plan/RelationKind.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlFixture\Plan;
6
7/**
8 * The relationship operators of DBML, whose values are the operators themselves.
9 *
10 * Many-to-many has no operator here. DBML spells it `<>` and invents a
11 * junction table to draw it with, but a fixture has to put rows in that
12 * junction table, so it must be named. Writing the two halves separately says
13 * the same thing without hiding the table that carries the data.
14 */
15enum RelationKind: string
16{
17    /**
18     * The left side is the one, the right side is the many.
19     */
20    case OneToMany = '<';
21
22    /**
23     * The left side is the many, the right side is the one.
24     */
25    case ManyToOne = '>';
26
27    case OneToOne = '-';
28
29    /**
30     * The end that holds a single row, and is generated first.
31     */
32    public function parentSide(): RelationSide
33    {
34        return match ($this) {
35            self::OneToMany, self::OneToOne => RelationSide::Left,
36            self::ManyToOne => RelationSide::Right,
37        };
38    }
39
40    /**
41     * The end that references the parent, and may hold several rows.
42     */
43    public function childSide(): RelationSide
44    {
45        return $this->parentSide()->opposite();
46    }
47
48    /**
49     * Whether the child end holds a list of rows rather than a single row.
50     */
51    public function childIsCollection(): bool
52    {
53        return $this !== self::OneToOne;
54    }
55}
56