From 5aca6a5eef938b1fdedb4d14f5f7edcac91b5215 Mon Sep 17 00:00:00 2001 From: Li Zhineng Date: Mon, 28 Apr 2025 17:20:39 +0800 Subject: build data from raw html --- src/Build/CodeToNameHandler.php | 27 +++++++++ src/Build/RegionHandler.php | 13 ++++ src/Build/RelationshipHandler.php | 122 ++++++++++++++++++++++++++++++++++++++ src/Build/TopLevelHandler.php | 31 ++++++++++ 4 files changed, 193 insertions(+) create mode 100644 src/Build/CodeToNameHandler.php create mode 100644 src/Build/RegionHandler.php create mode 100644 src/Build/RelationshipHandler.php create mode 100644 src/Build/TopLevelHandler.php (limited to 'src/Build') diff --git a/src/Build/CodeToNameHandler.php b/src/Build/CodeToNameHandler.php new file mode 100644 index 0000000..6aaf7a5 --- /dev/null +++ b/src/Build/CodeToNameHandler.php @@ -0,0 +1,27 @@ + + */ + private array $data = []; + + public function handle(string $code, string $name): void + { + $this->data['_'.$code] = $name; + } + + public function export(string $destination): void + { + $exported = VarExporter::export($this->data); + + file_put_contents($destination, ' + */ + private array $data = []; + + public function handle(string $code, string $name): void + { + if ($this->belongsToGroup($code)) { + $this->data[$this->groupKey($code)] = []; + } + + if ($this->isProvince($code)) { + return; + } + + $group = $this->resolveGroupFor($code); + + $this->data[$group][] = (int) $code; + } + + private function belongsToGroup(string $code): bool + { + return $this->isProvince($code) + || $this->isCity($code); + } + + private function isProvince(string $code): bool + { + return substr($code, -4) === '0000'; + } + + private function groupKey(string $code): string + { + return '_'.$code; + } + + private function isCity(string $code): bool + { + return substr($code, -2) === '00'; + } + + private function isDistrict(string $code): bool + { + return substr($code, -2) !== '00'; + } + + private function resolveGroupFor(string $code): string + { + if ($this->isDistrict($code)) { + return $this->resolveGroupForDistrict($code); + } + + return $this->resolveGroupForCity($code); + } + + private function resolveGroupForDistrict(string $code): string + { + if ($this->hasCityGroup($code)) { + return $this->groupKey($this->cityKey($code)); + } + + if ($this->hasProvinceGroup($code)) { + return $this->groupKey($this->provinceKey($code)); + } + + throw new \InvalidArgumentException(sprintf( + 'Could not resolve the group for key [%s].', $code + )); + } + + private function resolveGroupForCity(string $code): string + { + if ($this->hasProvinceGroup($code)) { + return $this->groupKey($this->provinceKey($code)); + } + + throw new \InvalidArgumentException(sprintf( + 'Could not resolve the group for key [%s].', $code + )); + } + + private function hasCityGroup(string $code): bool + { + $key = $this->groupKey($this->cityKey($code)); + + return isset($this->data[$key]); + } + + private function cityKey(string $code): string + { + return substr($code, 0, 4).'00'; + } + + private function hasProvinceGroup(string $code): bool + { + $key = $this->groupKey($this->provinceKey($code)); + + return isset($this->data[$key]); + } + + private function provinceKey(string $code): string + { + return substr($code, 0, 2).'0000'; + } + + public function export(string $destination): void + { + $exported = VarExporter::export($this->data); + + file_put_contents($destination, 'data[] = (int) $code; + } + + public function export(string $destination): void + { + $exported = VarExporter::export($this->data); + + file_put_contents($destination, '