Completed
Pull Request — master (#339)
by
unknown
05:23
created

Row::getId()   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;
10
11
use Nette;
12
use LeanMapper;
13
use Nextras;
14
use DibiRow;
15
use Ublaboo\DataGrid\Utils\PropertyAccessHelper;
16
use Nette\Utils\Html;
17
use Ublaboo\DataGrid\Exception\DataGridException;
18
use Nette\Database\Table\ActiveRow;
19
20
class Row extends Nette\Object
21
{
22
23
	/**
24
	 * @var DataGrid
25
	 */
26
	protected $datagrid;
27
28
	/**
29
	 * @var mixed
30
	 */
31
	protected $item;
32
33
	/**
34
	 * @var string
35
	 */
36
	protected $primary_key;
37
38
	/**
39
	 * @var mixed
40
	 */
41
	protected $id;
42
43
	/**
44
	 * @var Html
45
	 */
46
	protected $control;
47
48
49
	/**
50
	 * @param DataGrid $datagrid
51
	 * @param mixed    $item
52
	 * @param string   $primary_key
53
	 */
54
	public function __construct(DataGrid $datagrid, $item, $primary_key)
55
	{
56
		$this->control = Html::el('tr');
57
		$this->datagrid = $datagrid;
58
		$this->item = $item;
59
		$this->primary_key = $primary_key;
60
		$this->id = $this->getValue($primary_key);
61
62
		if ($datagrid->hasColumnsSummary()) {
63
			$datagrid->getColumnsSummary()->add($this);
64
		}
65
	}
66
67
68
	/**
69
	 * Get id value of item
70
	 * @return mixed
71
	 */
72
	public function getId()
73
	{
74
		return $this->id;
75
	}
76
77
78
	/**
79
	 * Get item value of key
80
	 * @param  mixed $key
81
	 * @return mixed
82
	 */
83
	public function getValue($key)
84
	{
85
		if ($this->item instanceof LeanMapper\Entity) {
86
			return $this->getLeanMapperEntityProperty($this->item, $key);
87
88
		} else if ($this->item instanceof Nextras\Orm\Entity\Entity) {
0 ignored issues
show
Bug introduced by
The class Nextras\Orm\Entity\Entity 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...
89
			return $this->getNextrasEntityProperty($this->item, $key);
90
91
		} else if ($this->item instanceof DibiRow) {
92
			return $this->item->{$key};
93
94
		} else if ($this->item instanceof ActiveRow) {
95
			return $this->item->{$key};
96
97
		} else if (is_array($this->item)) {
98
			return $this->item[$key];
99
100
		} else {
101
			/**
102
			 * Doctrine entity
103
			 */
104
			return $this->getDoctrineEntityProperty($this->item, $key);
105
106
		}
107
	}
108
109
110
	/**
111
	 * @return Html
112
	 */
113
	public function getControl()
114
	{
115
		return $this->control;
116
	}
117
118
119
	/**
120
	 * @return string
121
	 */
122
	public function getControlClass()
123
	{
124
		if (!$class = $this->control->class) {
125
			return '';
126
		}
127
128
		return implode(' ', array_keys($class));
129
	}
130
131
132
	/**
133
	 * LeanMapper: Access object properties to get a item value
134
	 * @param  LeanMapper\Entity $item
135
	 * @param  mixed             $key
136
	 * @return mixed
137
	 */
138 View Code Duplication
	public function getLeanMapperEntityProperty(LeanMapper\Entity $item, $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
139
	{
140
		$properties = explode('.', $key);
141
		$value = $item;
142
143
		while ($property = array_shift($properties)) {
144
			if (!isset($value->{$property})) {
145
				if ($this->datagrid->strict_entity_property) {
146
					throw new DataGridException(sprintf(
147
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
148
						$value, str_replace('.', '->', $key)
149
					));
150
				}
151
152
				return NULL;
153
			}
154
155
			$value = $value->{$property};
156
		}
157
158
		return $value;
159
	}
160
161
162
	/**
163
	 * Nextras: Access object properties to get a item value
164
	 * @param  Nextras\Orm\Entity\Entity $item
165
	 * @param  string                    $key
166
	 * @return mixed
167
	 */
168 View Code Duplication
	public function getNextrasEntityProperty(Nextras\Orm\Entity\Entity $item, $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
169
	{
170
		$properties = explode('.', $key);
171
		$value = $item;
172
173
		while ($property = array_shift($properties)) {
174
			if (!isset($value->{$property})) {
175
				if ($this->datagrid->strict_entity_property) {
176
					throw new DataGridException(sprintf(
177
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
178
						$value, str_replace('.', '->', $key)
179
					));
180
				}
181
182
				return NULL;
183
			}
184
185
			$value = $value->{$property};
186
		}
187
188
		return $value;
189
	}
190
191
192
	/**
193
	 * Doctrine: Access object properties to get a item value
194
	 * @param  mixed $item
195
	 * @param  mixed $key
196
	 * @return mixed
197
	 */
198
	public function getDoctrineEntityProperty($item, $key)
199
	{
200
		$properties = explode('.', $key);
201
		$value = $item;
202
		$accessor = PropertyAccessHelper::getAccessor();
203
204
		while ($property = array_shift($properties)) {
205
			if (!is_object($value) && !$value) {
206
				if ($this->datagrid->strict_entity_property) {
207
					throw new DataGridException(sprintf(
208
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
209
						$value, str_replace('.', '->', $key)
210
					));
211
				}
212
213
				return NULL;
214
			}
215
216
			$value = $accessor->getValue($value, $property);
217
		}
218
219
		return $value;
220
	}
221
222
223
	/**
224
	 * Get original item
225
	 * @return mixed
226
	 */
227
	public function getItem()
228
	{
229
		return $this->item;
230
	}
231
232
233
	/**
234
	 * Has particular row group actions allowed?
235
	 * @return bool
236
	 */
237
	public function hasGroupAction()
238
	{
239
		$condition = $this->datagrid->getRowCondition('group_action');
240
241
		return $condition ? $condition($this->item) : TRUE;
242
	}
243
244
245
	/**
246
	 * Has particular row a action allowed?
247
	 * @param  mixed  $key
248
	 * @return bool
249
	 */
250
	public function hasAction($key)
251
	{
252
		$condition = $this->datagrid->getRowCondition('action', $key);
253
254
		return $condition ? $condition($this->item) : TRUE;
255
	}
256
257
258
	/**
259
	 * Has particular row inlie edit allowed?
260
	 * @return bool
261
	 */
262
	public function hasInlineEdit()
263
	{
264
		$condition = $this->datagrid->getRowCondition('inline_edit');
265
266
		return $condition ? $condition($this->item) : TRUE;
267
	}
268
269
270
	/**
271
	 * @param  string        $key
272
	 * @param  Column\Column $column
273
	 * @return void
274
	 */
275
	public function applyColumnCallback($key, Column\Column $column)
276
	{
277
		$callback = $this->datagrid->getColumnCallback($key);
278
279
		if ($callback !== NULL) {
280
			call_user_func($callback, $column, $this->getItem());
281
		}
282
283
		return $column;
284
	}
285
286
}
287