CodePage   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 32
dl 0
loc 79
ccs 30
cts 30
cp 1
rs 10
c 2
b 0
f 1
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLetterArr() 0 9 2
A getRangeStr() 0 27 6
A getRange() 0 14 1
1
<?php
2
3
/**
4
 * User: onnov
5
 * Date: 02.09.2019
6
 * Time: 18:25
7
 */
8
9
declare(strict_types=1);
10
11
namespace Onnov\DetectEncoding;
12
13
/**
14
 * Class CodePage
15
 *
16
 * @package Onnov\DetectEncoding
17
 */
18
class CodePage
19
{
20
    /**
21
     * Method to get a custom encoding range
22
     *
23
     * @param string $uppercaseLetters
24
     * @param string $lowercaseLetters
25
     * @param string $encoding
26
     *
27
     * @return array<string, array<string, string>>
28
     */
29 1
    public function getRange(
30
        string $uppercaseLetters,
31
        string $lowercaseLetters,
32
        string $encoding
33
    ): array {
34
        return [
35
            $encoding => [
36 1
                'upper' => $this->getRangeStr($this->getLetterArr(
37 1
                    $uppercaseLetters,
38
                    $encoding
39
                )),
40 1
                'lower' => $this->getRangeStr($this->getLetterArr(
41 1
                    $lowercaseLetters,
42
                    $encoding
43
                )),
44
            ],
45
        ];
46
    }
47
48
    /**
49
     * @param array<int, int|string> $array
50
     *
51
     * @return string
52
     */
53 1
    private function getRangeStr(array $array): string
54
    {
55 1
        $ranges = [];
56 1
        $last = null;
57 1
        foreach ($array as $current) {
58 1
            if ($current > $last + 1) {
59 1
                $lastKey = array_key_last($ranges);
60 1
                if (null !== $lastKey) {
61 1
                    $ranges[$lastKey][1] = $last;
62
                }
63 1
                $ranges[] = [$current, null];
64
            }
65 1
            $last = (int)$current;
66
        }
67 1
        $lastKey = array_key_last($ranges);
68 1
        $ranges[$lastKey][1] = $last;
69
70 1
        $stringIntervals = [];
71 1
        foreach ($ranges as $interval) {
72 1
            if (current($interval) < end($interval)) {
73 1
                $stringIntervals[] = implode('-', $interval);
74 1
                continue;
75
            }
76 1
            $stringIntervals[] = array_pop($interval);
77
        }
78
79 1
        return implode(', ', $stringIntervals);
80
    }
81
82
    /**
83
     * @param string $strLetters
84
     * @param string $encoding
85
     *
86
     * @return array<int, int|string>
87
     */
88 1
    private function getLetterArr(string &$strLetters, string $encoding): array
89
    {
90 1
        $res = [];
91 1
        $str = iconv('utf-8', $encoding . '//IGNORE', $strLetters);
92 1
        if (is_string($str)) {
0 ignored issues
show
introduced by
The condition is_string($str) is always true.
Loading history...
93 1
            $res = array_keys(count_chars($str, 1));
94
        }
95
96 1
        return $res;
97
    }
98
}
99