ColumnStatus::getOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\Column;
10
11
use Ublaboo\DataGrid\DataGrid;
12
use Ublaboo\DataGrid\Exception\DataGridColumnStatusException;
13
use Ublaboo\DataGrid\Row;
14
use Ublaboo\DataGrid\Status\Option;
15
use Ublaboo\DataGrid\Traits;
16
17 1
class ColumnStatus extends Column
18
{
19 1
	use Traits\TButtonCaret;
20
21
	/**
22
	 * @var callable[]
23
	 */
24
	public $onChange = [];
25
26
	/**
27
	 * @var string
28
	 */
29
	protected $key;
30
31
	/**
32
	 * @var array
33
	 */
34
	protected $options = [];
35
36
37
	/**
38
	 * @param DataGrid $grid
39
	 * @param string   $key
40
	 * @param string   $column
41
	 * @param string   $name
42
	 */
43
	public function __construct(DataGrid $grid, $key, $column, $name)
44
	{
45 1
		parent::__construct($grid, $key, $column, $name);
46
47 1
		$this->key = $key;
48
49 1
		$this->setTemplate(__DIR__ . '/../templates/column_status.latte');
50 1
	}
51
52
53
	/**
54
	 * @return string
55
	 */
56
	public function getKey()
57
	{
58 1
		return $this->key;
59
	}
60
61
62
	/**
63
	 * @return array
64
	 */
65
	public function getOptions()
66
	{
67 1
		return $this->options;
68
	}
69
70
71
	/**
72
	 * Get particular option
73
	 * @param  mixed $value
74
	 * @return Option
75
	 * @throws DataGridColumnStatusException
76
	 */
77
	public function getOption($value)
78
	{
79
		foreach ($this->options as $option) {
80
			if ($option->getValue() === $value) {
81
				return $option;
82
			}
83
		}
84
85
		throw new DataGridColumnStatusException("Option [$value] does not exist");
86
	}
87
88
89
	/**
90
	 * @return string
91
	 */
92
	public function getColumn()
93
	{
94 1
		return $this->column;
95
	}
96
97
98
	/**
99
	 * Find selected option for current item/row
100
	 * @param  Row    $row
101
	 * @return Option|NULL
102
	 */
103
	public function getCurrentOption(Row $row)
104
	{
105 1
		foreach ($this->getOptions() as $option) {
106 1
			if ($option->getValue() == $row->getValue($this->getColumn())) {
107 1
				return $option;
108
			}
109
		}
110
111
		return null;
112
	}
113
114
115
	/**
116
	 * Add option to status select
117
	 * @param mixed $value
118
	 * @param string $text
119
	 * @return Option
120
	 * @throws DataGridColumnStatusException
121
	 */
122
	public function addOption($value, $text)
123
	{
124 1
		if (!is_scalar($value)) {
125
			throw new DataGridColumnStatusException('Option value has to be scalar');
126
		}
127
128 1
		$option = new Option($this, $value, $text);
129
130 1
		$this->options[] = $option;
131
132 1
		return $option;
133
	}
134
135
136
	/**
137
	 * Set all options at once
138
	 * @param array $options
139
	 * @return static
140
	 */
141
	public function setOptions(array $options)
142
	{
143
		foreach ($options as $value => $text) {
144
			$this->addOption($value, $text);
145
		}
146
147
		return $this;
148
	}
149
150
151
	/**
152
	 * @param  mixed $value
153
	 * @return void
154
	 */
155
	public function removeOption($value)
156
	{
157
		foreach ($this->options as $key => $option) {
158
			if ($option->getValue() == $value) {
159
				unset($this->options[$key]);
160
			}
161
		}
162
	}
163
164
165
	/**
166
	 * Column can have variables that will be passed to custom template scope
167
	 * @return array
168
	 */
169
	public function getTemplateVariables()
170
	{
171
		return array_merge($this->template_variables, [
172
			'options' => $this->getOptions(),
173
			'column' => $this->getColumn(),
174
			'key' => $this->getKey(),
175
			'status' => $this,
176
		]);
177
	}
178
	
179
	public function setReplacement(array $replacements)	
180
	{
181
		throw new DataGridColumnStatusException('Cannot set replacement for Column Status. For status texts replacement use ->setOptions($replacements)');
182
	}
183
}
184