Completed
Push — master ( 392d04...e48b3d )
by Pavel
02:16
created

ColumnStatus::hasCaret()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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