diff --git a/src/Map/CHANGELOG.md b/src/Map/CHANGELOG.md index 160d3c886c4..df7d6551c62 100644 --- a/src/Map/CHANGELOG.md +++ b/src/Map/CHANGELOG.md @@ -4,7 +4,7 @@ - Add method `Symfony\UX\Map\Renderer\AbstractRenderer::tapOptions()`, to allow Renderer to modify options before rendering a Map. - Add `ux_map.google_maps.default_map_id` configuration to set the Google ``Map ID`` -- Add compatibility with [Live Components](https://symfony.com/bundles/ux-live-component/current/index.html), for the moment only zoom, center, markers and polygons are supported. +- Add `ComponentWithMapTrait` to ease maps integration in [Live Components](https://symfony.com/bundles/ux-live-component/current/index.html) ## 2.20 diff --git a/src/Map/doc/index.rst b/src/Map/doc/index.rst index ae5b3958afa..8274a99ad5a 100644 --- a/src/Map/doc/index.rst +++ b/src/Map/doc/index.rst @@ -323,7 +323,7 @@ Usage with Live Components To use a Map inside a Live Component, you need to use the ``ComponentWithMapTrait`` trait and implement the method ``instantiateMap`` to return a ``Map`` instance. -You can interact with the Map by using `LiveAction` +You can interact with the Map by using ``LiveAction`` attribute: .. code-block:: @@ -355,12 +355,12 @@ You can interact with the Map by using `LiveAction` } } -Then, you can render the map with ``ux_map()`` in your template: +Then, you can render the map with ``ux_map()`` in your component template: .. code-block:: html+twig - - {{ ux_map(map, { style: 'height: 300px' }) }} + + {{ ux_map(map, {style: 'height: 300px'}) }} Then, you can define `Live Actions`_ to interact with the map from the client-side. diff --git a/src/Map/src/Exception/UnableToDenormalizeOptionsException.php b/src/Map/src/Exception/UnableToDenormalizeOptionsException.php index 095dd12972c..73f5a782e9d 100644 --- a/src/Map/src/Exception/UnableToDenormalizeOptionsException.php +++ b/src/Map/src/Exception/UnableToDenormalizeOptionsException.php @@ -22,7 +22,7 @@ public function __construct(string $message) public static function missingProviderKey(string $key): self { - return new self(\sprintf('the provider key ("%s") is missing in the normalized options.', $key)); + return new self(\sprintf('the provider key "%s" is missing in the normalized options.', $key)); } public static function unsupportedProvider(string $provider, array $supportedProviders): self diff --git a/src/Map/src/Exception/UnableToNormalizeOptionsException.php b/src/Map/src/Exception/UnableToNormalizeOptionsException.php index 081888b896a..ef29c1c4945 100644 --- a/src/Map/src/Exception/UnableToNormalizeOptionsException.php +++ b/src/Map/src/Exception/UnableToNormalizeOptionsException.php @@ -23,10 +23,10 @@ public function __construct(string $message) } /** - * @param class-string $classOptions + * @param class-string $optionsClass */ - public static function unsupportedProviderClass(string $classOptions): self + public static function unsupportedProviderClass(string $optionsClass): self { - return new self(\sprintf('the class "%s" is not supported.', $classOptions)); + return new self(\sprintf('the class "%s" is not supported.', $optionsClass)); } } diff --git a/src/Map/src/Live/ComponentWithMapTrait.php b/src/Map/src/Live/ComponentWithMapTrait.php index 869d6edf1eb..cd6322c7471 100644 --- a/src/Map/src/Live/ComponentWithMapTrait.php +++ b/src/Map/src/Live/ComponentWithMapTrait.php @@ -20,6 +20,8 @@ /** * @author Hugo Alliaume + * + * @experimental */ trait ComponentWithMapTrait { @@ -34,11 +36,7 @@ abstract protected function instantiateMap(): Map; public function getMap(): Map { - if (null === $this->map) { - $this->map = $this->instantiateMap(); - } - - return $this->map; + return $this->map ??= $this->instantiateMap(); } /** diff --git a/src/Map/src/MapOptionsNormalizer.php b/src/Map/src/MapOptionsNormalizer.php index 214a25f5fa0..8233c00d71d 100644 --- a/src/Map/src/MapOptionsNormalizer.php +++ b/src/Map/src/MapOptionsNormalizer.php @@ -42,13 +42,11 @@ public static function denormalize(array $array): MapOptionsInterface throw UnableToDenormalizeOptionsException::missingProviderKey(self::KEY_PROVIDER); } - if (!isset(self::$providers[$provider])) { - throw UnableToDenormalizeOptionsException::unsupportedProvider($provider, array_keys(self::$providers)); - } - unset($array[self::KEY_PROVIDER]); - $class = self::$providers[$provider]; + if (null === $class = self::$providers[$provider] ?? null) { + throw UnableToDenormalizeOptionsException::unsupportedProvider($provider, array_keys(self::$providers)); + } return $class::fromArray($array); } diff --git a/src/Map/src/Renderer/AbstractRenderer.php b/src/Map/src/Renderer/AbstractRenderer.php index 252e3eb333e..b79d2e2c24b 100644 --- a/src/Map/src/Renderer/AbstractRenderer.php +++ b/src/Map/src/Renderer/AbstractRenderer.php @@ -84,7 +84,7 @@ final public function renderMap(Map $map, array $attributes = []): string private function getMapAttributes(Map $map): array { - $computeId = fn (array $array) => hash('xxh3', json_encode($array)); + $computeId = fn (array $array) => hash('xxh3', json_encode($array, JSON_THROW_ON_ERROR)); $attrs = $map->toArray(); diff --git a/src/Map/tests/MapOptionsNormalizerTest.php b/src/Map/tests/MapOptionsNormalizerTest.php index cbb214856e3..1348a4a464c 100644 --- a/src/Map/tests/MapOptionsNormalizerTest.php +++ b/src/Map/tests/MapOptionsNormalizerTest.php @@ -27,7 +27,7 @@ protected function tearDown(): void public function testDenormalizingWhenProviderKeyIsMissing(): void { $this->expectException(UnableToDenormalizeOptionsException::class); - $this->expectExceptionMessage(' the provider key ("@provider") is missing in the normalized options.'); + $this->expectExceptionMessage(' the provider key "@provider" is missing in the normalized options.'); MapOptionsNormalizer::denormalize([]); }