Completed
Pull Request — master (#73)
by Martin
02:50
created

Row::hasAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
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
17
class Row extends Nette\Object
18
{
19
20
	/**
21
	 * @var DataGrid
22
	 */
23
	protected $datagrid;
24
25
	/**
26
	 * @var mixed
27
	 */
28
	protected $item;
29
30
	/**
31
	 * @var string
32
	 */
33
	protected $primary_key;
34
35
	/**
36
	 * @var mixed
37
	 */
38
	protected $id;
39
40
	/**
41
	 * @var Html
42
	 */
43
	protected $control;
44
45
46
	/**
47
	 * @param mixed  $item
48
	 * @param string $primary_key
49
	 */
50
	public function __construct(DataGrid $datagrid, $item, $primary_key)
51
	{
52
		$this->control = Html::el('tr');
53
		$this->datagrid = $datagrid;
54
		$this->item = $item;
55
		$this->primary_key = $primary_key;
56
		$this->id = $this->getValue($primary_key);
57
	}
58
59
60
	/**
61
	 * @return Html
62
	 */
63
	public function getControl()
64
	{
65
		return $this->control;
66
	}
67
68
69
	/**
70
	 * Get id value of item
71
	 * @return mixed
72
	 */
73
	public function getId()
74
	{
75
		return $this->id;
76
	}
77
78
79
	/**
80
	 * Get item value of key
81
	 * @param  mixed $key
82
	 * @return mixed
83
	 */
84
	public function getValue($key)
85
	{
86
		if ($this->item instanceof LeanMapper\Entity) {
87
			return $this->getLeanMapperEntityProperty($this->item, $key);
88
89
		} else if ($this->item instanceof DibiRow) {
90
			return $this->item->{$key};
91
92
		} else if ($this->item instanceof Nette\Database\Table\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...
93
			return $this->item->{$key};
94
95
		} else if (is_array($this->item)) {
96
			return $this->item[$key];
97
98
		} else {
99
			/**
100
			 * Doctrine entity
101
			 */
102
			return $this->getDoctrineEntityProperty($this->item, $key);
103
104
		}
105
	}
106
107
108
	/**
109
	 * @return string
110
	 */
111
	public function getClass()
112
	{
113
		if (!$class = $this->control->class) {
114
			return '';
115
		}
116
117
		return implode(' ', array_keys($class));
118
	}
119
120
121
	/**
122
	 * LeanMapper: Access object properties to get a item value
123
	 * @param  LeanMapper\Entity $item
124
	 * @param  mixed             $key
125
	 * @return mixed
126
	 */
127
	public function getLeanMapperEntityProperty(LeanMapper\Entity $item, $key)
128
	{
129
		$properties = explode('.', $key);
130
		$value = $item;
131
132
		while ($property = array_shift($properties)) {
133 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...
134
				if ($this->datagrid->strict_entity_property) {
135
					throw new DataGridException(sprintf(
136
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
137
						$value, str_replace('.', '->', $key)
138
					));
139
				}
140
141
				return NULL;
142
			}
143
144
			$value = $value->{$property};
145
		}
146
147
		return $value;
148
	}
149
150
151
	/**
152
	 * Doctrine: Access object properties to get a item value
153
	 * @param  mixed $item
154
	 * @param  mixed $key
155
	 * @return mixed
156
	 */
157
	public function getDoctrineEntityProperty($item, $key)
158
	{
159
		$properties = explode('.', $key);
160
		$value = $item;
161
		$accessor = PropertyAccessHelper::getAccessor();
162
163
		while ($property = array_shift($properties)) {
164 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...
165
				if ($this->datagrid->strict_entity_property) {
166
					throw new DataGridException(sprintf(
167
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
168
						$value, str_replace('.', '->', $key)
169
					));
170
				}
171
172
				return NULL;
173
			}
174
175
			$value = $accessor->getValue($value, $property);
176
		}
177
178
		return $value;
179
	}
180
181
182
	/**
183
	 * Get original item
184
	 * @return mixed
185
	 */
186
	public function getItem()
187
	{
188
		return $this->item;
189
	}
190
191
192
	/**
193
	 * Has particular row group actions allowed?
194
	 * @return bool
195
	 */
196
	public function hasGroupAction()
197
	{
198
		$condition = $this->datagrid->getRowCondition('group_action');
199
200
		return $condition ? $condition($this->item) : TRUE;
201
	}
202
203
204
	/**
205
	 * Has particular row and action allowed?
206
	 * @param  mixed  $key
207
	 * @return bool
208
	 */
209
	public function hasAction($key)
210
	{
211
		$condition = $this->datagrid->getRowCondition('action', $key);
212
213
		return $condition ? $condition($this->item) : TRUE;
214
	}
215
216
}
217