Skip to content

Commit ccf3d36

Browse files
committed
Throw an exception if bcmath is not installed
Closes #7
1 parent 9520191 commit ccf3d36

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

src/AddressValidator.php

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,37 @@ class AddressValidator
77
private $includeTestnet = false;
88
private $onlyTestnet = false;
99

10+
public function __construct()
11+
{
12+
if (extension_loaded('bcmath') === false) {
13+
throw new \RuntimeException(
14+
'The required BCMath extension is missing. Please install it to use this package.'
15+
);
16+
}
17+
}
18+
1019
/**
1120
* Allow both mainnet and testnet addresses.
12-
*
13-
* @return AddressValidator
1421
*/
15-
public function includeTestnet()
22+
public function includeTestnet(): AddressValidator
1623
{
1724
$this->includeTestnet = true;
1825
return $this;
1926
}
2027

2128
/**
2229
* Allow only testnet addresses.
23-
*
24-
* @return AddressValidator
2530
*/
26-
public function onlyTestnet()
31+
public function onlyTestnet(): AddressValidator
2732
{
2833
$this->onlyTestnet = true;
2934
return $this;
3035
}
3136

3237
/**
3338
* Validates a given address.
34-
*
35-
* @param string $address
36-
* @return boolean
3739
*/
38-
public function isValid($address)
40+
public function isValid(string $address): bool
3941
{
4042
if ($this->isPayToPublicKeyHash($address)) {
4143
return true;
@@ -58,53 +60,46 @@ public function isValid($address)
5860

5961
/**
6062
* Validates a P2PKH address.
61-
*
62-
* @param string $address
63-
* @return boolean
6463
*/
65-
public function isPayToPublicKeyHash($address)
64+
public function isPayToPublicKeyHash(string $address): bool
6665
{
6766
$prefix = $this->onlyTestnet ? 'nm' : ($this->includeTestnet ? '1nm' : '1');
6867
$expr = sprintf('/^[%s][a-km-zA-HJ-NP-Z1-9]{25,34}$/', $prefix);
6968

7069
if (preg_match($expr, $address) === 1) {
7170
try {
72-
$base58 = new Base58;
73-
return $base58->verify($address);
71+
return (new Base58)->verify($address);
7472
} catch (\Throwable $th) {
7573
return false;
7674
}
7775
}
76+
77+
return false;
7878
}
7979

8080
/**
8181
* Validates a P2SH (segwit) address.
82-
*
83-
* @param string $address
84-
* @return boolean
8582
*/
86-
public function isPayToScriptHash($address)
83+
public function isPayToScriptHash(string $address): bool
8784
{
8885
$prefix = $this->onlyTestnet ? '2' : ($this->includeTestnet ? '23' : '3');
8986
$expr = sprintf('/^[%s][a-km-zA-HJ-NP-Z1-9]{25,34}$/', $prefix);
9087

9188
if (preg_match($expr, $address) === 1) {
9289
try {
93-
$base58 = new Base58;
94-
return $base58->verify($address);
90+
return (new Base58)->verify($address);
9591
} catch (\Throwable $th) {
9692
return false;
9793
}
9894
}
95+
96+
return false;
9997
}
10098

10199
/**
102100
* Validates a P2TR (taproot) address.
103-
*
104-
* @param string $address
105-
* @return boolean
106101
*/
107-
public function isPayToTaproot($address)
102+
public function isPayToTaproot(string $address): bool
108103
{
109104
if (in_array(substr($address, 0, 4), ['bc1p', 'bcrt1p', 'tb1p']) === false) {
110105
return false;
@@ -131,11 +126,8 @@ public function isPayToTaproot($address)
131126

132127
/**
133128
* Validates a bech32 (native segwit) address.
134-
*
135-
* @param string $address
136-
* @return boolean
137129
*/
138-
public function isBech32($address)
130+
public function isBech32(string $address): bool
139131
{
140132
$prefix = $this->onlyTestnet ? 'tb' : ($this->includeTestnet ? 'bc|tb' : 'bc');
141133
$expr = sprintf(

0 commit comments

Comments
 (0)