Completed
Push — master ( 6e2392...a3beb7 )
by Никита
06:30
created

ArraysDB::get_array()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 9.576

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 21
cts 26
cp 0.8077
rs 4.909
c 0
b 0
f 0
cc 9
eloc 24
nc 7
nop 1
crap 9.576
1
<?php
2
3
    namespace NokitaKaze\OrthogonalArrays;
4
5
    abstract class ArraysDB {
6
        private static $_db = null;
7
8 45
        private static function init_db() {
9 45
            if (!is_null(self::$_db)) {
10 40
                return;
11
            }
12
13 5
            self::$_db = unserialize(file_get_contents(__DIR__.'/precomputed.dat'));
14 5
        }
15
16
        /**
17
         * @param integer[] $geometry
18
         *
19
         * @return integer[][]|null
20
         * @throws OrthogonalArraysException
21
         */
22 45
        public static function get_array($geometry) {
23 45
            self::init_db();
24 45
            $count = count($geometry);
25 45
            if (!isset(self::$_db[$count])) {
26
                return null;
27
            }
28
29 45
            $hash = implode('-', $geometry);
30 45
            if (!isset(self::$_db[$count][$hash])) {
31
                return null;
32
            }
33
34 45
            $indexes = explode(',', self::$_db[$count][$hash]);
35 45
            $pairs = [];
36 45
            foreach ($indexes as $s) {
37 45
                $value = [];
38 45
                while (!empty($s)) {
39 45
                    $c = substr($s, 0, 1);
40 45
                    $s = substr($s, 1);
41 45
                    $ord = ord($c);
42 45
                    if (($ord >= ord('a')) and ($ord <= ord('z'))) {
43 45
                        $value[] = $ord - ord('a');
44 18
                    } elseif (($ord >= ord('A')) and ($ord <= ord('Z'))) {
45
                        $value[] = $ord - ord('A') + 26;
46
                    } else {
47
                        throw new OrthogonalArraysException();
48
                    }
49 18
                }
50 45
                $pairs[] = $value;
51 18
            }
52
53 45
            return $pairs;
54
        }
55
56
    }
57
58
?>