Skip to content

Commit

Permalink
Added pretty print option.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Nov 17, 2023
1 parent 8628804 commit da7bbd4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 43 deletions.
14 changes: 13 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"role": "Developer"
}
],

"require": {
"php": "^8.2",
"symfony/http-kernel": "^6.0",
Expand Down Expand Up @@ -63,5 +62,18 @@
"allow-plugins": {
"phpstan/extension-installer": true
}
},
"scripts": {
"cs-fixer": "vendor/bin/php-cs-fixer fix --diff --dry-run",
"psalm": "vendor/bin/psalm --config=psalm.xml",
"phpstan": "vendor/bin/phpstan analyse --memory-limit 2G -c phpstan.neon",
"rector": "vendor/bin/rector process --dry-run --config=rector.php",
"run-all": [
"@cs-fixer",
"@psalm",
"@phpstan",
"@rector",
"phpunit"
]
}
}
80 changes: 40 additions & 40 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions src/Highcharts/AbstractChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ abstract class AbstractChart implements ChartInterface
public ChartOption $lang;
public ChartOption $legend;
public ChartOption $plotOptions;
public bool $prettyPrint = false;
public ChartOption $scrollbar;
public array $series;
public ChartOption $subtitle;
Expand Down Expand Up @@ -119,7 +120,7 @@ protected function jsonEncode(ChartOption|array $data, string $name = ''): strin
return '';
}

$encoded = \json_encode($data);
$encoded = \json_encode($data, $this->getJsonEncodeOptions());

return self::SPACE . "$name: $encoded" . self::END_LINE;
}
Expand All @@ -140,7 +141,7 @@ protected function renderCallback(ChartOption|array $data, string $name = ''): s
}

// Zend\Json is used in place of json_encode to preserve JS anonymous functions
$encoded = Json::encode(valueToEncode: $data, options: self::ZEND_ENCODE_OPTIONS);
$encoded = Json::encode(valueToEncode: $data, options: $this->getZendEncodeOptions());

return self::SPACE . "$name: $encoded" . self::END_LINE;
}
Expand Down Expand Up @@ -278,4 +279,14 @@ protected function renderYAxis(): string
{
return $this->renderCallback($this->yAxis, 'yAxis');
}

private function getJsonEncodeOptions(): int
{
return $this->prettyPrint ? \JSON_PRETTY_PRINT : 0;
}

private function getZendEncodeOptions(): array
{
return \array_merge(self::ZEND_ENCODE_OPTIONS, ['prettyPrint' => $this->prettyPrint]);
}
}

0 comments on commit da7bbd4

Please sign in to comment.