Improved CvsFormatter to attempt to encode non-scalar variables into JSON before giving up

This commit is contained in:
Matias Griese
2020-07-01 14:51:27 +03:00
parent c060d1a36e
commit 28ce18f8f0
2 changed files with 15 additions and 0 deletions

View File

@@ -3,6 +3,8 @@
1. [](#new)
* Added new `onAfterCacheClear` event
1. [](#improved)
* Improved `CvsFormatter` to attempt to encode non-scalar variables into JSON before giving up
1. [](#bugfix)
* Fixed `MediaUploadTrait::copyUploadedFile()` not adding uploaded media to the collection
* Fixed regression in saving media to a new Flex Object [admin#1867](https://github.com/getgrav/grav-plugin-admin/issues/1867)

View File

@@ -123,6 +123,19 @@ class CsvFormatter extends AbstractFormatter
protected function encodeLine(array $line, string $delimiter): string
{
foreach ($line as $key => &$value) {
// Oops, we need to convert the line to a string.
if (!\is_scalar($value)) {
if (is_array($value) || $value instanceof \JsonSerializable || $value instanceof \stdClass) {
$value = json_encode($value);
} elseif (is_object($value)) {
if (method_exists($value, 'toJson')) {
$value = $value->toJson();
} elseif (method_exists($value, 'toArray')) {
$value = json_encode($value->toArray());
}
}
}
$value = $this->escape((string)$value);
}
unset($value);