1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\PhalueObjects\Format; |
4
|
|
|
|
5
|
|
|
use BestServedCold\PhalueObjects\Contract\VOStringable; |
6
|
|
|
use BestServedCold\PhalueObjects\VOArray\Find; |
7
|
|
|
use BestServedCold\PhalueObjects\VOArray\Map; |
8
|
|
|
use BestServedCold\PhalueObjects\VOClosure\Value; |
9
|
|
|
use BestServedCold\PhalueObjects\VOFloat; |
10
|
|
|
use BestServedCold\PhalueObjects\VOString; |
11
|
|
|
use BestServedCold\PhalueObjects\VOString\Word; |
12
|
|
|
use BestServedCold\PhalueObjects\VOString\Mixin as VOStringTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Byte |
16
|
|
|
* |
17
|
|
|
* @package BestServedCold\PhalueObjects\Format |
18
|
|
|
*/ |
19
|
|
|
abstract class Byte extends VOFloat implements VOStringable |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
use VOStringTrait; |
22
|
|
|
|
23
|
|
|
const UNITS = [ |
24
|
|
|
[ 'Byte' => 'Byte' ], |
25
|
|
|
[ 'KB' => 'Kilobyte' ], |
26
|
|
|
[ 'MB' => 'Megabyte' ], |
27
|
|
|
[ 'GB' => 'Gigabyte' ], |
28
|
|
|
[ 'TB' => 'Terabyte' ], |
29
|
|
|
[ 'PB' => 'Pecabyte' ], |
30
|
|
|
[ 'EB' => 'Exabyte' ], |
31
|
|
|
[ 'ZB' => 'Zettabyte' ], |
32
|
|
|
[ 'YB' => 'Yottabyte' ] |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var integer $power |
37
|
|
|
*/ |
38
|
|
|
protected static $power; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
3 |
|
public function __toString() |
44
|
|
|
{ |
45
|
3 |
|
return $this->toString(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $string |
50
|
|
|
* @return static |
51
|
|
|
*/ |
52
|
2 |
|
public static function fromString($string) |
53
|
|
|
{ |
54
|
2 |
|
$voString = VOString::fromString($string); |
55
|
|
|
|
56
|
2 |
|
return new static( |
57
|
2 |
|
$voString->getNumbers() * |
58
|
2 |
|
pow( |
59
|
2 |
|
static::$power, |
60
|
2 |
|
Find::fromArray(self::getUnitKeyList())->keyFromArrayValue($voString->getLetters()) |
61
|
2 |
|
) |
62
|
2 |
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
3 |
|
public function toString() |
69
|
|
|
{ |
70
|
3 |
|
return $this->getValue() ? $this->getPower().' '.$this->getUnit() : '0 Bytes'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param null $power |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
4 |
|
public function getUnit($power = null) |
78
|
|
|
{ |
79
|
4 |
|
$power = $power ?: $this->getPower(); |
80
|
4 |
|
$unit = self::UNITS[ (int) floor($this->base()) ]; |
81
|
4 |
|
return Word::fromString(reset($unit))->getPluralised($power); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int $precision |
86
|
|
|
* @return float |
87
|
|
|
*/ |
88
|
4 |
|
public function getPower($precision = 2) |
89
|
|
|
{ |
90
|
4 |
|
return round(pow(static::$power, $this->base() - floor($this->base())), $precision); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return float |
95
|
|
|
*/ |
96
|
4 |
|
private function base() |
97
|
|
|
{ |
98
|
4 |
|
return log($this->getValue(), static::$power); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
2 |
|
private static function getUnitKeyList() |
105
|
|
|
{ |
106
|
2 |
|
return Map::fromVariadic(Value::toArrayWithPlural(true), self::UNITS)->getValue(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.