mmucklo /
DtcGridBundle
| 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 bool $searchable |
||||
| 22 | * @param int|null $order If there are columns that have an order mixed with columns of 'null' order, the null ones will appear last |
||||
| 23 | */ |
||||
| 24 | public function __construct( |
||||
| 25 | $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 (null !== $this->order && !is_int($this->order)) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 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 = [ |
||||
| 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 | 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); |
||||
|
0 ignored issues
–
show
Are you sure
print_r($value, true) of type string|true can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 98 | } |
||||
| 99 | |||||
| 100 | return $value; |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | /** |
||||
| 104 | * @return $formatter |
||||
|
0 ignored issues
–
show
|
|||||
| 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 |