|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dtc\GridBundle\Grid\Column; |
|
4
|
|
|
|
|
5
|
|
|
use Dtc\GridBundle\Grid\Source\GridSourceInterface; |
|
6
|
|
|
|
|
7
|
|
|
class GridColumn extends AbstractGridColumn |
|
8
|
|
|
{ |
|
9
|
|
|
protected $formatter; |
|
10
|
|
|
protected $field; |
|
11
|
|
|
protected $label; |
|
12
|
|
|
protected $searchable; |
|
13
|
|
|
protected $order; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* GridColumn constructor. |
|
17
|
|
|
* |
|
18
|
|
|
* @param $field |
|
19
|
|
|
* @param string|null $label |
|
20
|
|
|
* @param mixed $formatter |
|
21
|
|
|
* @param array|null $options |
|
22
|
|
|
* @param bool $searchable |
|
23
|
|
|
* @param int|null $order If there are columns that have an order mixed with columns of 'null' order, the null ones will appear last |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($field, |
|
26
|
|
|
$label = null, |
|
27
|
|
|
$formatter = null, |
|
28
|
|
|
array $options = null, |
|
29
|
|
|
$searchable = true, |
|
30
|
|
|
$order = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->field = $field; |
|
33
|
|
|
|
|
34
|
|
|
if (!$label) { |
|
|
|
|
|
|
35
|
|
|
$label = ucwords($field); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->label = $label; |
|
39
|
|
|
$this->formatter = $formatter; |
|
40
|
|
|
if ($options) { |
|
41
|
|
|
$this->setOptions($options); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->searchable = $searchable; |
|
45
|
|
|
|
|
46
|
|
|
$this->order = $order; |
|
47
|
|
|
|
|
48
|
|
|
if ($this->order !== null && !is_int($this->order)) { |
|
49
|
|
|
throw new \InvalidArgumentException('$order must be an integer or null'); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function isSearchable() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->searchable; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function format($object, GridSourceInterface $gridsource) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($this->formatter) { |
|
61
|
|
|
return call_user_func($this->formatter, $object, $this); |
|
62
|
|
|
} else { |
|
63
|
|
|
return $this->_format($object); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function _format($object) |
|
68
|
|
|
{ |
|
69
|
|
|
$value = null; |
|
70
|
|
|
if (is_array($object)) { |
|
71
|
|
|
if (isset($object[$this->field])) { |
|
72
|
|
|
$value = $object[$this->field]; |
|
73
|
|
|
} |
|
74
|
|
|
} elseif (is_object($object)) { |
|
75
|
|
|
$funcPrefix = array( |
|
76
|
|
|
'get', |
|
77
|
|
|
'is', |
|
78
|
|
|
'has', |
|
79
|
|
|
); |
|
80
|
|
|
foreach ($funcPrefix as $prefix) { |
|
81
|
|
|
$methodName = $prefix.$this->field; |
|
82
|
|
|
if (method_exists($object, $methodName)) { |
|
83
|
|
|
$value = $object->$methodName(); |
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
View Code Duplication |
if (is_object($value)) { |
|
|
|
|
|
|
89
|
|
|
if ($value instanceof \DateTime) { |
|
90
|
|
|
return $value->format(\DateTime::ISO8601); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return 'object: '.get_class($value); |
|
94
|
|
|
} elseif (is_scalar($value)) { |
|
95
|
|
|
return $value; |
|
96
|
|
|
} elseif (is_array($value)) { |
|
97
|
|
|
return 'array: '.print_r($value, true); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $value; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return $formatter |
|
|
|
|
|
|
105
|
|
|
*/ |
|
106
|
|
|
public function getFormatter() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->formatter; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param $formatter |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setFormatter($formatter) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->formatter = $formatter; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function toArray() |
|
120
|
|
|
{ |
|
121
|
|
|
$retVal = (array) $this; |
|
122
|
|
|
unset($retVal['formatter']); |
|
123
|
|
|
|
|
124
|
|
|
return $retVal; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: