Completed
Push — master ( 91ef18...a6f36a )
by Pavel
03:04
created

Action::getConfirm()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 19
rs 9.4285
cc 3
eloc 8
nc 3
nop 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 Nette\Utils\Html;
12
use Ublaboo\DataGrid\DataGrid;
13
use Ublaboo\DataGrid\Exception\DataGridHasToBeAttachedToPresenterComponentException;
14
use Ublaboo\DataGrid\Exception\DataGridException;
15
use Ublaboo\DataGrid\Exception\DataGridColumnRendererException;
16
use Ublaboo\DataGrid\Row;
17
use Ublaboo\DataGrid\Traits;
18
19
class Action extends Column
20
{
21
22
	use Traits\ButtonIconTrait;
23
24
	/**
25
	 * @var string|callable
26
	 */
27
	protected $title;
28
29
	/**
30
	 * @var string|callable
31
	 */
32
	protected $class;
33
34
	/**
35
	 * @var string|callable
36
	 */
37
	protected $icon;
38
39
	/**
40
	 * @var DataGrid
41
	 */
42
	protected $grid;
43
44
	/**
45
	 * @var string
46
	 */
47
	protected $href;
48
49
	/**
50
	 * @var string
51
	 */
52
	protected $name;
53
54
	/**
55
	 * @var array
56
	 */
57
	protected $params;
58
59
	/**
60
	 * @var array|callable
61
	 */
62
	protected $confirm;
63
64
	/**
65
	 * @var array
66
	 */
67
	protected $data_attributes = [];
68
69
70
	/**
71
	 * @param DataGrid $grid
72
	 * @param string   $href
73
	 * @param string   $name
74
	 * @param array    $params
75
	 */
76
	public function __construct(DataGrid $grid, $href, $name, $params)
77
	{
78
		$this->grid = $grid;
79
		$this->href = $href;
80
		$this->name = $name;
81
		$this->params = $params;
82
83
		$this->class = 'btn btn-xs btn-default';
84
	}
85
86
87
	/**
88
	 * Render row item into template
89
	 * @param  Row   $row
90
	 * @return mixed
91
	 */
92
	public function render(Row $row)
93
	{
94
		/**
95
		 * Renderer function may be used
96
		 */
97
		try {
98
			return $this->useRenderer($row);
99
		} catch (DataGridColumnRendererException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
100
101
		$link = $this->createLink($this->href, $this->getItemParams($row, $this->params));
102
103
		$a = Html::el('a')->href($link);
104
105
		$this->tryAddIcon($a, $this->getIcon($row), $this->getName());
0 ignored issues
show
Bug introduced by
It seems like $this->getIcon($row) targeting Ublaboo\DataGrid\Column\Action::getIcon() can also be of type null; however, Ublaboo\DataGrid\Traits\...IconTrait::tryAddIcon() does only seem to accept callable, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
106
107
		if (!empty($this->data_attributes)) {
108
			foreach ($this->data_attributes as $key => $value) {
109
				$a->data($key, $value);
110
			}
111
		}
112
113
		$a->add($this->translate($this->getName()));
114
115
		if ($this->title) { $a->title($this->translate($this->getTitle($row))); }
116
		if ($this->class) { $a->class($this->getClass($row)); }
117
		if ($confirm = $this->getConfirm($row)) { $a->data('confirm', $this->translate($confirm)); }
118
119
		return $a;
120
	}
121
122
123
	/**
124
	 * Set attribute title
125
	 * @param string|callable $title
126
	 * @return static
127
	 * @throws DataGridException
128
	 */
129
	public function setTitle($title)
130
	{
131
		$this->checkPropertyStringOrCallable($title, 'title');
132
133
		$this->title = $title;
134
135
		return $this;
136
	}
137
138
139
	/**
140
	 * Get attribute title
141
	 * @param Row $row
142
	 * @return string
143
	 * @throws DataGridException
144
	 */
145
	public function getTitle(Row $row)
146
	{
147
		/**
148
		 * If user callback was used for setting action title, it has to return string
149
		 */
150
		return $this->getPropertyStringOrCallableGetString($row, $this->title, 'title');
0 ignored issues
show
Documentation introduced by
$this->title is of type callable, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
151
	}
152
153
154
	/**
155
	 * Set attribute class
156
	 * @param string|callable $class
157
	 * @return static
158
	 * @throws DataGridException
159
	 */
160
	public function setClass($class)
161
	{
162
		$this->checkPropertyStringOrCallable($class, 'class');
163
164
		$this->class = $class;
165
166
		return $this;
167
	}
168
169
170
	/**
171
	 * Get attribute class
172
	 * @param Row $row
173
	 * @return string
174
	 * @throws DataGridException
175
	 */
176
	public function getClass(Row $row)
177
	{
178
		/**
179
		 * If user callback was used for setting action class, it has to return string
180
		 */
181
		return $this->getPropertyStringOrCallableGetString($row, $this->class, 'class');
0 ignored issues
show
Documentation introduced by
$this->class is of type callable, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
182
	}
183
184
185
	/**
186
	 * Set icon
187
	 * @param string|callable $icon
188
	 * @return static|callable
189
	 * @throws DataGridException
190
	 */
191
	public function setIcon($icon)
192
	{
193
		$this->checkPropertyStringOrCallable($icon, 'icon');
194
195
		$this->icon = $icon;
196
197
		return $this;
198
	}
199
200
201
	/**
202
	 * Get icon
203
	 * @param Row $row
204
	 * @return string
205
	 * @throws DataGridException
206
	 */
207
	public function getIcon(Row $row)
208
	{
209
		/**
210
		 * If user callback was used for setting action icon, it has to return string
211
		 */
212
		return $this->getPropertyStringOrCallableGetString($row, $this->icon, 'icon');
0 ignored issues
show
Documentation introduced by
$this->icon is of type callable, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
213
	}
214
215
216
	/**
217
	 * Set confirm dialog
218
	 * @param string|callable $message
219
	 * @param string $column
220
	 * @return static
221
	 * @throws DataGridException
222
	 */
223
	public function setConfirm($message, $column = NULL)
224
	{
225
		$this->checkPropertyStringOrCallable($message, 'confirmation message');
226
227
		$this->confirm = [$message, $column];
228
229
		return $this;
230
	}
231
232
233
	/**
234
	 * Get confirm dialog for particular row item
235
	 * @param Row $row
236
	 * @return string
237
	 * @throws DataGridException
238
	 */
239
	public function getConfirm(Row $row)
240
	{
241
		if (!$this->confirm) {
242
			return NULL;
243
		}
244
245
		$question = $this->confirm[0];
246
247
		/**
248
		 * If user callback was used for setting action confirmation dialog, it has to return string
249
		 */
250
		$question = $this->getPropertyStringOrCallableGetString($row, $question, 'confirmation dialog');
251
252
		if (!$this->confirm[1]) {
253
			return $question;
254
		}
255
256
		return str_replace('%s', $row->getValue($this->confirm[1]), $question);
257
	}
258
259
260
	/**
261
	 * Setting data attributes
262
	 * @param string $key
263
	 * @param mixed $value
264
	 */
265
	public function setDataAttribute($key, $value)
266
	{
267
		$this->data_attributes[$key] = $value;
268
	}
269
270
271
	/**
272
	 * Check whether given property is string or callable
273
	 * @param  mixed $property
274
	 * @return void
275
	 * @throws DataGridException
276
	 */
277
	protected function checkPropertyStringOrCallable($property, $name)
278
	{
279
		if (!is_string($property) && !is_callable($property) && !is_null($property)) {
280
			throw new DataGridException(
281
				"Action {$name} has to be either string or a callback returning string"
282
			);
283
		}
284
	}
285
286
287
	/**
288
	 * Check whether given property is string or callable
289
	 * 	in that case call callback and check property and return it
290
	 * @param  Row         $row
291
	 * @param  string|NULL $property
292
	 * @param  string      $name
293
	 * @return string
294
	 * @throws DataGridException
295
	 */
296
	public function getPropertyStringOrCallableGetString(Row $row, $property, $name)
297
	{
298
		if (is_callable($property)) {
299
			$value = call_user_func($property, $row->getItem());
300
301
			if (!is_string($value)) {
302
				throw new DataGridException("Action {$name} callback has to return a string");
303
			}
304
305
			return $value;
306
		}
307
308
		return $property;
309
	}
310
311
312
	/**
313
	 * Translator helper
314
	 * @param  string $message
315
	 * @return string
316
	 */
317
	protected function translate($message)
318
	{
319
		return $this->grid->getTranslator()->translate($message);
320
	}
321
322
}
323