Completed
Pull Request — master (#89)
by thomas
16:20
created

Util::normalizeNetwork()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 6.0016

Importance

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