Issues (20)

src/Definition/Relation.php (1 issue)

Check for loose comparison of strings.

Bug Major
1
<?php
2
3
/**
4
 * Cycle ORM Schema Builder.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Schema\Definition;
13
14
use Cycle\Schema\Definition\Map\OptionMap;
15
use Cycle\Schema\Exception\RelationException;
16
17
final class Relation
18
{
19
    /** @var OptionMap */
20
    private $options;
21
22
    /** @var string */
23
    private $type;
24
25
    /** @var string */
26
    private $target;
27
28
    /** @var string|null */
29
    private $inverse = null;
30
31
    /** @var string|null */
32
    private $inverseType = null;
33
34
    /** @var int|null */
35
    private $inverseLoad = null;
36
37
    /**
38
     * Relation constructor.
39
     */
40
    public function __construct()
41
    {
42
        $this->options = new OptionMap();
43
    }
44
45
    /**
46
     * Cloning.
47
     */
48
    public function __clone()
49
    {
50
        $this->options = clone $this->options;
51
    }
52
53
    /**
54
     * @return OptionMap
55
     */
56
    public function getOptions(): OptionMap
57
    {
58
        return $this->options;
59
    }
60
61
    /**
62
     * @param string $type
63
     * @return Relation
64
     */
65
    public function setType(string $type): Relation
66
    {
67
        $this->type = $type;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getType(): string
76
    {
77
        if ($this->type === null) {
78
            throw new RelationException('Relation type must be set');
79
        }
80
81
        return $this->type;
82
    }
83
84
    /**
85
     * @param string $target
86
     * @return Relation
87
     */
88
    public function setTarget(string $target): Relation
89
    {
90
        $this->target = $target;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getTarget(): string
99
    {
100
        if ($this->target === null) {
101
            throw new RelationException('Relation target must be set');
102
        }
103
104
        return $this->target;
105
    }
106
107
    /**
108
     * @param string $into
109
     * @param string $as
110
     * @param int    $load
111
     * @return Relation
112
     */
113
    public function setInverse(string $into, string $as, ?int $load = null): Relation
114
    {
115
        $this->inverse = $into;
116
        $this->inverseType = $as;
117
        $this->inverseLoad = $load;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @return bool
124
     */
125
    public function isInversed(): bool
126
    {
127
        return $this->inverse != null;
0 ignored issues
show
It seems like you are loosely comparing $this->inverse of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
128
    }
129
130
    /**
131
     * @return string|null
132
     */
133
    public function getInverseName(): ?string
134
    {
135
        return $this->inverse;
136
    }
137
138
    /**
139
     * @return string|null
140
     */
141
    public function getInverseType(): ?string
142
    {
143
        return $this->inverseType;
144
    }
145
146
    /**
147
     * @return int|null
148
     */
149
    public function getInverseLoad(): ?int
150
    {
151
        return $this->inverseLoad;
152
    }
153
}
154