Completed
Pull Request — master (#88)
by thomas
05:05
created

Util::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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