This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathNumberColumn.php
More file actions
241 lines (201 loc) · 6.19 KB
/
Copy pathNumberColumn.php
File metadata and controls
241 lines (201 loc) · 6.19 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?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\Datatable\Column;
use Sg\DatatablesBundle\Datatable\Helper;
use Symfony\Component\OptionsResolver\OptionsResolver;
class NumberColumn extends Column
{
/**
* A NumberFormatter instance.
* A required option.
*
* @var \NumberFormatter
*/
protected $formatter;
/**
* Use NumberFormatter::formatCurrency instead NumberFormatter::format to format the value.
* Default: false.
*
* @var bool
*/
protected $useFormatCurrency;
/**
* The currency code.
* Default: null => NumberFormatter::INTL_CURRENCY_SYMBOL is used.
*
* @var string|null
*/
protected $currency;
//-------------------------------------------------
// ColumnInterface
//-------------------------------------------------
/**
* {@inheritdoc}
*/
public function renderSingleField(array &$row)
{
$path = Helper::getDataPropertyPath($this->data);
if ($this->accessor->isReadable($row, $path)) {
if (true === $this->isEditableContentRequired($row)) {
$content = $this->renderTemplate($this->accessor->getValue($row, $path), $row[$this->editable->getPk()]);
} else {
$content = $this->renderTemplate($this->accessor->getValue($row, $path));
}
$this->accessor->setValue($row, $path, $content);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function renderToMany(array &$row)
{
$value = null;
$path = Helper::getDataPropertyPath($this->data, $value);
$entries = $this->accessor->getValue($row, $path);
if ($this->accessor->isReadable($row, $path)) {
if (\count($entries) > 0) {
foreach ($entries as $key => $entry) {
$currentPath = $path.'['.$key.']'.$value;
$currentObjectPath = Helper::getPropertyPathObjectNotation($path, $key, $value);
if (true === $this->isEditableContentRequired($row)) {
$content = $this->renderTemplate(
$this->accessor->getValue($row, $currentPath),
$row[$this->editable->getPk()],
$currentObjectPath
);
} else {
$content = $this->renderTemplate($this->accessor->getValue($row, $currentPath));
}
$this->accessor->setValue($row, $currentPath, $content);
}
}
// no placeholder - leave this blank
}
return $this;
}
//-------------------------------------------------
// Options
//-------------------------------------------------
/**
* @return $this
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setRequired('formatter');
$resolver->setDefaults(
[
'use_format_currency' => false,
'currency' => null,
]
);
$resolver->setAllowedTypes('formatter', ['object']);
$resolver->setAllowedTypes('use_format_currency', ['bool']);
$resolver->setAllowedTypes('currency', ['null', 'string']);
$resolver->setAllowedValues('formatter', function ($formatter) {
if (! $formatter instanceof \NumberFormatter) {
return false;
}
return true;
});
return $this;
}
//-------------------------------------------------
// Getters && Setters
//-------------------------------------------------
/**
* @return \NumberFormatter
*/
public function getFormatter()
{
return $this->formatter;
}
/**
* @return $this
*/
public function setFormatter(\NumberFormatter $formatter)
{
$this->formatter = $formatter;
return $this;
}
/**
* @return bool
*/
public function isUseFormatCurrency()
{
return $this->useFormatCurrency;
}
/**
* @param bool $useFormatCurrency
*
* @return $this
*/
public function setUseFormatCurrency($useFormatCurrency)
{
$this->useFormatCurrency = $useFormatCurrency;
return $this;
}
/**
* @return string|null
*/
public function getCurrency()
{
return $this->currency;
}
/**
* @param string|null $currency
*
* @return $this
*/
public function setCurrency($currency)
{
$this->currency = $currency;
return $this;
}
//-------------------------------------------------
// Helper
//-------------------------------------------------
/**
* Render template.
*
* @param string|null $data
* @param string|null $pk
* @param string|null $path
*
* @return mixed|string
*/
private function renderTemplate($data, $pk = null, $path = null)
{
if (true === $this->useFormatCurrency) {
if (false === \is_float($data)) {
$data = (float) $data;
}
if (null === $this->currency) {
$this->currency = $this->formatter->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL);
}
$data = $this->formatter->formatCurrency($data, $this->currency);
} elseif ($data === null) {
$data = null;
} else {
// expected number (int or float), other values will be converted to a numeric value
$data = $this->formatter->format($data);
}
return $this->twig->render(
$this->getCellContentTemplate(),
[
'data' => $data,
'column_class_editable_selector' => $this->getColumnClassEditableSelector(),
'pk' => $pk,
'path' => $path,
]
);
}
}