Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Alphabet | |
100.00% |
25 / 25 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
getLowerCase26Ary | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
getUpperCase26Ary | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
isValidDiffAsciiCode | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * @license MIT |
5 | * @author hazuki3417<hazuki3417@gmail.com> |
6 | * @copyright 2021 hazuki3417 all rights reserved. |
7 | */ |
8 | |
9 | namespace Selen\Str; |
10 | |
11 | use Selen\Verify\Str\Radixes\Radix; |
12 | |
13 | class Alphabet |
14 | { |
15 | /** |
16 | * アルファベットの文字数. |
17 | */ |
18 | public const NUMBER = 26; |
19 | |
20 | /** |
21 | * アルファベット大文字の開始位置(ASCIIコード). |
22 | */ |
23 | public const UPPER_CASE_ASCII_CODE = 65; |
24 | |
25 | /** |
26 | * アルファベット小文字の開始位置(ASCIIコード). |
27 | */ |
28 | public const LOWER_CASE_ASCII_CODE = 97; |
29 | |
30 | /** |
31 | * 26進数指定でアルファベット(小文字)を取得するメソッド(a-z:0-p). |
32 | */ |
33 | public static function getLowerCase26Ary(string $val): string |
34 | { |
35 | if (!Radix::verify($val, self::NUMBER)) { |
36 | throw new \InvalidArgumentException( |
37 | 'Please specify in 26-ary format.' |
38 | ); |
39 | } |
40 | |
41 | $asciiCodeDiff = base_convert($val, self::NUMBER, 10); |
42 | |
43 | if (!self::isValidDiffAsciiCode((int) $asciiCodeDiff)) { |
44 | throw new \RuntimeException( |
45 | 'Specify the value within the range of 1 digit of the 26-ary number.' |
46 | ); |
47 | } |
48 | |
49 | /** @phpstan-ignore-next-line */ |
50 | $asciiCode = self::LOWER_CASE_ASCII_CODE + $asciiCodeDiff; |
51 | |
52 | return chr($asciiCode); |
53 | } |
54 | |
55 | /** |
56 | * 26進数指定でアルファベット(大文字)を取得するメソッド(a-z:0-p). |
57 | */ |
58 | public static function getUpperCase26Ary(string $val): string |
59 | { |
60 | if (!Radix::verify($val, self::NUMBER)) { |
61 | throw new \InvalidArgumentException( |
62 | 'Please specify in 26-ary format.' |
63 | ); |
64 | } |
65 | |
66 | $asciiCodeDiff = base_convert($val, self::NUMBER, 10); |
67 | |
68 | if (!self::isValidDiffAsciiCode((int) $asciiCodeDiff)) { |
69 | throw new \RuntimeException( |
70 | 'Specify the value within the range of 1 digit of the 26-ary number.' |
71 | ); |
72 | } |
73 | |
74 | /** @phpstan-ignore-next-line */ |
75 | $asciiCode = self::UPPER_CASE_ASCII_CODE + $asciiCodeDiff; |
76 | |
77 | return chr($asciiCode); |
78 | } |
79 | |
80 | private static function isValidDiffAsciiCode(int $val): bool |
81 | { |
82 | $alphaNumRangeMin = 0; |
83 | $alphaNumRangeMax = self::NUMBER; |
84 | |
85 | return $alphaNumRangeMin <= $val && $val < $alphaNumRangeMax; |
86 | } |
87 | } |