Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Week
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
5 / 5
28
100.00% covered (success)
100.00%
1 / 1
 checkWeekId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 enWeekStrToWeekId
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 jpWeekStrToWeekId
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 toJp
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
10
 toEn
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
10
1<?php
2
3/**
4 * @license MIT
5 * @author hazuki3417<hazuki3417@gmail.com>
6 * @copyright 2021 hazuki3417 all rights reserved.
7 */
8
9namespace Selen\Str\Date;
10
11/**
12 * 日本語・英語の曜日を相互変換するクラス.
13 */
14class Week
15{
16    /**
17     * NOTE: 相互変換フロー
18     *       英語  > 数値(weekId) > 日本語
19     *       日本語 > 数値(weekId) > 英語.
20     */
21    public const EN_LONG_NAMES = [
22        'Sunday',
23        'Monday',
24        'Tuesday',
25        'Wednesday',
26        'Thursday',
27        'Friday',
28        'Saturday',
29    ];
30
31    public const EN_SHORT_NAMES = [
32        'Sun',
33        'Mon',
34        'Tue',
35        'Wed',
36        'Thu',
37        'Fri',
38        'Sat',
39    ];
40
41    public const JP_LONG_NAMES = [
42        '日曜日',
43        '月曜日',
44        '火曜日',
45        '水曜日',
46        '木曜日',
47        '金曜日',
48        '土曜日',
49    ];
50
51    public const JP_SHORT_NAMES = [
52        '日',
53        '月',
54        '火',
55        '水',
56        '木',
57        '金',
58        '土',
59    ];
60
61    /**
62     * 曜日IDの妥当性を確認します.
63     *
64     * @return bool 有効な場合はtrueを、それ以外の場合はfalseを返します
65     */
66    public static function checkWeekId(int $weekId)
67    {
68        $startWeekId = 0;
69        $endWeekId   = 6;
70
71        return $startWeekId <= $weekId && $weekId <= $endWeekId;
72    }
73
74    /**
75     * 曜日文字列(en)を曜日IDに変換します.
76     *
77     * @return int 対応する曜日IDを返します。対応する曜日IDがない場合は-1を返します
78     */
79    public static function enWeekStrToWeekId(string $weekEnName)
80    {
81        $longId = array_search($weekEnName, self::EN_LONG_NAMES, true);
82
83        if ($longId !== false) {
84            return $longId;
85        }
86
87        $shortId = array_search($weekEnName, self::EN_SHORT_NAMES, true);
88
89        if ($shortId !== false) {
90            return $shortId;
91        }
92
93        return -1;
94    }
95
96    /**
97     * 曜日文字列(jp)を曜日IDに変換します.
98     *
99     * @return int 対応する曜日IDを返します。対応する曜日IDがない場合は-1を返します
100     */
101    public static function jpWeekStrToWeekId(string $weekJpName)
102    {
103        $longId = array_search($weekJpName, self::JP_LONG_NAMES, true);
104
105        if ($longId !== false) {
106            return $longId;
107        }
108
109        $shortId = array_search($weekJpName, self::JP_SHORT_NAMES, true);
110
111        if ($shortId !== false) {
112            return $shortId;
113        }
114
115        return -1;
116    }
117
118    /**
119     * 英語から日本語に変換します.
120     *
121     * @return string
122     *
123     * @param mixed $week
124     */
125    public static function toJp($week, string $format)
126    {
127        $isInteger        = is_int($week);
128        $isString         = is_string($week);
129        $allowArgWeekType = $isInteger || $isString;
130
131        if (!$allowArgWeekType) {
132            throw new \InvalidArgumentException('型が不正');
133        }
134
135        $weekId = 0;
136
137        if ($isInteger) {
138            if (!self::checkWeekId($week)) {
139                throw new \InvalidArgumentException('値が不正');
140            }
141            $weekId = $week;
142        }
143
144        if ($isString) {
145            $weekId = self::enWeekStrToWeekId($week);
146
147            if ($weekId < 0) {
148                throw new \InvalidArgumentException('値が不正');
149            }
150        }
151
152        switch (true) {
153            case $format === 'l':
154                return self::JP_LONG_NAMES[$weekId];
155            case $format === 'D':
156                return self::JP_SHORT_NAMES[$weekId];
157            default:
158                break;
159        }
160
161        throw new \InvalidArgumentException('値が不正');
162    }
163
164    /**
165     * 日本語から英語に変換します.
166     *
167     * @return string
168     *
169     * @param mixed $week
170     */
171    public static function toEn($week, string $format)
172    {
173        $isInteger        = is_int($week);
174        $isString         = is_string($week);
175        $allowArgWeekType = $isInteger || $isString;
176
177        if (!$allowArgWeekType) {
178            throw new \InvalidArgumentException('型が不正');
179        }
180
181        $weekId = 0;
182
183        if ($isInteger) {
184            if (!self::checkWeekId($week)) {
185                throw new \InvalidArgumentException('値が不正');
186            }
187            $weekId = $week;
188        }
189
190        if ($isString) {
191            $weekId = self::jpWeekStrToWeekId($week);
192
193            if ($weekId < 0) {
194                throw new \InvalidArgumentException('値が不正');
195            }
196        }
197
198        switch (true) {
199            case $format === 'l':
200                return self::EN_LONG_NAMES[$weekId];
201            case $format === 'D':
202                return self::EN_SHORT_NAMES[$weekId];
203            default:
204                break;
205        }
206
207        throw new \InvalidArgumentException('値が不正');
208    }
209}