This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Config.php
120 lines (89 loc) · 2.91 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace Lookitsatravis\Listify;
use InvalidArgumentException;
class Config
{
const ADD_NEW_ITEM_TO_KEY = 'addNewItemTo';
const POSITION_BOTTOM = 'bottom';
const POSITION_COLUMN_NAME_KEY = 'positionColumnName';
const POSITION_TOP = 'top';
const SCOPE_KEY = 'scope';
const TOP_POSITION_IN_LIST_KEY = 'topPositionInList';
protected $config = [];
protected $defaultAddNewItemTo = self::POSITION_BOTTOM;
protected $defaultPositionColumnName = 'position';
protected $defaultScope = '1 = 1';
protected $defaultTopPositionInList = 1;
public function __construct()
{
$this->config = $this->buildDefaultConfig();
}
public function getAddNewItemTo()
{
return $this->get(self::ADD_NEW_ITEM_TO_KEY);
}
public function getPositionColumnName()
{
return $this->get(self::POSITION_COLUMN_NAME_KEY);
}
public function getScope()
{
return $this->get(self::SCOPE_KEY);
}
public function getTopPositionInList()
{
return $this->get(self::TOP_POSITION_IN_LIST_KEY);
}
public function setAddNewItemTo($listPosition)
{
if ($listPosition != self::POSITION_TOP && $listPosition != self::POSITION_BOTTOM) {
throw new InvalidArgumentException('Only POSITION_TOP and POSITION_BOTTOM are allowed.');
}
return $this->set(self::ADD_NEW_ITEM_TO_KEY, $listPosition);
}
public function setPositionColumnName($name)
{
return $this->set(self::POSITION_COLUMN_NAME_KEY, $name);
}
public function setScope($scope)
{
return $this->set(self::SCOPE_KEY, $scope);
}
public function setTopPositionInList($position)
{
if (! is_int($position)) {
throw new InvalidArgumentException('Only integers are allowed.');
}
return $this->set(self::TOP_POSITION_IN_LIST_KEY, $position);
}
protected function assertKeyIsValid($key): void
{
if (! in_array($key, $this->validKeys())) {
throw new InvalidArgumentException("Key '{$key}' is invalid.");
}
}
protected function buildDefaultConfig()
{
return [
self::TOP_POSITION_IN_LIST_KEY => $this->defaultTopPositionInList,
self::POSITION_COLUMN_NAME_KEY => $this->defaultPositionColumnName,
self::SCOPE_KEY => $this->defaultScope,
self::ADD_NEW_ITEM_TO_KEY => $this->defaultAddNewItemTo,
];
}
protected function get($key)
{
$this->assertKeyIsValid($key);
return $this->config[$key];
}
protected function set($key, $value)
{
$this->assertKeyIsValid($key);
$this->config[$key] = $value;
return $this;
}
protected function validKeys()
{
return [self::TOP_POSITION_IN_LIST_KEY, self::POSITION_COLUMN_NAME_KEY, self::SCOPE_KEY, self::ADD_NEW_ITEM_TO_KEY];
}
}