1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
7
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
8
|
|
|
use Illuminate\Contracts\Support\Renderable; |
9
|
|
|
|
10
|
|
|
class Row |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Row number. |
14
|
|
|
* |
15
|
|
|
* @var |
16
|
|
|
*/ |
17
|
|
|
public $number; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Row data. |
21
|
|
|
* |
22
|
|
|
* @var |
23
|
|
|
*/ |
24
|
|
|
protected $data; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Attributes of row. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $attributes = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param $number |
37
|
|
|
* @param $data |
38
|
|
|
*/ |
39
|
|
|
public function __construct($number, $data) |
40
|
|
|
{ |
41
|
|
|
$this->number = $number; |
42
|
|
|
|
43
|
|
|
$this->data = $data; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the value of the model's primary key. |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function getKey() |
52
|
|
|
{ |
53
|
|
|
return $this->model->getKey(); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get attributes in html format. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getRowAttributes() |
62
|
|
|
{ |
63
|
|
|
return $this->formatHtmlAttribute($this->attributes); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get column attributes. |
68
|
|
|
* |
69
|
|
|
* @param string $column |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getColumnAttributes($column) |
74
|
|
|
{ |
75
|
|
|
if ($attributes = Column::getAttributes($column)) { |
76
|
|
|
return $this->formatHtmlAttribute($attributes); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return ''; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Format attributes to html. |
84
|
|
|
* |
85
|
|
|
* @param array $attributes |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
private function formatHtmlAttribute($attributes = []) |
90
|
|
|
{ |
91
|
|
|
$attrArr = []; |
92
|
|
|
foreach ($attributes as $name => $val) { |
93
|
|
|
$attrArr[] = "$name=\"$val\""; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return implode(' ', $attrArr); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set attributes. |
101
|
|
|
* |
102
|
|
|
* @param array $attributes |
103
|
|
|
*/ |
104
|
|
|
public function setAttributes(array $attributes) |
105
|
|
|
{ |
106
|
|
|
$this->attributes = $attributes; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set style of the row. |
111
|
|
|
* |
112
|
|
|
* @param array|string $style |
113
|
|
|
*/ |
114
|
|
|
public function style($style) |
115
|
|
|
{ |
116
|
|
|
if (is_array($style)) { |
117
|
|
|
$style = implode('', array_map(function ($key, $val) { |
118
|
|
|
return "$key:$val"; |
119
|
|
|
}, array_keys($style), array_values($style))); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (is_string($style)) { |
123
|
|
|
$this->attributes['style'] = $style; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get data of this row. |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function model() |
133
|
|
|
{ |
134
|
|
|
return $this->data; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Getter. |
139
|
|
|
* |
140
|
|
|
* @param mixed $attr |
141
|
|
|
* |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
public function __get($attr) |
145
|
|
|
{ |
146
|
|
|
return array_get($this->data, $attr); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get or set value of column in this row. |
151
|
|
|
* |
152
|
|
|
* @param string $name |
153
|
|
|
* @param mixed $value |
154
|
|
|
* |
155
|
|
|
* @return $this|mixed |
156
|
|
|
*/ |
157
|
|
|
public function column($name, $value = null) |
158
|
|
|
{ |
159
|
|
|
if (is_null($value)) { |
160
|
|
|
$column = array_get($this->data, $name); |
161
|
|
|
|
162
|
|
|
return $this->output($column); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ($value instanceof Closure) { |
166
|
|
|
$value = $value->call($this, $this->column($name)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
array_set($this->data, $name, $value); |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Output column value. |
176
|
|
|
* |
177
|
|
|
* @param mixed $value |
178
|
|
|
* |
179
|
|
|
* @return mixed|string |
180
|
|
|
*/ |
181
|
|
|
protected function output($value) |
182
|
|
|
{ |
183
|
|
|
if ($value instanceof Renderable) { |
184
|
|
|
$value = $value->render(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if ($value instanceof Htmlable) { |
188
|
|
|
$value = $value->toHtml(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ($value instanceof Jsonable) { |
192
|
|
|
$value = $value->toJson(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (!is_null($value) && !is_scalar($value)) { |
196
|
|
|
return sprintf('<pre>%s</pre>', var_export($value, true)); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $value; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.