Completed
Pull Request — master (#227)
by Martin
02:59
created

Row::getValue()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 26
rs 4.909
cc 9
eloc 13
nc 6
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;
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') && $this->item instanceof LeanMapper\Entity) {
80
			return $this->getLeanMapperEntityProperty($this->item, $key);
81
82
		} else if (class_exists('DibiRow') && $this->item instanceof DibiRow) {
83
			return $this->item->{$key};
84
85
		} 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...
86
			return $this->item->{$key};
87
88
		} else if (is_array($this->item)) {
89
			if (!isset($this->item[$key])) {
90
				return NULL;
91
			}
92
93
			return $this->item[$key];
94
95
		} else {
96
			/**
97
			 * Doctrine entity
98
			 */
99
			return $this->getDoctrineEntityProperty($this->item, $key);
100
101
		}
102
	}
103
104
105
	/**
106
	 * @return Html
107
	 */
108
	public function getControl()
109
	{
110
		return $this->control;
111
	}
112
113
114
	/**
115
	 * @return string
116
	 */
117
	public function getControlClass()
118
	{
119
		if (!$class = $this->control->class) {
120
			return '';
121
		}
122
123
		return implode(' ', array_keys($class));
124
	}
125
126
127
	/**
128
	 * LeanMapper: Access object properties to get a item value
129
	 * @param  LeanMapper\Entity $item
130
	 * @param  mixed             $key
131
	 * @return mixed
132
	 */
133
	public function getLeanMapperEntityProperty(LeanMapper\Entity $item, $key)
134
	{
135
		$properties = explode('.', $key);
136
		$value = $item;
137
138
		while ($property = array_shift($properties)) {
139 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...
140
				if ($this->datagrid->strict_entity_property) {
141
					throw new DataGridException(sprintf(
142
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
143
						$value, str_replace('.', '->', $key)
144
					));
145
				}
146
147
				return NULL;
148
			}
149
150
			$value = $value->{$property};
151
		}
152
153
		return $value;
154
	}
155
156
157
	/**
158
	 * Doctrine: Access object properties to get a item value
159
	 * @param  mixed $item
160
	 * @param  mixed $key
161
	 * @return mixed
162
	 */
163
	public function getDoctrineEntityProperty($item, $key)
164
	{
165
		$properties = explode('.', $key);
166
		$value = $item;
167
		$accessor = PropertyAccessHelper::getAccessor();
168
169
		while ($property = array_shift($properties)) {
170 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...
171
				if ($this->datagrid->strict_entity_property) {
172
					throw new DataGridException(sprintf(
173
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
174
						$value, str_replace('.', '->', $key)
175
					));
176
				}
177
178
				return NULL;
179
			}
180
181
			$value = $accessor->getValue($value, $property);
182
		}
183
184
		return $value;
185
	}
186
187
188
	/**
189
	 * Get original item
190
	 * @return mixed
191
	 */
192
	public function getItem()
193
	{
194
		return $this->item;
195
	}
196
197
198
	/**
199
	 * Has particular row group actions allowed?
200
	 * @return bool
201
	 */
202
	public function hasGroupAction()
203
	{
204
		$condition = $this->datagrid->getRowCondition('group_action');
205
206
		return $condition ? $condition($this->item) : TRUE;
207
	}
208
209
210
	/**
211
	 * Has particular row and action allowed?
212
	 * @param  mixed  $key
213
	 * @return bool
214
	 */
215
	public function hasAction($key)
216
	{
217
		$condition = $this->datagrid->getRowCondition('action', $key);
218
219
		return $condition ? $condition($this->item) : TRUE;
220
	}
221
222
223
	/**
224
	 * @param  string        $key
225
	 * @param  Column\Column $column
226
	 * @return void
227
	 */
228
	public function applyColumnCallback($key, Column\Column $column)
229
	{
230
		$callback = $this->datagrid->getColumnCallback($key);
231
232
		if ($callback !== NULL) {
233
			call_user_func($callback, $column, $this->getItem());
234
		}
235
236
		return $column;
237
	}
238
239
}
240