Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Apr 27, 2024
2 parents 11aa161 + 343117e commit 8508d34
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 203 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

- Added markdown configuration file.

## [2.20.12] - 2024-04-22

- Removed `orklah/psalm-*` plugins.
- Replaced engine string values by `Engine` enumeration.

## [2.20.11] - 2024-04-18

- Modified the constructor.
Expand Down Expand Up @@ -140,6 +145,7 @@ as possible. See [Security Advisory: ZF2014-01](https://framework.zend.com/secur
- Initial release

[Unreleased]: https://github.com/laurentmuller/HighchartsBundle/compare/1.7...HEAD
[2.20.12]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.11...2.20.12
[2.20.11]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.10...2.20.11
[2.20.10]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.9...2.20.10
[2.20.9]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.8...2.20.9
Expand Down
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"slam/phpstan-extensions": "^6.0",
"orklah/psalm-insane-comparison": "^2.0",
"orklah/psalm-not-empty": "^1.0",
"orklah/psalm-strict-equality": "^2.0",
"spaze/phpstan-disallowed-calls": "^2.0",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-strict-rules": "^1.5",
Expand Down
163 changes: 1 addition & 162 deletions composer.lock

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

26 changes: 7 additions & 19 deletions src/Highcharts/AbstractChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ public static function createExpression(string $expression): Expr
return new Expr($expression);
}

/**
* @psalm-param ChartInterface::ENGINE_* $engine
*/
public function render(string $engine = self::ENGINE_JQUERY): string
public function render(Engine $engine = Engine::JQUERY): string
{
$chartJS = '';
$this->renderChartStart($chartJS, $engine);
Expand Down Expand Up @@ -174,13 +171,10 @@ protected function renderChartCommon(string &$chartJS): void
$this->renderAccessibility($chartJS);
}

/**
* @psalm-param ChartInterface::ENGINE_* $engine
*/
protected function renderChartEnd(string &$chartJS, string $engine): void
protected function renderChartEnd(string &$chartJS, Engine $engine): void
{
$chartJS = \rtrim($chartJS, self::END_LINE) . self::NEW_LINE . self::HALF_SPACE . '});' . self::NEW_LINE;
if (self::ENGINE_NONE !== $engine) {
if (Engine::NONE !== $engine) {
$chartJS .= '});' . self::NEW_LINE;
}
}
Expand All @@ -189,10 +183,7 @@ protected function renderChartOptions(string &$chartJS): void
{
}

/**
* @psalm-param ChartInterface::ENGINE_* $engine
*/
protected function renderChartStart(string &$chartJS, string $engine): void
protected function renderChartStart(string &$chartJS, Engine $engine): void
{
$this->renderEngine($chartJS, $engine);
$this->renderOptions($chartJS);
Expand All @@ -209,14 +200,11 @@ protected function renderCredits(string &$chartJS): void
$chartJS .= $this->jsonEncode($this->credits);
}

/**
* @psalm-param ChartInterface::ENGINE_* $engine
*/
protected function renderEngine(string &$chartJS, string $engine): void
protected function renderEngine(string &$chartJS, Engine $engine): void
{
$chartJS .= match ($engine) {
self::ENGINE_MOOTOOLS => 'window.addEvent(\'domready\', function () {',
self::ENGINE_JQUERY => '$(function () {',
Engine::MOOTOOLS => 'window.addEvent(\'domready\', function () {',
Engine::JQUERY => '$(function () {',
default => '',
};
}
Expand Down
10 changes: 1 addition & 9 deletions src/Highcharts/ChartInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,8 @@
*/
interface ChartInterface
{
public const ENGINE_JQUERY = 'jquery';

public const ENGINE_MOOTOOLS = 'mootools';

public const ENGINE_NONE = '';

/**
* Render this chart for the given engine.
*
* @psalm-param self::ENGINE_* $engine
*/
public function render(string $engine): string;
public function render(Engine $engine): string;
}
34 changes: 34 additions & 0 deletions src/Highcharts/Engine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/*
* This file is part of the HighchartsBundle package.
*
* (c) bibi.nu <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace HighchartsBundle\Highcharts;

/**
* The render engine enumeration.
*
* @see ChartInterface::render()
*/
enum Engine: string
{
/**
* The JQuery engine.
*/
case JQUERY = 'jquery';
/**
* The MooTools engine.
*/
case MOOTOOLS = 'mootools';
/**
* No engine.
*/
case NONE = '';
}
22 changes: 20 additions & 2 deletions src/Twig/HighchartsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
namespace HighchartsBundle\Twig;

use HighchartsBundle\Highcharts\ChartInterface;
use HighchartsBundle\Highcharts\Engine;
use Twig\Error\SyntaxError;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

Expand All @@ -24,10 +26,14 @@ class HighchartsExtension extends AbstractExtension
/**
* Render the given chart with the given engine.
*
* @psalm-param ChartInterface::ENGINE_* $engine
* @throws SyntaxError if the engine is a string and the corresponding enumeration cannot be found
*/
public function chart(ChartInterface $chart, string $engine = ChartInterface::ENGINE_JQUERY): string
public function chart(ChartInterface $chart, Engine|string $engine = Engine::JQUERY): string
{
if (\is_string($engine)) {
$engine = $this->parseEngine($engine);
}

return $chart->render($engine);
}

Expand All @@ -37,4 +43,16 @@ public function getFunctions(): array
new TwigFunction('chart', $this->chart(...), ['is_safe' => ['html']]),
];
}

/**
* @throws SyntaxError
*/
private function parseEngine(string $engine): Engine
{
try {
return Engine::from($engine);
} catch (\ValueError $e) {
throw new SyntaxError("Invalid chart engine: \"$engine\".", previous: $e);
}
}
}
11 changes: 6 additions & 5 deletions tests/AbstractChartTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
namespace HighchartsBundle\Tests;

use HighchartsBundle\Highcharts\ChartInterface;
use HighchartsBundle\Highcharts\Engine;
use PHPUnit\Framework\TestCase;

abstract class AbstractChartTestCase extends TestCase
{
/**
* @psalm-param ChartInterface::ENGINE_* $engine
*/
protected static function assertChartMatchesRegularExpression(ChartInterface $chart, string $regex, string $engine = 'jquery'): void
{
protected static function assertChartMatchesRegularExpression(
ChartInterface $chart,
string $regex,
Engine $engine = Engine::JQUERY
): void {
$result = $chart->render($engine);
self::assertMatchesRegularExpression($regex, $result);
}
Expand Down
Loading

0 comments on commit 8508d34

Please sign in to comment.