Completed
Pull Request — master (#85)
by thomas
15:44
created

Util   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 94.83%

Importance

Changes 0
Metric Value
dl 0
loc 113
ccs 55
cts 58
cp 0.9483
rs 10
c 0
b 0
f 0
wmc 27
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 4 2
C arrayMapWithIndex() 0 23 8
B parseApiNetwork() 0 27 6
C normalizeNetwork() 0 36 11
1
<?php
2
3
namespace Blocktrail\SDK;
4
5
abstract class Util {
6 4
    public static function all(callable $fn, array $arr) {
7 4
        $allvalues = array_map($fn, $arr);
8 4
        return count(array_unique($allvalues)) === 1 && end($allvalues) === true;
9
    }
10
11
    /**
12
     * Given a $network string and $testnet bool, this function
13
     * will apply the old method (conditionally prepending 't'
14
     * to the network if ($testnet).. but also some new special
15
     * cases which ignore the testnet setting.
16
     *
17
     * @param string $network
18
     * @param bool $testnet
19
     * @return array
20
     */
21 80
    public static function parseApiNetwork($network, $testnet) {
22 80
        $network = strtoupper($network);
23
24
        // deal with testnet special cases
25 80
        if (strlen($network) === 4 && $network[0] === 'T') {
26 4
            $testnet = true;
27 4
            $network = substr($network, 1);
28
        }
29
30 80
        if (strlen($network) === 3) {
31
            // work out apiNetwork
32 80
            $apiNetwork = $network;
33 80
            if ($testnet) {
34 80
                $apiNetwork = "t{$network}";
35
            }
36 3
        } else if ($network === "RBTC") {
37
            // Regtest is magic
38 2
            $apiNetwork = "rBTC";
39 2
            $testnet = true;
40
        } else {
41
            // Default to bitcoin if they make no sense.
42 1
            $apiNetwork = "BTC";
43 1
            $testnet = false;
44
        }
45
46 80
        return [$apiNetwork, $testnet];
47
    }
48
49
    /**
50
     * normalize network string
51
     *
52
     * @param $network
53
     * @param $testnet
54
     * @return array
55
     * @throws \Exception
56
     */
57 80
    public static function normalizeNetwork($network, $testnet) {
58 80
        switch (strtolower($network)) {
59 80
            case 'btc':
60 9
            case 'bitcoin':
61 80
                $network = 'bitcoin';
62 80
                break;
63
64 9
            case 'tbtc':
65 6
            case 'bitcoin-testnet':
66 3
                $network = 'bitcoin';
67 3
                $testnet = true;
68 3
                break;
69 6
            case 'bcc':
70 5
            case 'bitcoincash':
71 1
                $network = 'bitcoincash';
72 1
                break;
73
74 5
            case 'tbcc':
75 2
            case 'bitcoincash-testnet':
76 3
                $network = 'bitcoincash';
77 3
                $testnet = true;
78 3
                break;
79
80 2
            case 'rbtc':
81
            case 'bitcoin-regtest':
82 2
                $network = 'bitcoin';
83 2
                $testnet = true;
84 2
                break;
85
86
            default:
87
                throw new \Exception("Unknown network [{$network}]");
88
            // this comment silences a phpcs error.
89
        }
90
91 80
        return [$network, $testnet];
92
    }
93
94 18
    public static function arrayMapWithIndex(callable $fn, $arr) {
95 18
        $result = [];
96 18
        $assoc = null;
97 18
        foreach ($arr as $idx => $value) {
98 18
            list($newidx, $newvalue) = $fn($idx, $value);
99
100 18
            if ($assoc === null) {
101 18
                $assoc = $newidx !== null;
102
            }
103
104 18
            if ($newidx === null && $assoc || $newidx !== null && !$assoc) {
105
                throw new \Exception("Mix of non assoc and assoc keys");
106
            }
107
108 18
            if ($assoc) {
109 18
                $result[$newidx] = $newvalue;
110
            } else {
111 18
                $result[] = $newvalue;
112
            }
113
        }
114
115 18
        return $result;
116
    }
117
}
118