Completed
Push — master ( 2b0446...afde37 )
by Bartko
04:01 queued 01:40
created

Options::setLeftColumnName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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