ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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...

+ Inheritance diagram for ILIAS\Data\DataSize:
+ 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
 
static array 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

Definition at line 86 of file DataSize.php.

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

87  {
88  $this->suffix = $this->mapUnitToSuffix($unit);
89  $this->size = (float) $size / (float) $unit; // the div operation can return int and float
90  $this->unit = $unit;
91  }
mapUnitToSuffix(int $unit)
Definition: DataSize.php:93
static array float $size
Definition: DataSize.php:55
+ 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 140 of file DataSize.php.

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

140  : string
141  {
142  $size = $this->inBytes();
143  $unit = match (true) {
144  $size > self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR => DataSize::TB,
145  $size > self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR => DataSize::GB,
146  $size > self::SIZE_FACTOR * self::SIZE_FACTOR => DataSize::MB,
147  $size > self::SIZE_FACTOR => DataSize::KB,
148  default => DataSize::Byte,
149  };
150 
151  $size = round($size / (float) $unit, self::PRECISION);
152 
153  return "$size " . $this->mapUnitToSuffix($unit);
154  }
mapUnitToSuffix(int $unit)
Definition: DataSize.php:93
static array float $size
Definition: DataSize.php:55
inBytes()
Get the size in bytes.
Definition: DataSize.php:128
+ Here is the call graph for this function:

◆ getSize()

ILIAS\Data\DataSize::getSize ( )

The calculated data size.

Definition at line 112 of file DataSize.php.

References ILIAS\Data\DataSize\$size.

Referenced by ILIAS\Filesystem\FilesystemFacade\getSize().

112  : float
113  {
114  return $this->size;
115  }
static array float $size
Definition: DataSize.php:55
+ 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 120 of file DataSize.php.

References ILIAS\Data\DataSize\$unit.

120  : int
121  {
122  return $this->unit;
123  }

◆ inBytes()

ILIAS\Data\DataSize::inBytes ( )

Get the size in bytes.

Definition at line 128 of file DataSize.php.

References ILIAS\Data\DataSize\$unit.

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

128  : float
129  {
130  return $this->size * $this->unit;
131  }
+ Here is the caller graph for this function:

◆ mapUnitToSuffix()

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

Definition at line 93 of file DataSize.php.

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

93  : string
94  {
95  return match ($unit) {
96  self::Byte => 'B',
97  self::KiB => 'KiB',
98  self::MiB => 'MiB',
99  self::GiB => 'GiB',
100  self::TiB => 'TiB',
101  self::KB => 'KB',
102  self::MB => 'MB',
103  self::GB => 'GB',
104  self::TB => 'TB',
105  default => throw new \InvalidArgumentException('The given data size unit is not valid, please check the provided class constants of the DataSize class.')
106  };
107  }
+ Here is the caller graph for this function:

Field Documentation

◆ $abbreviations

array ILIAS\Data\DataSize::$abbreviations
static
Initial value:
= [
'B' => self::Byte

Definition at line 52 of file DataSize.php.

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

◆ $size

array float ILIAS\Data\DataSize::$size
private
Initial value:
=> 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 55 of file DataSize.php.

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

◆ $suffix

string ILIAS\Data\DataSize::$suffix
private

Definition at line 76 of file DataSize.php.

◆ $unit

int ILIAS\Data\DataSize::$unit
private

◆ Byte

◆ GB

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

◆ GiB

const ILIAS\Data\DataSize::GiB = 1_073_741_824

◆ KB

◆ KiB

const ILIAS\Data\DataSize::KiB = 1024

◆ MB

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

◆ MiB

const ILIAS\Data\DataSize::MiB = 1_048_576

◆ 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.

◆ TB

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

Definition at line 47 of file DataSize.php.

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

◆ TiB

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

Definition at line 41 of file DataSize.php.


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