summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLi Zhineng <[email protected]>2025-04-28 17:53:19 +0800
committerLi Zhineng <[email protected]>2025-04-28 17:53:19 +0800
commitcb8ad9567a940db0434b9ca8991422ac9d2ce795 (patch)
tree55c571e331228ec3713530fee420e5bc5c50b155 /src
parente1acfb0b977a38a8c465f3c6d5cc1aed49b79656 (diff)
downloadregion-china-cb8ad9567a940db0434b9ca8991422ac9d2ce795.tar.gz
region-china-cb8ad9567a940db0434b9ca8991422ac9d2ce795.zip
region manager
Diffstat (limited to 'src')
-rw-r--r--src/RegionException.php10
-rw-r--r--src/RegionManager.php58
2 files changed, 68 insertions, 0 deletions
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 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Zhineng\Region;
+
+final class RegionException extends \Exception
+{
+ //
+}
diff --git a/src/RegionManager.php b/src/RegionManager.php
new file mode 100644
index 0000000..9b8b969
--- /dev/null
+++ b/src/RegionManager.php
@@ -0,0 +1,58 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Zhineng\Region;
+
+final class RegionManager
+{
+ /**
+ * @param array<string, string> $codeToName
+ * @param array<string, int[]> $relationships
+ * @param array<int, int[]> $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;
+ }
+}