-
Notifications
You must be signed in to change notification settings - Fork 1
/
SymbolDecorator.php
71 lines (57 loc) · 1.44 KB
/
SymbolDecorator.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
<?php
namespace ScriptFUSION\Byte\Unit;
use ScriptFUSION\Byte\Base;
/**
* Decorates byte values with unit symbols.
*/
class SymbolDecorator implements UnitDecorator
{
const
PREFIXES = 'KMGTPEZY',
SUFFIX_NONE = '',
SUFFIX_METRIC = 'B',
SUFFIX_IEC = 'iB'
;
private $suffix;
private $alwaysShowUnit;
public function __construct($suffix = null)
{
$this->setSuffix($suffix);
}
public function decorate($exponent, $base, $value)
{
if (($suffix = $this->suffix) === null) {
switch ($base) {
case Base::BINARY:
$suffix = static::SUFFIX_IEC;
break;
case Base::DECIMAL:
$suffix = static::SUFFIX_METRIC;
break;
}
}
if (!$exponent) {
return $suffix !== static::SUFFIX_NONE || $this->alwaysShowUnit ? 'B' : '';
}
return substr(static::PREFIXES, min($exponent, strlen(static::PREFIXES)) - 1, 1) . $suffix;
}
public function getSuffix()
{
return $this->suffix;
}
/**
* @param string|null $suffix
*
* @return $this
*/
public function setSuffix($suffix)
{
$this->suffix = $suffix;
return $this;
}
public function alwaysShowUnit($show = true)
{
$this->alwaysShowUnit = (bool)$show;
return $this;
}
}