Completed
Push — master ( 6ac92e...33f9f0 )
by Pavel
02:48
created

Row::applyColumnCallback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
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;
10
11
use Nette;
12
use LeanMapper;
13
use DibiRow;
14
use Ublaboo\DataGrid\Utils\PropertyAccessHelper;
15
use Nette\Utils\Html;
16
use Ublaboo\DataGrid\Exception\DataGridException;
17
use Nette\Database\Table\ActiveRow;
18
19
class Row extends Nette\Object
20
{
21
22
	/**
23
	 * @var DataGrid
24
	 */
25
	protected $datagrid;
26
27
	/**
28
	 * @var mixed
29
	 */
30
	protected $item;
31
32
	/**
33
	 * @var string
34
	 */
35
	protected $primary_key;
36
37
	/**
38
	 * @var mixed
39
	 */
40
	protected $id;
41
42
	/**
43
	 * @var Html
44
	 */
45
	protected $control;
46
47
48
	/**
49
	 * @param mixed  $item
50
	 * @param string $primary_key
51
	 */
52
	public function __construct(DataGrid $datagrid, $item, $primary_key)
53
	{
54
		$this->control = Html::el('tr');
55
		$this->datagrid = $datagrid;
56
		$this->item = $item;
57
		$this->primary_key = $primary_key;
58
		$this->id = $this->getValue($primary_key);
59
	}
60
61
62
	/**
63
	 * Get id value of item
64
	 * @return mixed
65
	 */
66
	public function getId()
67
	{
68
		return $this->id;
69
	}
70
71
72
	/**
73
	 * Get item value of key
74
	 * @param  mixed $key
75
	 * @return mixed
76
	 */
77
	public function getValue($key)
78
	{
79
		if (class_exists(LeanMapper\Entity::class) && $this->item instanceof LeanMapper\Entity) {
80
			return $this->getLeanMapperEntityProperty($this->item, $key);
81
82
		} else if (class_exists(DibiRow::class) && $this->item instanceof DibiRow) {
83
			return $this->item->{$key};
84
85
		} else if (class_exists(ActiveRow::class) && $this->item instanceof ActiveRow) {
0 ignored issues
show
Bug introduced by
The class Nette\Database\Table\ActiveRow does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
86
			return $this->item->{$key};
87
88
		} else if (is_array($this->item)) {
89
			return $this->item[$key];
90
91
		} else {
92
			/**
93
			 * Doctrine entity
94
			 */
95
			return $this->getDoctrineEntityProperty($this->item, $key);
96
97
		}
98
	}
99
100
101
	/**
102
	 * @return Html
103
	 */
104
	public function getControl()
105
	{
106
		return $this->control;
107
	}
108
109
110
	/**
111
	 * @return string
112
	 */
113
	public function getControlClass()
114
	{
115
		if (!$class = $this->control->class) {
116
			return '';
117
		}
118
119
		return implode(' ', array_keys($class));
120
	}
121
122
123
	/**
124
	 * LeanMapper: Access object properties to get a item value
125
	 * @param  LeanMapper\Entity $item
126
	 * @param  mixed             $key
127
	 * @return mixed
128
	 */
129
	public function getLeanMapperEntityProperty(LeanMapper\Entity $item, $key)
130
	{
131
		$properties = explode('.', $key);
132
		$value = $item;
133
134
		while ($property = array_shift($properties)) {
135 View Code Duplication
			if (!isset($value->{$property})) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
				if ($this->datagrid->strict_entity_property) {
137
					throw new DataGridException(sprintf(
138
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
139
						$value, str_replace('.', '->', $key)
140
					));
141
				}
142
143
				return NULL;
144
			}
145
146
			$value = $value->{$property};
147
		}
148
149
		return $value;
150
	}
151
152
153
	/**
154
	 * Doctrine: Access object properties to get a item value
155
	 * @param  mixed $item
156
	 * @param  mixed $key
157
	 * @return mixed
158
	 */
159
	public function getDoctrineEntityProperty($item, $key)
160
	{
161
		$properties = explode('.', $key);
162
		$value = $item;
163
		$accessor = PropertyAccessHelper::getAccessor();
164
165
		while ($property = array_shift($properties)) {
166 View Code Duplication
			if (!is_object($value) && !$value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
				if ($this->datagrid->strict_entity_property) {
168
					throw new DataGridException(sprintf(
169
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
170
						$value, str_replace('.', '->', $key)
171
					));
172
				}
173
174
				return NULL;
175
			}
176
177
			$value = $accessor->getValue($value, $property);
178
		}
179
180
		return $value;
181
	}
182
183
184
	/**
185
	 * Get original item
186
	 * @return mixed
187
	 */
188
	public function getItem()
189
	{
190
		return $this->item;
191
	}
192
193
194
	/**
195
	 * Has particular row group actions allowed?
196
	 * @return bool
197
	 */
198
	public function hasGroupAction()
199
	{
200
		$condition = $this->datagrid->getRowCondition('group_action');
201
202
		return $condition ? $condition($this->item) : TRUE;
203
	}
204
205
206
	/**
207
	 * Has particular row and action allowed?
208
	 * @param  mixed  $key
209
	 * @return bool
210
	 */
211
	public function hasAction($key)
212
	{
213
		$condition = $this->datagrid->getRowCondition('action', $key);
214
215
		return $condition ? $condition($this->item) : TRUE;
216
	}
217
218
219
	/**
220
	 * @param  string        $key
221
	 * @param  Column\Column $column
222
	 * @return void
223
	 */
224
	public function applyColumnCallback($key, Column\Column $column)
225
	{
226
		$callback = $this->datagrid->getColumnCallback($key);
227
228
		if ($callback !== NULL) {
229
			call_user_func($callback, $column, $this->getItem());
230
		}
231
232
		return $column;
233
	}
234
235
}
236