From cb8ad9567a940db0434b9ca8991422ac9d2ce795 Mon Sep 17 00:00:00 2001 From: Li Zhineng Date: Mon, 28 Apr 2025 17:53:19 +0800 Subject: region manager --- src/RegionException.php | 10 +++++++++ src/RegionManager.php | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/RegionException.php create mode 100644 src/RegionManager.php (limited to 'src') diff --git a/src/RegionException.php b/src/RegionException.php new file mode 100644 index 0000000..f33adec --- /dev/null +++ b/src/RegionException.php @@ -0,0 +1,10 @@ + $codeToName + * @param array $relationships + * @param array $topLevels + */ + public function __construct( + private array $codeToName, + private array $relationships, + private array $topLevels + ) { + // + } + + public static function createFromBuiltIn(): static + { + return new self( + require __DIR__.'/../resources/code-to-name.php', + require __DIR__.'/../resources/relationships.php', + require __DIR__.'/../resources/top-levels.php' + ); + } + + public function getName(int $code): string + { + return $this->codeToName[$this->groupKey($code)] + ?? throw new RegionException('The region code does not exist.'); + } + + /** + * @return int[] + */ + public function getTopLevelNodes(): array + { + return $this->topLevels; + } + + /** + * @return int[] + */ + public function getNodes(int $code): array + { + return $this->relationships[$this->groupKey($code)] + ?? throw new RegionException('Could not find any nodes under the region code.'); + } + + private function groupKey(int $code): string + { + return '_'.$code; + } +} -- cgit v1.2.3