-
Notifications
You must be signed in to change notification settings - Fork 3
/
Satoshi.php
96 lines (84 loc) · 1.89 KB
/
Satoshi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* Sake
*
* @link http://github.com/sandrokeil/BlockchainWalletApi for the canonical source repository
* @copyright Copyright (c) 2014 Sandro Keil
* @license http://github.com/sandrokeil/BlockchainWalletApi/blob/master/LICENSE.txt New BSD License
*/
namespace Sake\BlockchainWalletApi\View\Helper;
use Zend\View\Helper\AbstractHelper;
/**
* Satoshi view helper
*
* Converts satoshi to other format e.g. btc
*/
class Satoshi extends AbstractHelper
{
/**
* Value is converted to BTC
*
* @var string
*/
const UNIT_BTC = 'BTC';
/**
* Value is converted to mBTC
*
* @var string
*/
const UNIT_MBTC = 'mBTC';
/**
* Value is converted to uBTC
*
* @var string
*/
const UNIT_UBTC = 'uBTC';
/**
* Default unit
*
* @var string
*/
protected $defaultUnit;
/**
* Converts satoshi to given unit
*
* @param int $satoshi Satoshi
* @param string $unit Use UNIT_* constant, default BTC
* @return float
*/
public function __invoke($satoshi, $unit = null)
{
return $this->format($satoshi, $unit);
}
/**
* @param string $unit Use UNIT_* constant
*/
public function setDefaultUnit($unit)
{
$this->defaultUnit = $unit;
}
/**
* @param $satoshi
* @param $unit
* @return float
*/
protected function format($satoshi, $unit)
{
if (null === $unit) {
$unit = $this->defaultUnit;
}
switch ($unit) {
case self::UNIT_MBTC:
$value = $satoshi / 100000;
break;
case self::UNIT_UBTC:
$value = $satoshi / 100;
break;
case self::UNIT_BTC:
default:
$value = $satoshi / 100000000;
break;
}
return $value;
}
}