TButtonRenderer::getRenderer()   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
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 1
cts 1
cp 1
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\Traits;
10
11
use Ublaboo\DataGrid\Column\Renderer;
12
use Ublaboo\DataGrid\Exception\DataGridColumnRendererException;
13
use Ublaboo\DataGrid\Exception\DataGridException;
14
use Ublaboo\DataGrid\Row;
15
16 1
trait TButtonRenderer
17
{
18
19
	/**
20
	 * @var Renderer|null
21
	 */
22
	protected $renderer;
23
24
	/**
25
	 * @var array
26
	 */
27
	protected $replacements = [];
28
29
30
	/**
31
	 * Try to render item with custom renderer
32
	 * @param  Row|null $row
33
	 * @return mixed
34
	 * @throws DataGridColumnRendererException
35
	 */
36
	public function useRenderer($row = null)
37
	{
38 1
		$renderer = $this->getRenderer();
39
40 1
		if ($row instanceof Row) {
41 1
			$args = [$row->getItem()];
42
		} else {
43
			$args = [];
44
		}
45
46 1
		if (!$renderer) {
47 1
			throw new DataGridColumnRendererException;
48
		}
49
50
		if ($renderer->getConditionCallback()) {
51
			if (!call_user_func_array($renderer->getConditionCallback(), $args)) {
52
				throw new DataGridColumnRendererException;
53
			}
54
55
			return call_user_func_array($renderer->getCallback(), $args);
56
		}
57
58
		return call_user_func_array($renderer->getCallback(), $args);
59
	}
60
61
62
	/**
63
	 * Set renderer callback and (it may be optional - the condition callback will decide)
64
	 * @param callable $renderer
65
	 * @throws DataGridException
66
	 */
67
	public function setRenderer($renderer, $condition_callback = null)
68
	{
69
		if ($this->hasReplacements()) {
70
			throw new DataGridException(
71
				'Use either Column::setReplacement() or Column::setRenderer, not both.'
72
			);
73
		}
74
75
		if (!is_callable($renderer)) {
76
			throw new DataGridException(
77
				'Renderer (method Column::setRenderer()) must be callable.'
78
			);
79
		}
80
81
		if ($condition_callback != null && !is_callable($condition_callback)) {
82
			throw new DataGridException(
83
				'Renderer (method Column::setRenderer()) must be callable.'
84
			);
85
		}
86
87
		$this->renderer = new Renderer($renderer, $condition_callback);
88
89
		return $this;
90
	}
91
92
93
	/**
94
	 * Set renderer callback just if condition is truthy
95
	 * @param callable $renderer
96
	 */
97
	public function setRendererOnCondition($renderer, $condition_callback)
98
	{
99
		return $this->setRenderer($renderer, $condition_callback);
100
	}
101
102
103
	/**
104
	 * Return custom renderer callback
105
	 * @return Renderer|null
106
	 */
107
	public function getRenderer()
108
	{
109 1
		return $this->renderer;
110
	}
111
112
113
	/**
114
	 * Set column replacements
115
	 * @param  array $replacements
116
	 * @return Column
117
	 */
118
	public function setReplacement(array $replacements)
119
	{
120
		$this->replacements = $replacements;
121
122
		return $this;
123
	}
124
125
126
	/**
127
	 * Tell whether columns has replacements
128
	 * @return bool
129
	 */
130
	public function hasReplacements()
131
	{
132
		return (bool) $this->replacements;
133
	}
134
135
136
	/**
137
	 * Apply replacements
138
	 * @param  Row   $row
139
	 * @return array
140
	 */
141
	public function applyReplacements(Row $row)
142
	{
143 1
		$value = $row->getValue($this->column);
0 ignored issues
show
Bug introduced by
The property column does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
144
145 1
		if ((is_scalar($value) || $value === null) && isset($this->replacements[$value])) {
146
			return [true, $this->replacements[$value]];
147
		}
148
149 1
		return [false, null];
150
	}
151
}
152