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

Util::parseApiNetwork()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 8
nop 2
dl 0
loc 27
ccs 15
cts 15
cp 1
crap 6
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 93
    public static function parseApiNetwork($network, $testnet) {
30 93
        $network = strtoupper($network);
31
32
        // deal with testnet special cases
33 93
        if (strlen($network) === 4 && $network[0] === 'T') {
34 4
            $testnet = true;
35 4
            $network = substr($network, 1);
36
        }
37
38 93
        if (strlen($network) === 3) {
39
            // work out apiNetwork
40 93
            $apiNetwork = $network;
41 93
            if ($testnet) {
42 93
                $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 93
        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 93
    public static function normalizeNetwork($network, $testnet) {
66 93
        $network = strtolower($network);
67
68 93
        switch (strtolower($network)) {
69 93
            case 'btc':
70 11
                $name = 'bitcoin';
71 11
                $params = NetworkFactory::bitcoin();
72 11
                break;
73
74 93
            case 'tbtc':
75 93
                $name = 'bitcoin';
76 93
                $params = NetworkFactory::bitcoinTestnet();
77 93
                $testnet = true;
78 93
                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 93
        return new NetworkParams($network, $name, $testnet, $params);
103
    }
104
105 22
    public static function arrayMapWithIndex(callable $fn, $arr) {
106 22
        $result = [];
107 22
        $assoc = null;
108 22
        foreach ($arr as $idx => $value) {
109 22
            list($newidx, $newvalue) = $fn($idx, $value);
110
111 22
            if ($assoc === null) {
112 22
                $assoc = $newidx !== null;
113
            }
114
115 22
            if ($newidx === null && $assoc || $newidx !== null && !$assoc) {
116
                throw new \Exception("Mix of non assoc and assoc keys");
117
            }
118
119 22
            if ($assoc) {
120 22
                $result[$newidx] = $newvalue;
121
            } else {
122 22
                $result[] = $newvalue;
123
            }
124
        }
125
126 22
        return $result;
127
    }
128
}
129