JapanPrefectureUtil::prepareFlippedPrefectureMap()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of JapanUtil.
5
 *
6
 * (c) Takashi OGAWA
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace NinjaAnija\JapanUtil;
13
14
/**
15
 * 日本の都道府県を扱うためのユーティリティ.
16
 *
17
 * @author Takashi OGAWA
18
 *
19
 */
20
class JapanPrefectureUtil
21
{
22
    private static $prefectureMap = [
23
        1 => '北海道',
24
        2 => '青森県', 3 => '岩手県', 4 => '宮城県', 5 => '秋田県', 6 => '山形県', 7 => '福島県',
25
        8 => '茨城県', 9 => '栃木県', 10 => '群馬県', 11 => '埼玉県', 12 => '千葉県', 13 => '東京都', 14 => '神奈川県',
26
        15 => '新潟県', 16 => '富山県', 17 => '石川県', 18 => '福井県', 19 => '山梨県', 20 => '長野県', 21 => '岐阜県', 22 => '静岡県', 23 => '愛知県',
27
        24 => '三重県', 25 => '滋賀県', 26 => '京都府', 27 => '大阪府', 28 => '兵庫県', 29 => '奈良県', 30 => '和歌山県',
28
        31 => '鳥取県', 32 => '島根県', 33 => '岡山県', 34 => '広島県', 35 => '山口県',
29
        36 => '徳島県', 37 => '香川県', 38 => '愛媛県', 39 => '高知県',
30
        40 => '福岡県', 41 => '佐賀県', 42 => '長崎県', 43 => '熊本県', 44 => '大分県', 45 => '宮崎県', 46 => '鹿児島県', 47 => '沖縄県',
31
    ];
32
33
    private static $areaMap = [
34
        1 => '北海道',
35
        2 => '東北',
36
        3 => '関東',
37
        4 => '中部',
38
        5 => '近畿',
39
        6 => '中国',
40
        7 => '四国',
41
        8 => '九州',
42
    ];
43
44
    private static $prefectureAreaMap = [
45
        1 => 1,
46
        2 => 2, 3 => 2, 4 => 2, 5 => 2, 6 => 2, 7 => 2,
47
        8 => 3, 9 => 3, 10 => 3, 11 => 3, 12 => 3, 13 => 3, 14 => 3,
48
        15 => 4, 16 => 4, 17 => 4, 18 => 4, 19 => 4, 20 => 4, 21 => 4, 22 => 4,
49
        24 => 5, 25 => 5, 26 => 5, 27 => 5, 28 => 5, 29 => 5, 30 => 5,
50
        31 => 6, 32 => 6, 33 => 6, 34 => 6,
51
        36 => 7, 37 => 7, 38 => 7, 39 => 7,
52
        40 => 8, 41 => 8, 42 => 8, 43 => 8, 44 => 8, 44 => 8, 45 => 8, 46 => 8, 48 => 8,
53
    ];
54
55
    private static $flippedPrefectureMap = null;
56
    private static $flippedAreaMap = null;
57
    private static $flippedPrefectureAreaMap = null;
58
59 7
    private static function prepareFlippedPrefectureMap()
60
    {
61 7
        if (self::$flippedPrefectureMap === null) {
62 1
            self::$flippedPrefectureMap = array_flip(self::$prefectureMap);
63 1
        }
64 7
    }
65
66 1
    private static function prepareFlippedAreaMap()
67
    {
68 1
        if (self::$flippedAreaMap === null) {
69 1
            self::$flippedAreaMap = array_flip(self::$areaMap);
70 1
        }
71 1
    }
72
73 4
    public static function convertCodeToName($prefectureCode)
74
    {
75 4
        if (isset(self::$prefectureMap[$prefectureCode])) {
76 4
            return self::$prefectureMap[$prefectureCode];
77
        }
78 4
        return null;
79
    }
80
81 7
    public static function convertNameToCode($prefectureName)
82
    {
83 7
        self::prepareFlippedPrefectureMap();
84 7
        if (isset(self::$flippedPrefectureMap[$prefectureName])) {
85 7
            return self::$flippedPrefectureMap[$prefectureName];
86
        }
87 7
        return null;
88
    }
89
90 2
    public static function convertAreaCodeToName($areaCode)
91
    {
92 2
        if (isset(self::$areaMap[$areaCode])) {
93 2
            return self::$areaMap[$areaCode];
94
        }
95 2
        return null;
96
    }
97
98 1
    public static function convertAreaNameToCode($areaName)
99
    {
100 1
        self::prepareFlippedAreaMap();
101 1
        if (isset(self::$flippedAreaMap[$areaName])) {
102 1
            return self::$flippedAreaMap[$areaName];
103
        }
104 1
        return null;
105
    }
106
107
    /**
108
     * @param string $heystack
109
     * @param string $needle
110
     * @param integer $adjuster
111
     */
112 4
    private static function mbStrposOf($heystack, $needle, $adjuster)
113
    {
114 4
        $mbStrposOf = mb_strpos($heystack, $needle);
115 4
        if ($mbStrposOf !== false) {
116 4
            $mbStrposOf -= $adjuster;
117 4
        }
118 4
        if ($mbStrposOf === false || $mbStrposOf < 0) {
119 4
            $mbStrposOf = -1;
120 4
        }
121 4
        return $mbStrposOf;
122
    }
123
124 4
    public static function pickFirstAsCode($address)
125
    {
126 4
        $address = JapanStringUtil::standardize($address);
127
        $array = [
128 4
            1 => self::mbStrposOf($address, '東京都', 0),
129 4
            2 => self::mbStrposOf($address, '北海道', 0),
130 4
            3 => self::mbStrposOf($address, '府', 2),
131 4
            4 => self::mbStrposOf($address, '県', 2),
132 4
        ];
133 4
        $array[5] = $array[4] === -1 ? -1 : $array[4] - 1;
134 4
        asort($array);
135 4
        $prefectureName = '';
136 4
        foreach ($array as $key => $val) {
137 4
            if ($val < 0) {
138 4
                continue;
139
            }
140 4
            $substrLength = $key === 5 ? 4 : 3;
141 4
            $prefectureName = @mb_substr($address, $val, $substrLength);
142 4
            $code = self::convertNameToCode($prefectureName);
143 4
            if ($code !== null) {
144 4
                return $code;
145
            }
146 4
        }
147 4
        return null;
148
    }
149
150
    /**
151
     * @param string $address
152
     */
153 3
    public static function pickFirst($address)
154
    {
155 3
        $pickedCode = self::pickFirstAsCode($address);
156 3
        return self::convertCodeToName($pickedCode);
157
    }
158
159 2
    public static function pickAll($address)
160
    {
161 2
        $address = JapanStringUtil::standardize($address);
162 2
        $list = [];
163 2
        while ($prefectureName = self::pickFirst($address)) {
164 2
            $list[] = $prefectureName;
165 2
            $address = str_replace($prefectureName, '', $address);
166 2
        }
167 2
        return $list;
168
    }
169
170 1
    public static function pickAllAsCode($address)
171
    {
172 1
        $list = self::pickAll($address);
173 1
        $codeList = [];
174 1
        foreach ($list as $prefectureName) {
175 1
            $codeList[] = self::convertNameToCode($prefectureName);
176 1
        }
177 1
        return $codeList;
178
    }
179
180 2
    public static function getAreaCode($prefectureNameOrCode)
181
    {
182 2
        if (!is_numeric($prefectureNameOrCode)) {
183 2
            $prefectureNameOrCode = self::convertNameToCode($prefectureNameOrCode);
184 2
        }
185 2
        return $prefectureNameOrCode === null ? null : self::$prefectureAreaMap[$prefectureNameOrCode];
186
    }
187
188 1
    public static function getAreaName($prefectureNameOrCode)
189
    {
190 1
        $areaCode = self::getAreaCode($prefectureNameOrCode);
191 1
        return self::convertAreaCodeToName($areaCode);
192
    }
193
}
194