Completed
Push — master ( 141d05...3164b1 )
by Pavel
03:05
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 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 DataGrid $datagrid
50
	 * @param mixed    $item
51
	 * @param string   $primary_key
52
	 */
53
	public function __construct(DataGrid $datagrid, $item, $primary_key)
54
	{
55
		$this->control = Html::el('tr');
56
		$this->datagrid = $datagrid;
57
		$this->item = $item;
58
		$this->primary_key = $primary_key;
59
		$this->id = $this->getValue($primary_key);
60
61
		if ($datagrid->hasColumnsSummary()) {
62
			$datagrid->getColumnsSummary()->add($this);
63
		}
64
	}
65
66
67
	/**
68
	 * Get id value of item
69
	 * @return mixed
70
	 */
71
	public function getId()
72
	{
73
		return $this->id;
74
	}
75
76
77
	/**
78
	 * Get item value of key
79
	 * @param  mixed $key
80
	 * @return mixed
81
	 */
82
	public function getValue($key)
83
	{
84
		if (class_exists('LeanMapper\Entity') && $this->item instanceof LeanMapper\Entity) {
85
			return $this->getLeanMapperEntityProperty($this->item, $key);
86
87
		} else if (class_exists('DibiRow') && $this->item instanceof DibiRow) {
88
			return $this->item->{$key};
89
90
		} else if (class_exists('ActiveRow') && $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...
91
			return $this->item->{$key};
92
93
		} else if (is_array($this->item)) {
94
			return $this->item[$key];
95
96
		} else {
97
			/**
98
			 * Doctrine entity
99
			 */
100
			return $this->getDoctrineEntityProperty($this->item, $key);
101
102
		}
103
	}
104
105
106
	/**
107
	 * @return Html
108
	 */
109
	public function getControl()
110
	{
111
		return $this->control;
112
	}
113
114
115
	/**
116
	 * @return string
117
	 */
118
	public function getControlClass()
119
	{
120
		if (!$class = $this->control->class) {
121
			return '';
122
		}
123
124
		return implode(' ', array_keys($class));
125
	}
126
127
128
	/**
129
	 * LeanMapper: Access object properties to get a item value
130
	 * @param  LeanMapper\Entity $item
131
	 * @param  mixed             $key
132
	 * @return mixed
133
	 */
134
	public function getLeanMapperEntityProperty(LeanMapper\Entity $item, $key)
135
	{
136
		$properties = explode('.', $key);
137
		$value = $item;
138
139
		while ($property = array_shift($properties)) {
140 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...
141
				if ($this->datagrid->strict_entity_property) {
142
					throw new DataGridException(sprintf(
143
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
144
						$value, str_replace('.', '->', $key)
145
					));
146
				}
147
148
				return NULL;
149
			}
150
151
			$value = $value->{$property};
152
		}
153
154
		return $value;
155
	}
156
157
158
	/**
159
	 * Doctrine: Access object properties to get a item value
160
	 * @param  mixed $item
161
	 * @param  mixed $key
162
	 * @return mixed
163
	 */
164
	public function getDoctrineEntityProperty($item, $key)
165
	{
166
		$properties = explode('.', $key);
167
		$value = $item;
168
		$accessor = PropertyAccessHelper::getAccessor();
169
170
		while ($property = array_shift($properties)) {
171 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...
172
				if ($this->datagrid->strict_entity_property) {
173
					throw new DataGridException(sprintf(
174
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
175
						$value, str_replace('.', '->', $key)
176
					));
177
				}
178
179
				return NULL;
180
			}
181
182
			$value = $accessor->getValue($value, $property);
183
		}
184
185
		return $value;
186
	}
187
188
189
	/**
190
	 * Get original item
191
	 * @return mixed
192
	 */
193
	public function getItem()
194
	{
195
		return $this->item;
196
	}
197
198
199
	/**
200
	 * Has particular row group actions allowed?
201
	 * @return bool
202
	 */
203
	public function hasGroupAction()
204
	{
205
		$condition = $this->datagrid->getRowCondition('group_action');
206
207
		return $condition ? $condition($this->item) : TRUE;
208
	}
209
210
211
	/**
212
	 * Has particular row a action allowed?
213
	 * @param  mixed  $key
214
	 * @return bool
215
	 */
216
	public function hasAction($key)
217
	{
218
		$condition = $this->datagrid->getRowCondition('action', $key);
219
220
		return $condition ? $condition($this->item) : TRUE;
221
	}
222
223
224
	/**
225
	 * Has particular row inlie edit allowed?
226
	 * @return bool
227
	 */
228
	public function hasInlineEdit()
229
	{
230
		$condition = $this->datagrid->getRowCondition('inline_edit');
231
232
		return $condition ? $condition($this->item) : TRUE;
233
	}
234
235
236
	/**
237
	 * @param  string        $key
238
	 * @param  Column\Column $column
239
	 * @return void
240
	 */
241
	public function applyColumnCallback($key, Column\Column $column)
242
	{
243
		$callback = $this->datagrid->getColumnCallback($key);
244
245
		if ($callback !== NULL) {
246
			call_user_func($callback, $column, $this->getItem());
247
		}
248
249
		return $column;
250
	}
251
252
}
253