ILIAS  release_8 Revision v8.24
ILIAS\Data\DataSize Class Reference

This class provides the data size with additional information to remove the work to calculate the size to different unit like GiB, GB usw. More...

+ Collaboration diagram for ILIAS\Data\DataSize:

Public Member Functions

 __construct (int $size, int $unit)
 DataSize constructor. More...
 
 getSize ()
 The calculated data size. More...
 
 getUnit ()
 The unit which equals the class constant used to calculate the data size. More...
 
 inBytes ()
 Get the size in bytes. More...
 
 __toString ()
 Returns the data size in a human readable manner. More...
 

Data Fields

const Byte = 1
 
const KiB = 1024
 
const MiB = 1_048_576
 
const GiB = 1_073_741_824
 
const TiB = 1_099_511_627_776
 
const KB = 1000
 
const MB = 1_000_000
 
const GB = 1_000_000_000
 
const TB = 1_000_000_000_000
 

Static Public Attributes

static array $abbreviations
 

Private Member Functions

 mapUnitToSuffix (int $unit)
 

Private Attributes

const SIZE_FACTOR = 1000
 
const PRECISION = 2
 
float $size
 
int $unit
 
string $suffix
 

Detailed Description

This class provides the data size with additional information to remove the work to calculate the size to different unit like GiB, GB usw.

Author
Nicolas Schäfli ns@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch
Fabian Schmid fabia.nosp@m.n@sr.nosp@m..solu.nosp@m.tion.nosp@m.s

Definition at line 30 of file DataSize.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Data\DataSize::__construct ( int  $size,
int  $unit 
)

DataSize constructor.

Parameters
int$sizeThe data size in bytes.
int$unitThe unit which is used to calculate the data size.
Exceptions

InvalidArgumentException If the given unit is not valid or the arguments are not of the type int.

Definition at line 85 of file DataSize.php.

86 {
87 $this->suffix = $this->mapUnitToSuffix($unit);
88 $this->size = (float) $size / (float) $unit; // the div operation can return int and float
89 $this->unit = $unit;
90 }
mapUnitToSuffix(int $unit)
Definition: DataSize.php:91

References ILIAS\Data\DataSize\$size, ILIAS\Data\DataSize\$unit, and ILIAS\Data\DataSize\mapUnitToSuffix().

+ Here is the call graph for this function:

Member Function Documentation

◆ __toString()

ILIAS\Data\DataSize::__toString ( )

Returns the data size in a human readable manner.

Example output: 950 B 3922 GB

Definition at line 144 of file DataSize.php.

144 : string
145 {
146 $size = $this->inBytes();
147 // can be switched to match in ILIAS 9
148 switch (true) {
149 case $size > self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR:
151 break;
152 case $size > self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR:
154 break;
155 case $size > self::SIZE_FACTOR * self::SIZE_FACTOR:
157 break;
160 break;
161 default:
163 break;
164 }
165
166 $size = round($size / (float) $unit, self::PRECISION);
167
168 return "$size " . $this->mapUnitToSuffix($unit);
169 }
inBytes()
Get the size in bytes.
Definition: DataSize.php:133

References ILIAS\Data\DataSize\$size, ILIAS\Data\DataSize\$unit, ILIAS\Data\DataSize\Byte, ILIAS\Data\DataSize\GB, ILIAS\Data\DataSize\inBytes(), ILIAS\Data\DataSize\KB, ILIAS\Data\DataSize\mapUnitToSuffix(), ILIAS\Data\DataSize\MB, ILIAS\Data\DataSize\SIZE_FACTOR, and ILIAS\Data\DataSize\TB.

+ Here is the call graph for this function:

◆ getSize()

ILIAS\Data\DataSize::getSize ( )

The calculated data size.

Definition at line 119 of file DataSize.php.

119 : float
120 {
121 return $this->size;
122 }

References ILIAS\Data\DataSize\$size.

Referenced by ILIAS\Filesystem\FilesystemFacade\getSize(), and ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\getSize().

+ Here is the caller graph for this function:

◆ getUnit()

ILIAS\Data\DataSize::getUnit ( )

The unit which equals the class constant used to calculate the data size.

(self::GiB, ...)

Definition at line 126 of file DataSize.php.

126 : int
127 {
128 return $this->unit;
129 }

References ILIAS\Data\DataSize\$unit.

◆ inBytes()

ILIAS\Data\DataSize::inBytes ( )

Get the size in bytes.

Definition at line 133 of file DataSize.php.

133 : float
134 {
135 return $this->size * $this->unit;
136 }

References ILIAS\Data\DataSize\$unit.

Referenced by ILIAS\Data\DataSize\__toString().

+ Here is the caller graph for this function:

◆ mapUnitToSuffix()

ILIAS\Data\DataSize::mapUnitToSuffix ( int  $unit)
private

Definition at line 91 of file DataSize.php.

91 : string
92 {
93 switch ($unit) {
94 case self::Byte:
95 return 'B';
96 case self::KiB:
97 return 'KiB';
98 case self::MiB:
99 return 'MiB';
100 case self::GiB:
101 return 'GiB';
102 case self::TiB:
103 return 'TiB';
104 case self::KB:
105 return 'KB';
106 case self::MB:
107 return 'MB';
108 case self::GB:
109 return 'GB';
110 case self::TB:
111 return 'TB';
112 default:
113 throw new \InvalidArgumentException('The given data size unit is not valid, please check the provided class constants of the DataSize class.');
114 }
115 }

References ILIAS\Data\DataSize\$unit, ILIAS\Data\DataSize\Byte, ILIAS\Data\DataSize\GB, ILIAS\Data\DataSize\GiB, ILIAS\Data\DataSize\KB, ILIAS\Data\DataSize\KiB, ILIAS\Data\DataSize\MB, ILIAS\Data\DataSize\MiB, ILIAS\Data\DataSize\TB, and ILIAS\Data\DataSize\TiB.

Referenced by ILIAS\Data\DataSize\__construct(), and ILIAS\Data\DataSize\__toString().

+ Here is the caller graph for this function:

Field Documentation

◆ $abbreviations

array ILIAS\Data\DataSize::$abbreviations
static
Initial value:
= [
'B' => self::Byte,
'KB' => self::KB,
'K' => self::KiB,
'k' => self::KiB,
'KiB' => self::KiB,
'MB' => self::MB,
'M' => self::MiB,
'm' => self::MiB,
'MiB' => self::MiB,
'GB' => self::GB,
'G' => self::GiB,
'g' => self::GiB,
'GiB' => self::GiB,
'TB' => self::TB,
'TiB' => self::TiB,
]

Definition at line 53 of file DataSize.php.

Referenced by ILIAS\Data\Factory\dataSize().

◆ $size

float ILIAS\Data\DataSize::$size
private

◆ $suffix

string ILIAS\Data\DataSize::$suffix
private

Definition at line 76 of file DataSize.php.

◆ $unit

◆ Byte

const ILIAS\Data\DataSize::Byte = 1

◆ GB

const ILIAS\Data\DataSize::GB = 1_000_000_000

◆ GiB

◆ KB

const ILIAS\Data\DataSize::KB = 1000

◆ KiB

◆ MB

const ILIAS\Data\DataSize::MB = 1_000_000

◆ MiB

◆ PRECISION

const ILIAS\Data\DataSize::PRECISION = 2
private

Definition at line 33 of file DataSize.php.

◆ SIZE_FACTOR

const ILIAS\Data\DataSize::SIZE_FACTOR = 1000
private

Definition at line 32 of file DataSize.php.

Referenced by ILIAS\Data\DataSize\__toString().

◆ TB

const ILIAS\Data\DataSize::TB = 1_000_000_000_000

◆ TiB

const ILIAS\Data\DataSize::TiB = 1_099_511_627_776

Definition at line 40 of file DataSize.php.

Referenced by ILIAS\Data\DataSize\mapUnitToSuffix().


The documentation for this class was generated from the following file: