summaryrefslogtreecommitdiff
path: root/src/Build/RelationshipHandler.php
blob: 33f8618ea4295be033218ab84550a40018f76c82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

declare(strict_types=1);

namespace Zhineng\Region\Build;

use Symfony\Component\VarExporter\VarExporter;

final class RelationshipHandler implements RegionHandler
{
    /**
     * @var array<string, int[]>
     */
    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, '<?php return '.$exported.';');
    }
}