|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TDataSize class file |
|
4
|
|
|
* |
|
5
|
|
|
* @author Brad Anderson <[email protected]> |
|
6
|
|
|
* @link https://github.com/pradosoft/prado |
|
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
8
|
|
|
* @package Prado\Web\UI\WebControls |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Web\UI\WebControls; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\Exceptions\TInvalidDataValueException; |
|
14
|
|
|
use Prado\Prado; |
|
15
|
|
|
use Prado\TPropertyValue; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* TDataSize class |
|
19
|
|
|
* |
|
20
|
|
|
* TDataSize produces the size of a file in its natural size for human |
|
21
|
|
|
* readability rather than bytes. |
|
22
|
|
|
* <code> |
|
23
|
|
|
* <com:TDataSize Size="475837458" UseMarketingSize="true" Abbreviate="true"/> |
|
24
|
|
|
* </code> |
|
25
|
|
|
* will output "476 MB". |
|
26
|
|
|
* |
|
27
|
|
|
* The TDataSize output depends on {@link getAbbreviate Abbreviate} and |
|
28
|
|
|
* {@link getUseMarketingSize UseMarketingSize}. |
|
29
|
|
|
* |
|
30
|
|
|
* {@link getUseMarketingSize UseMarketingSize} will change the size of a |
|
31
|
|
|
* kilobyte to be 1000 rather the technical 1024. This changes the output |
|
32
|
|
|
* between bytes, kilobytes, megabytes, gigabytes, terabytes, petabytes, |
|
33
|
|
|
* exabytes, zettabytes, and yottabytes for UseMarketingSize="True" (base 1000) and |
|
34
|
|
|
* the technical bytes, kibibytes, mebibytes, gibibytes, tebibytes, pebibytes, |
|
35
|
|
|
* exbibytes, zebibytes, and yobibytes for UseMarketingSize="False" (base 1024). |
|
36
|
|
|
* The singular and plural of these these outputted words are localized. |
|
37
|
|
|
* |
|
38
|
|
|
* For {@link getAbbreviate Abbreviate} that is true, with UseMarketingSize="True" |
|
39
|
|
|
* then B, KB, MB, GB, TB, PB, EB, ZB, YB is outputted. Otherwise with |
|
40
|
|
|
* UseMarketingSize="False" then B, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB is |
|
41
|
|
|
* outputted. These outputted abbreviations are localized. |
|
42
|
|
|
* |
|
43
|
|
|
* @author Brad Anderson <[email protected]> |
|
44
|
|
|
* @package Prado\Web\UI\WebControls |
|
45
|
|
|
* @since 4.2.0 |
|
46
|
|
|
*/ |
|
47
|
|
|
class TDataSize extends TLabel |
|
48
|
|
|
{ |
|
49
|
|
|
/** |
|
50
|
|
|
* renders the size in the closes base, bytes, kilobytes, megabytes, |
|
51
|
|
|
* gigabytes, terabytes, petabytes, exabytes, zettabytes, and yottabytes |
|
52
|
|
|
* for marketing terms, and bytes, kibibytes, mebibytes gibibytes, |
|
53
|
|
|
* tebibytes, pebibytes, exbibytes, zebibytes, and yobibytes for technical |
|
54
|
|
|
* terms. |
|
55
|
|
|
* @param object $writer where the method writes output. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function renderContents($writer) |
|
58
|
|
|
{ |
|
59
|
|
|
$s = $this->getSize(); |
|
60
|
|
|
$abbr = $this->getAbbreviate(); |
|
61
|
|
|
$marketingSize = $this->getUseMarketingSize(); |
|
62
|
|
|
|
|
63
|
|
|
$d = $marketingSize ? 1000 : 1024; |
|
64
|
|
|
$index = min(max(floor(log($s, $d)), 0), 8); |
|
65
|
|
|
$s /= pow($d, $index); |
|
66
|
|
|
|
|
67
|
|
|
$sf = ($s >= 1000) ? 3 : 2; |
|
68
|
|
|
$s = round($s, (int) ceil($sf - log10($s))); |
|
69
|
|
|
|
|
70
|
|
|
if ($abbr && $marketingSize) { |
|
71
|
|
|
$decimal = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
|
72
|
|
|
$t = $s . ' ' . Prado::localize($decimal[$index]); |
|
73
|
|
|
} elseif ($abbr && !$marketingSize) { |
|
74
|
|
|
$binary = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; |
|
75
|
|
|
$t = $s . ' ' . Prado::localize($binary[$index]); |
|
76
|
|
|
} else { |
|
77
|
|
|
if ($marketingSize) { |
|
78
|
|
|
$names = ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte', 'petabyte', 'exabyte', 'zettabyte', 'yottabyte']; |
|
79
|
|
|
} else { |
|
80
|
|
|
$names = ['byte', 'kibibyte', 'mebibyte', 'gibibyte', 'tebibyte', 'pebibyte', 'exbibyte', 'zebibyte', 'yobibyte']; |
|
81
|
|
|
} |
|
82
|
|
|
$appendix = ''; |
|
83
|
|
|
if ($s != 1) { |
|
84
|
|
|
$appendix = 's'; |
|
85
|
|
|
} |
|
86
|
|
|
$t = $s . ' ' . Prado::localize($names[$index] . $appendix); |
|
87
|
|
|
} |
|
88
|
|
|
$writer->write($t); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return int data size in bytes. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getSize() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->getViewState('Size', 0); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param int $size data size in bytes |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setSize($size) |
|
103
|
|
|
{ |
|
104
|
|
|
$size = TPropertyValue::ensureFloat($size); |
|
105
|
|
|
if ($size < 0) { |
|
106
|
|
|
throw new TInvalidDataValueException('datasize_no_negative_size', $size); |
|
107
|
|
|
} |
|
108
|
|
|
$this->setViewState('Size', $size, 0); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return bool using marketing sizes (base 1000) or not (technical sizes, base 1024) |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getUseMarketingSize() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->getViewState('UseMarketingSize', true); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param bool $marketing using marketing sizes (base 1000) or not (technical sizes, base 1024) |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setUseMarketingSize($marketing) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->setViewState('UseMarketingSize', TPropertyValue::ensureBoolean($marketing), true); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return bool using abbreviations or not |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getAbbreviate() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->getViewState('Abbreviate', true); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param bool $abbr using abbreviations or not |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setAbbreviate($abbr) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->setViewState('Abbreviate', TPropertyValue::ensureBoolean($abbr), true); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|