forked from stwe/DatatablesBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatatableFormatter.php
More file actions
154 lines (133 loc) · 5.18 KB
/
Copy pathDatatableFormatter.php
File metadata and controls
154 lines (133 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* This file is part of the SgDatatablesBundle package.
*
* (c) stwe <https://github.com/stwe/DatatablesBundle>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sg\DatatablesBundle\Response;
use Sg\DatatablesBundle\Datatable\Column\ColumnInterface;
use Sg\DatatablesBundle\Datatable\DatatableInterface;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
/**
* Class DatatableFormatter
*
* @package Sg\DatatablesBundle\Response
*/
class DatatableFormatter
{
/**
* The output array.
*
* @var array
*/
private $output;
/**
* The PropertyAccessor.
* Provides functions to read and write from/to an object or array using a simple string notation.
*
* @var PropertyAccessor
*/
private $accessor;
//-------------------------------------------------
// Ctor.
//-------------------------------------------------
/**
* DatatableFormatter constructor.
*/
public function __construct()
{
$this->output = array('data' => array());
$this->accessor = PropertyAccess::createPropertyAccessor();
}
//-------------------------------------------------
// Formatter
//-------------------------------------------------
/**
* Create the output array.
*
* @param Paginator $paginator
* @param DatatableInterface $datatable
*/
public function runFormatter(Paginator $paginator, DatatableInterface $datatable)
{
$lineFormatter = $datatable->getLineFormatter();
$columns = $datatable->getColumnBuilder()->getColumns();
foreach ($paginator as $row) {
// Adding custom DQL fields make PARTIAL columns stored in key 0
if (isset($row[0])) {
$row = array_merge($row, $row[0]);
unset($row[0]);
}
// Format custom DQL fields output ('custom.dql.name' => $row['custom']['dql']['name'] = 'value')
foreach ($columns as $column) {
/** @noinspection PhpUndefinedMethodInspection */
if (true === $column->isCustomDql()) {
/** @noinspection PhpUndefinedMethodInspection */
$columnAlias = str_replace('.', '_', $column->getData());
/** @noinspection PhpUndefinedMethodInspection */
$columnPath = '['.str_replace('.', '][', $column->getData()).']';
/** @noinspection PhpUndefinedMethodInspection */
if ($columnAlias !== $column->getData()) {
$this->accessor->setValue($row, $columnPath, $row[$columnAlias]);
unset($row[$columnAlias]);
}
}
}
// 1. Set (if necessary) the custom data source for the Columns with a 'data' option
foreach ($columns as $column) {
/** @noinspection PhpUndefinedMethodInspection */
$dql = $column->getDql();
/** @noinspection PhpUndefinedMethodInspection */
$data = $column->getData();
/** @noinspection PhpUndefinedMethodInspection */
if (false === $column->isAssociation()) {
if (null !== $dql && $dql !== $data && false === array_key_exists($data, $row)) {
$row[$data] = $row[$dql];
unset($row[$dql]);
}
}
else if(false === $this->accessor->isReadable($row, $path))
{
// Set empty values for the path of an association without object
$pathParts = explode('.', $column->getData());
$buildPath = '';
foreach($pathParts as $part)
{
if(false === $this->accessor->isReadable($row, $buildPath.'['.$part.']'))
$this->accessor->setValue($row, $buildPath, array($part => null));
$buildPath .= '['.$part.']';
}
}
}
// 2. Call the the lineFormatter to format row items
if (null !== $lineFormatter && is_callable($lineFormatter)) {
$row = call_user_func($datatable->getLineFormatter(), $row);
}
/** @var ColumnInterface $column */
foreach ($columns as $column) {
// 3. Add some special data to the output array. For example, the visibility of actions.
$column->addDataToOutputArray($row);
// 4. Call Columns renderContent method to format row items (e.g. for images or boolean values)
$column->renderCellContent($row);
}
$this->output['data'][] = $row;
}
}
//-------------------------------------------------
// Getters && Setters
//-------------------------------------------------
/**
* Get output.
*
* @return array
*/
public function getOutput()
{
return $this->output;
}
}