Passed
Push — master ( 6753d5...2522c4 )
by Takashi
02:45
created

JapanPrefectureUtil::mbStrposOf()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.2
cc 4
eloc 7
nc 4
nop 3
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;
1 ignored issue
show
Unused Code introduced by
The property $flippedPrefectureAreaMap is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
58
59
    private static function prepareFlippedPrefectureMap()
60
    {
61
        if (self::$flippedPrefectureMap === null) {
62
            self::$flippedPrefectureMap = array_flip(self::$prefectureMap);
63
        }
64
    }
65
66
    private static function prepareFlippedAreaMap()
67
    {
68
        if (self::$flippedAreaMap === null) {
69
            self::$flippedAreaMap = array_flip(self::$areaMap);
70
        }
71
    }
72
73
    public static function convertCodeToName($prefectureCode)
74
    {
75
        if (isset(self::$prefectureMap[$prefectureCode])) {
76
            return self::$prefectureMap[$prefectureCode];
77
        }
78
        return null;
79
    }
80
81
    public static function convertNameToCode($prefectureName)
82
    {
83
        self::prepareFlippedPrefectureMap();
84
        if (isset(self::$flippedPrefectureMap[$prefectureName])) {
85
            return self::$flippedPrefectureMap[$prefectureName];
86
        }
87
        return null;
88
    }
89
90
    public static function convertAreaCodeToName($areaCode)
91
    {
92
        if (isset(self::$areaMap[$areaCode])) {
93
            return self::$areaMap[$areaCode];
94
        }
95
        return null;
96
    }
97
98
    public static function convertAreaNameToCode($areaName)
99
    {
100
        self::prepareFlippedAreaMap();
101
        if (isset(self::$flippedAreaMap[$areaName])) {
102
            return self::$flippedAreaMap[$areaName];
103
        }
104
        return null;
105
    }
106
107
    /**
108
     * @param string $heystack
109
     * @param string $needle
110
     * @param integer $adjuster
111
     */
112
    private static function mbStrposOf($heystack, $needle, $adjuster)
113
    {
114
        $mbStrposOf = mb_strpos($heystack, $needle);
115
        if ($mbStrposOf !== false) {
116
            $mbStrposOf -= $adjuster;
117
        }
118
        if ($mbStrposOf === false || $mbStrposOf < 0) {
119
            $mbStrposOf = -1;
120
        }
121
        return $mbStrposOf;
122
    }
123
124
    public static function pickFirstAsCode($address)
125
    {
126
        $address = JapanStringUtil::standardize($address);
127
        $array = [
128
            1 => self::mbStrposOf($address, '東京都', 0),
129
            2 => self::mbStrposOf($address, '北海道', 0),
130
            3 => self::mbStrposOf($address, '府', 2),
131
            4 => self::mbStrposOf($address, '県', 2),
132
        ];
133
        $array[5] = $array[4] === -1 ? -1 : $array[4] - 1;
134
        asort($array);
135
        $prefectureName = '';
1 ignored issue
show
Unused Code introduced by
$prefectureName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
136
        foreach ($array as $key => $val) {
137
            if ($val < 0) {
138
                continue;
139
            }
140
            $substrLength = $key === 5 ? 4 : 3;
141
            $prefectureName = @mb_substr($address, $val, $substrLength);
142
            $code = self::convertNameToCode($prefectureName);
143
            if ($code !== null) {
144
                return $code;
145
            }
146
        }
147
        return null;
148
    }
149
150
    /**
151
     * @param string $address
152
     */
153
    public static function pickFirst($address)
154
    {
155
        $pickedCode = self::pickFirstAsCode($address);
156
        return self::convertCodeToName($pickedCode);
157
    }
158
159
    public static function pickAll($address)
160
    {
161
        $address = JapanStringUtil::standardize($address);
162
        $list = [];
163
        while ($prefectureName = self::pickFirst($address)) {
164
            $list[] = $prefectureName;
165
            $address = str_replace($prefectureName, '', $address);
166
        }
167
        return $list;
168
    }
169
170
    public static function pickAllAsCode($address)
171
    {
172
        $list = self::pickAll($address);
173
        $codeList = [];
174
        foreach ($list as $prefectureName) {
175
            $codeList[] = self::convertNameToCode($prefectureName);
176
        }
177
        return $codeList;
178
    }
179
180
    public static function getAreaCode($prefectureNameOrCode)
181
    {
182
        if (!is_numeric($prefectureNameOrCode)) {
183
            $prefectureNameOrCode = self::convertNameToCode($prefectureNameOrCode);
184
        }
185
        return $prefectureNameOrCode === null ? null : self::$prefectureAreaMap[$prefectureNameOrCode];
186
    }
187
188
    public static function getAreaName($prefectureNameOrCode)
189
    {
190
        $areaCode = self::getAreaCode($prefectureNameOrCode);
191
        return self::convertAreaCodeToName($areaCode);
192
    }
193
}
194