-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Map] Complete and simplify the normalization/denormalization process…
… of Map's value objects, add MapOptionsNormalizer
- Loading branch information
Showing
14 changed files
with
425 additions
and
47 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Map/src/Exception/UnableToDenormalizeOptionsException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Map\Exception; | ||
|
||
use Symfony\UX\Map\MapOptionsInterface; | ||
|
||
final class UnableToDenormalizeOptionsException extends LogicException | ||
{ | ||
public function __construct(string $message) | ||
{ | ||
parent::__construct(\sprintf('Unable to denormalize the map options: %s', $message)); | ||
} | ||
|
||
public static function missingProviderKey(string $key): self | ||
{ | ||
return new self(\sprintf('the provider key ("%s") is missing in the normalized options.', $key)); | ||
} | ||
|
||
public static function unsupportedProvider(string $provider, array $supportedProviders): self | ||
{ | ||
return new self(\sprintf('the provider "%s" is not supported. Supported providers are "%s".', $provider, implode('", "', $supportedProviders))); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Map/src/Exception/UnableToNormalizeOptionsException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Map\Exception; | ||
|
||
use Symfony\UX\Map\MapOptionsInterface; | ||
|
||
final class UnableToNormalizeOptionsException extends LogicException | ||
{ | ||
public function __construct(string $message) | ||
{ | ||
parent::__construct(\sprintf('Unable to normalize the map options: %s', $message)); | ||
} | ||
|
||
/** | ||
* @param class-string<MapOptionsInterface> $classOptions | ||
*/ | ||
public static function unsupportedProviderClass(string $classOptions): self | ||
{ | ||
return new self(\sprintf('the class "%s" is not supported.', $classOptions)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Map\Live; | ||
|
||
use Symfony\UX\LiveComponent\Attribute\LiveProp; | ||
use Symfony\UX\Map\Map; | ||
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; | ||
use Symfony\UX\TwigComponent\Attribute\PostMount; | ||
|
||
/** | ||
* | ||
* @author Hugo Alliaume <[email protected]> | ||
*/ | ||
trait ComponentWithMapTrait | ||
{ | ||
/** | ||
* @internal | ||
*/ | ||
#[LiveProp(hydrateWith: 'hydrateMap', dehydrateWith: 'dehydrateMap')] | ||
#[ExposeInTemplate(getter: 'getMap')] | ||
public ?Map $map = null; | ||
|
||
abstract protected function instantiateMap(): Map; | ||
|
||
public function getMap(): Map | ||
{ | ||
if (null === $this->map) { | ||
$this->map = $this->instantiateMap(); | ||
} | ||
|
||
return $this->map; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[PostMount] | ||
public function initializeMap(array $data): array | ||
{ | ||
// allow the Map object to be passed into the component() as "map" | ||
if (\array_key_exists('map', $data)) { | ||
$this->map = $data['map']; | ||
unset($data['map']); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function hydrateMap(array $data): Map | ||
{ | ||
return Map::fromArray($data); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function dehydrateMap(Map $map): array | ||
{ | ||
return $map->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Map; | ||
|
||
use Symfony\UX\Map\Bridge as MapBridge; | ||
use Symfony\UX\Map\Exception\UnableToDenormalizeOptionsException; | ||
use Symfony\UX\Map\Exception\UnableToNormalizeOptionsException; | ||
|
||
/** | ||
* Normalizes and denormalizes map options. | ||
* | ||
* @internal | ||
* @author Hugo Alliaume <[email protected]> | ||
*/ | ||
final class MapOptionsNormalizer | ||
{ | ||
private const string KEY_PROVIDER = '@provider'; | ||
|
||
/** | ||
* @var array<string, class-string<MapOptionsInterface>> | ||
*/ | ||
public static array $providers = [ | ||
'google' => MapBridge\Google\GoogleOptions::class, | ||
'leaflet' => MapBridge\Leaflet\LeafletOptions::class, | ||
]; | ||
|
||
public static function denormalize(array $array): MapOptionsInterface | ||
{ | ||
if (null === ($provider = $array[self::KEY_PROVIDER] ?? null)) { | ||
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]; | ||
|
||
return $class::fromArray($array); | ||
} | ||
|
||
public static function normalize(MapOptionsInterface $options): array | ||
{ | ||
$provider = array_search($options::class, self::$providers, true); | ||
if (!\is_string($provider)) { | ||
throw UnableToNormalizeOptionsException::unsupportedProviderClass($options::class); | ||
} | ||
|
||
$array = $options->toArray(); | ||
$array[self::KEY_PROVIDER] = $provider; | ||
|
||
return $array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Map\Tests; | ||
|
||
use Symfony\UX\Map\MapOptionsInterface; | ||
use Symfony\UX\Map\MapOptionsNormalizer; | ||
|
||
final readonly class DummyOptions implements MapOptionsInterface | ||
{ | ||
public function __construct( | ||
private string $mapId, | ||
private string $mapType, | ||
) { | ||
} | ||
|
||
public static function registerToNormalizer(): void | ||
{ | ||
MapOptionsNormalizer::$providers['dummy'] = self::class; | ||
} | ||
|
||
public static function unregisterFromNormalizer(): void | ||
{ | ||
unset(MapOptionsNormalizer::$providers['dummy']); | ||
} | ||
|
||
public static function fromArray(array $array): MapOptionsInterface | ||
{ | ||
return new self( | ||
$array['mapId'], | ||
$array['mapType'], | ||
); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'mapId' => $this->mapId, | ||
'mapType' => $this->mapType, | ||
]; | ||
} | ||
} |
Oops, something went wrong.