Completed
Push — develop ( e67729...75d57c )
by Bartko
02:02
created

Options::getRightColumnName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTree\NestedSet;
6
7
use StefanoTree\Exception\InvalidArgumentException;
8
9
class Options
10
{
11
    private $tableName = '';
12
    private $tableAlias = null;
13
14
    private $sequenceName = null;
15
16
    private $idColumnName = '';
17
18
    private $leftColumnName = 'lft';
19
    private $rightColumnName = 'rgt';
20
    private $levelColumnName = 'level';
21
    private $parentIdColumnName = 'parent_id';
22
    private $scopeColumnName = null;
23
24
    /**
25
     * @param array $options
26
     *
27
     * @throws InvalidArgumentException
28
     */
29 240
    public function __construct(array $options)
30
    {
31
        $requiredOptions = array(
32 240
            'tableName', 'idColumnName',
33
        );
34
35 240
        $missingKeys = array_diff_key(array_flip($requiredOptions), $options);
36
37 240
        if (count($missingKeys)) {
38 1
            throw new InvalidArgumentException(implode(', ', array_flip($missingKeys))
39 1
                .' must be set');
40
        }
41
42 239
        $this->setOptions($options);
43 239
    }
44
45
    /**
46
     * @param array $options
47
     */
48 239
    protected function setOptions(array $options): void
49
    {
50 239
        foreach ($options as $name => $value) {
51 239
            $methodName = 'set'.ucfirst($name);
52 239
            if (method_exists($this, $methodName)) {
53 239
                $this->$methodName($value);
54
            }
55
        }
56 239
    }
57
58
    /**
59
     * @param string $tableName
60
     *
61
     * @throws InvalidArgumentException
62
     */
63 239
    public function setTableName(string $tableName): void
64
    {
65 239
        $tableName = trim($tableName);
66
67 239
        if (empty($tableName)) {
68 1
            throw new InvalidArgumentException('tableName cannot be empty');
69
        }
70
71 239
        $this->tableName = $tableName;
72 239
    }
73
74
    /**
75
     * @return string
76
     */
77 207
    public function getTableName(): string
78
    {
79 207
        return $this->tableName;
80
    }
81
82
    /**
83
     * @param string $tableAlias
84
     */
85 16
    public function setTableAlias(string $tableAlias): void
86
    {
87 16
        $tableAlias = trim($tableAlias);
88
89 16
        if (empty($tableAlias)) {
90
            throw new InvalidArgumentException('tableAlias cannot be empty');
91
        }
92
93 16
        $this->tableAlias = $tableAlias;
94 16
    }
95
96
    /**
97
     * If alias was not set then first lower cased character from table name is returned.
98
     *
99
     * @return string
100
     */
101 150
    public function getTableAlias(): string
102
    {
103 150
        if (null === $this->tableAlias) {
104 134
            $this->tableAlias = strtolower(substr($this->tableName, 0, 1));
105
        }
106
107 150
        return $this->tableAlias;
108
    }
109
110
    /**
111
     * @param string $sequenceName
112
     */
113 1
    public function setSequenceName(string $sequenceName): void
114
    {
115 1
        $this->sequenceName = trim($sequenceName);
116 1
    }
117
118
    /**
119
     * @return string|null
120
     */
121 19
    public function getSequenceName(): ?string
122
    {
123 19
        return $this->sequenceName;
124
    }
125
126
    /**
127
     * @param string $idColumnName
128
     *
129
     * @throws InvalidArgumentException
130
     */
131 239
    public function setIdColumnName(string $idColumnName): void
132
    {
133 239
        $idColumnName = trim($idColumnName);
134
135 239
        if (empty($idColumnName)) {
136 1
            throw new InvalidArgumentException('idColumnName cannot be empty');
137
        }
138
139 239
        $this->idColumnName = $idColumnName;
140 239
    }
141
142
    /**
143
     * @param bool $withTableAlias
144
     *
145
     * @return string
146
     */
147 167
    public function getIdColumnName(bool $withTableAlias = false): string
148
    {
149 167
        return ($withTableAlias) ? $this->addTableAlias($this->idColumnName) : $this->idColumnName;
150
    }
151
152
    /**
153
     * @param string $leftColumnName
154
     *
155
     * @throws InvalidArgumentException
156
     */
157 2
    public function setLeftColumnName(string $leftColumnName): void
158
    {
159 2
        $leftColumnName = trim($leftColumnName);
160
161 2
        if (empty($leftColumnName)) {
162 1
            throw new InvalidArgumentException('leftColumnName cannot be empty');
163
        }
164
165 1
        $this->leftColumnName = $leftColumnName;
166 1
    }
167
168
    /**
169
     * @param bool $withTableAlias
170
     *
171
     * @return string
172
     */
173 143
    public function getLeftColumnName(bool $withTableAlias = false): string
174
    {
175 143
        return ($withTableAlias) ? $this->addTableAlias($this->leftColumnName) : $this->leftColumnName;
176
    }
177
178
    /**
179
     * @param string $rightColumnName
180
     *
181
     * @throws InvalidArgumentException
182
     */
183 2
    public function setRightColumnName(string $rightColumnName): void
184
    {
185 2
        $rightColumnName = trim($rightColumnName);
186
187 2
        if (empty($rightColumnName)) {
188 1
            throw new InvalidArgumentException('rightColumnName cannot be empty');
189
        }
190
191 1
        $this->rightColumnName = $rightColumnName;
192 1
    }
193
194
    /**
195
     * @param bool $withTableAlias
196
     *
197
     * @return string
198
     */
199 142
    public function getRightColumnName(bool $withTableAlias = false): string
200
    {
201 142
        return ($withTableAlias) ? $this->addTableAlias($this->rightColumnName) : $this->rightColumnName;
202
    }
203
204
    /**
205
     * @param string $levelColumnName
206
     *
207
     * @throws InvalidArgumentException
208
     */
209 2
    public function setLevelColumnName(string $levelColumnName): void
210
    {
211 2
        $levelColumnName = trim($levelColumnName);
212
213 2
        if (empty($levelColumnName)) {
214 1
            throw new InvalidArgumentException('levelColumnName cannot be empty');
215
        }
216
217 1
        $this->levelColumnName = $levelColumnName;
218 1
    }
219
220
    /**
221
     * @param bool $withTableAlias
222
     *
223
     * @return string
224
     */
225 130
    public function getLevelColumnName(bool $withTableAlias = false): string
226
    {
227 130
        return ($withTableAlias) ? $this->addTableAlias($this->levelColumnName) : $this->levelColumnName;
228
    }
229
230
    /**
231
     * @param string $parentIdColumnName
232
     *
233
     * @throws InvalidArgumentException
234
     */
235 2
    public function setParentIdColumnName(string $parentIdColumnName): void
236
    {
237 2
        $parentIdColumnName = trim($parentIdColumnName);
238
239 2
        if (empty($parentIdColumnName)) {
240 1
            throw new InvalidArgumentException('parentIdColumnName cannot be empty');
241
        }
242
243 1
        $this->parentIdColumnName = $parentIdColumnName;
244 1
    }
245
246
    /**
247
     * @param bool $withTableAlias
248
     *
249
     * @return string
250
     */
251 138
    public function getParentIdColumnName(bool $withTableAlias = false): string
252
    {
253 138
        return ($withTableAlias) ? $this->addTableAlias($this->parentIdColumnName) : $this->parentIdColumnName;
254
    }
255
256
    /**
257
     * @param $scopeColumnName
258
     */
259 76
    public function setScopeColumnName(string $scopeColumnName): void
260
    {
261 76
        $this->scopeColumnName = trim($scopeColumnName);
262 76
    }
263
264
    /**
265
     * @param bool $withTableAlias
266
     *
267
     * @return string|null
268
     */
269 146
    public function getScopeColumnName(bool $withTableAlias = false): ?string
270
    {
271 146
        return ($withTableAlias) ? $this->addTableAlias($this->scopeColumnName) : $this->scopeColumnName;
272
    }
273
274 141
    private function addTableAlias(?string $value): ?string
275
    {
276 141
        if (null === $value) {
277
            return null;
278
        }
279
280 141
        return sprintf('%s.%s', $this->getTableAlias(), $value);
281
    }
282
}
283