Completed
Pull Request — master (#1884)
by
unknown
01:24
created

Index::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Phinx\Db\Table;
9
10
use RuntimeException;
11
12
class Index
13
{
14
	/**
15
	 * @var string
16
	 */
17
	public const UNIQUE = 'unique';
18
19
	/**
20
	 * @var string
21
	 */
22
	public const INDEX = 'index';
23
24
	/**
25
	 * @var string
26
	 */
27
	public const FULLTEXT = 'fulltext';
28
29
	/**
30
	 * @var string[]
31
	 */
32
	protected $columns;
33
34
	/**
35
	 * @var string
36
	 */
37
	protected $type = self::INDEX;
38
39
	/**
40
	 * @var string|null
41
	 */
42
	protected $name;
43
44
	/**
45
	 * @var int|array|null
46
	 */
47
	protected $limit;
48
49
	/**
50
	 * @var string[]
51
	 */
52
	protected $order;
53
54
55
	/**
56
	 * Sets the index columns.
57
	 *
58
	 * @param string[] $columns Columns
59
	 *
60
	 * @return $this
61
	 */
62
	public function setColumns($columns)
63
	{
64
		$this->columns = $columns;
65
66
		return $this;
67
	}
68
69
	/**
70
	 * Gets the index columns.
71
	 *
72
	 * @return string[]
73
	 */
74 30
	public function getColumns()
75
	{
76 30
		return $this->columns;
77 30
	}
78
79
	/**
80
	 * Sets the index type.
81
	 *
82
	 * @param string $type Type
83
	 *
84
	 * @return $this
85 30
	 */
86
	public function setType($type)
87 30
	{
88
		$this->type = $type;
89
90
		return $this;
91
	}
92
93
	/**
94
	 * Gets the index type.
95
	 *
96 12
	 * @return string
97
	 */
98 12
	public function getType()
99 12
	{
100
		return $this->type;
101
	}
102
103
	/**
104
	 * Sets the index name.
105
	 *
106
	 * @param string $name Name
107 32
	 *
108
	 * @return $this
109 32
	 */
110
	public function setName($name)
111
	{
112
		$this->name = $name;
113
114
		return $this;
115
	}
116
117
	/**
118 10
	 * Gets the index name.
119
	 *
120 10
	 * @return string|null
121 10
	 */
122
	public function getName()
123
	{
124
		return $this->name;
125
	}
126
127
	/**
128
	 * Sets the index limit.
129 30
	 *
130
	 * @param int|array $limit limit value or array of limit value
131 30
	 *
132
	 * @return $this
133
	 */
134
	public function setLimit($limit)
135
	{
136
		$this->limit = $limit;
137
138
		return $this;
139
	}
140 1
141
	/**
142 1
	 * Gets the index limit.
143 1
	 *
144
	 * @return int|array
145
	 */
146
	public function getLimit()
147
	{
148
		return $this->limit;
149
	}
150
151 15
	/**
152
	 * Sets the index limit.
153 15
	 *
154
	 * @param string[] $order 
155
	 * @return $this 
156
	 */
157
	public function setOrder($order)
158
	{
159
		$this->order = $order;
160
161
		return $this;
162
	}
163 29
164
	/**
165
	 * Gets the index columns sort order.
166 29
	 *
167 29
	 * @return string[]
168 20
	 */
169 1
	public function getOrder()
170
	{
171
		return $this->order;
172
	}
173 19
174 9
175 9
	/**
176 9
	 * Utility method that maps an array of index options to this objects methods.
177 9
	 *
178
	 * @param array $options Options
179
	 *
180 12
	 * @throws \RuntimeException
181 12
	 *
182 28
	 * @return $this
183 28
	 */
184
	public function setOptions($options)
185
	{
186
		// Valid Options
187
		$validOptions = ['type', 'unique', 'name', 'limit', 'order'];
188
		foreach ($options as $option => $value) {
189
			if (!in_array($option, $validOptions, true)) {
190
				throw new RuntimeException(sprintf('"%s" is not a valid index option.', $option));
191
			}
192
193
			// handle $options['unique']
194
			if (strcasecmp($option, self::UNIQUE) === 0) {
195
				if ((bool)$value) {
196
					$this->setType(self::UNIQUE);
197
				}
198
				continue;
199
			}
200
201
			$method = 'set' . ucfirst($option);
202
			$this->$method($value);
203
		}
204
205
		return $this;
206
	}
207
}
208