Skip to content

Commit da76414

Browse files
committed
Adding those isolated functions into a conversion class and refactor where their are used.
Updating .gitignore and composer.json.
1 parent 97fff8d commit da76414

File tree

13 files changed

+99
-41
lines changed

13 files changed

+99
-41
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,6 @@ crashlytics-build.properties
7474
sftp-config.json
7575

7676

77+
### Composer stuffs
78+
vendor/
79+
composer.lock

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"license": "MIT",
77
"autoload": {
88
"psr-4": {
9-
"Blockchain": "lib/"
9+
"Blockchain\\": "src/"
1010
}
1111
},
1212
"require": {

example/explorer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<pre><?php
22

3-
require_once('../src/Blockchain.php');
3+
$sRootPath = dirname(__DIR__);
4+
var_dump($sRootPath);
5+
require_once($sRootPath . '/vendor/autoload.php');
46

57
$api_code = null;
68
if(file_exists('code.txt')) {
79
$api_code = trim(file_get_contents('code.txt'));
810
}
911

10-
$Blockchain = new Blockchain($api_code);
12+
$Blockchain = new \Blockchain\Blockchain($api_code);
1113

1214
// List all blocks at a certain height
1315
// var_dump($Blockchain->Explorer->getBlocksAtHeight(1));

src/Blockchain.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,4 @@ private function _call() {
140140

141141
return $json;
142142
}
143-
}
144-
145-
// Convert an incoming integer to a BTC string value
146-
function BTC_int2str($val) {
147-
$a = bcmul($val, "1.0", 1);
148-
return bcdiv($a, "100000000", 8);
149-
}
150-
// Convert a float value to BTC satoshi integer string
151-
function BTC_float2int($val) {
152-
return bcmul($val, "100000000", 0);
153-
}
154-
// From comment on http://php.net/manual/en/ref.bc.php
155-
function bcconv($fNumber) {
156-
$sAppend = '';
157-
$iDecimals = ini_get('precision') - floor(log10(abs($fNumber)));
158-
if (0 > $iDecimals) {
159-
$fNumber *= pow(10, $iDecimals);
160-
$sAppend = str_repeat('0', -$iDecimals);
161-
$iDecimals = 0;
162-
}
163-
return number_format($fNumber, $iDecimals, '.', '').$sAppend;
164143
}

src/Conversion/Conversion.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3+
4+
/**
5+
* Short File Description
6+
*
7+
* PHP version 5
8+
*
9+
* @category aCategory
10+
* @package aPackage
11+
* @subpackage aSubPackage
12+
* @author anAuthor
13+
* @copyright 2014 a Copyright
14+
* @license a License
15+
* @link http://www.aLink.com
16+
*/
17+
namespace Blockchain\Conversion;
18+
19+
/**
20+
* Short Class Description
21+
*
22+
* PHP version 5
23+
*
24+
* @category aCategory
25+
* @package aPackage
26+
* @subpackage aSubPackage
27+
* @author anAuthor
28+
* @copyright 2014 a Copyright
29+
* @license a License
30+
* @link http://www.aLink.com
31+
*/
32+
class Conversion
33+
{
34+
/**
35+
* Properties
36+
*/
37+
38+
39+
/**
40+
* Methods
41+
*/
42+
/**
43+
* Convert an incoming integer to a BTC string value
44+
*/
45+
static function BTC_int2str($val)
46+
{
47+
$a = bcmul($val, "1.0", 1);
48+
return bcdiv($a, "100000000", 8);
49+
}
50+
51+
/**
52+
* Convert a float value to BTC satoshi integer string
53+
*/
54+
static function BTC_float2int($val)
55+
{
56+
return bcmul($val, "100000000", 0);
57+
}
58+
59+
/**
60+
* From comment on http://php.net/manual/en/ref.bc.php
61+
*/
62+
static function bcconv($fNumber)
63+
{
64+
$sAppend = '';
65+
$iDecimals = ini_get('precision') - floor(log10(abs($fNumber)));
66+
if (0 > $iDecimals) {
67+
$fNumber *= pow(10, $iDecimals);
68+
$sAppend = str_repeat('0', -$iDecimals);
69+
$iDecimals = 0;
70+
}
71+
72+
return number_format($fNumber, $iDecimals, '.', '').$sAppend;
73+
}
74+
}

src/Explorer/Address.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public function __construct($json) {
5353
if(array_key_exists('n_tx', $json))
5454
$this->n_tx = $json['n_tx'];
5555
if(array_key_exists('total_received', $json))
56-
$this->total_received = BTC_int2str($json['total_received']);
56+
$this->total_received = \Blockchain\Conversion\Conversion::BTC_int2str($json['total_received']);
5757
if(array_key_exists('total_sent', $json))
58-
$this->total_sent = BTC_int2str($json['total_sent']);
58+
$this->total_sent = \Blockchain\Conversion\Conversion::BTC_int2str($json['total_sent']);
5959
if(array_key_exists('final_balance', $json))
60-
$this->final_balance = BTC_int2str($json['final_balance']);
60+
$this->final_balance = \Blockchain\Conversion\Conversion::BTC_int2str($json['final_balance']);
6161
if(array_key_exists('txs', $json)) {
6262
foreach ($json['txs'] as $txn) {
6363
$this->transactions[] = new Transaction($txn);

src/Explorer/Block.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function __construct($json) {
6868
if(array_key_exists('bits', $json))
6969
$this->bits = $json['bits'];
7070
if(array_key_exists('fee', $json))
71-
$this->fee = BTC_int2str($json['fee']);
71+
$this->fee = \Blockchain\Conversion\Conversion::BTC_int2str($json['fee']);
7272
if(array_key_exists('nonce', $json))
7373
$this->nonce = $json['nonce'];
7474
if(array_key_exists('n_tx', $json))

src/Explorer/Input.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct($json) {
5454
if(array_key_exists('n', $P))
5555
$this->n = $P['n'];
5656
if(array_key_exists('value', $P))
57-
$this->value = BTC_int2str($P['value']);
57+
$this->value = \Blockchain\Conversion\Conversion::BTC_int2str($P['value']);
5858
if(array_key_exists('addr', $P))
5959
$this->address = $P['addr'];
6060
if(array_key_exists('tx_index', $P))

src/Explorer/Output.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct($json) {
4848
if(array_key_exists('n', $json))
4949
$this->n = $json['n'];
5050
if(array_key_exists('value', $json))
51-
$this->value = BTC_int2str($json['value']);
51+
$this->value = \Blockchain\Conversion\Conversion::BTC_int2str($json['value']);
5252
if(array_key_exists('addr', $json))
5353
$this->address = $json['addr'];
5454
if(array_key_exists('tx_index', $json))

src/Explorer/UnspentOutput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct($json) {
5858
if(array_key_exists('script', $json))
5959
$this->script = $json['script'];
6060
if(array_key_exists('value', $json))
61-
$this->value = BTC_int2str($json['value']);
61+
$this->value = \Blockchain\Conversion\Conversion::BTC_int2str($json['value']);
6262
if(array_key_exists('value_hex', $json))
6363
$this->value_hex = $json['value_hex'];
6464
if(array_key_exists('confirmations', $json))

0 commit comments

Comments
 (0)