| 1 |
<?php |
| 2 |
/** |
| 3 |
* Conversion handling |
| 4 |
* |
| 5 |
* Handles all conversion operations. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Decalog\System; |
| 13 |
|
| 14 |
/** |
| 15 |
* Define the conversion functionality. |
| 16 |
* |
| 17 |
* Handles all conversion operations. |
| 18 |
* |
| 19 |
* @package System |
| 20 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class Conversion { |
| 24 |
|
| 25 |
/** |
| 26 |
* Initializes the class and set its properties. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get a shortened number. |
| 35 |
* |
| 36 |
* @param float $number The number to shorten. |
| 37 |
* @param integer $precision Optional. The decimal numbers. |
| 38 |
* @param boolean $detail Optional. Give the detail of the shortening. |
| 39 |
* @param string $separator Optional. Unit separator. |
| 40 |
* @return string|array The shortened number. |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
public static function number_shorten( $number, $precision = 2, $detail = false, $separator = '' ) { |
| 44 |
$divisors = [ |
| 45 |
pow( 1000, 0 ) => '', |
| 46 |
pow( 1000, 1 ) => esc_html_x( 'K', 'Abbreviation - Stands for "thousand".', 'decalog' ), |
| 47 |
pow( 1000, 2 ) => esc_html_x( 'M', 'Abbreviation - Stands for "million".', 'decalog' ), |
| 48 |
pow( 1000, 3 ) => esc_html_x( 'B', 'Abbreviation - Stands for "billion".', 'decalog' ), |
| 49 |
pow( 1000, 4 ) => esc_html_x( 'T', 'Abbreviation - Stands for "trillion".', 'decalog' ), |
| 50 |
pow( 1000, 5 ) => esc_html_x( 'Qa', 'Abbreviation - Stands for "quadrillion".', 'decalog' ), |
| 51 |
pow( 1000, 6 ) => esc_html_x( 'Qi', 'Abbreviation - Stands for "quintillion".', 'decalog' ), |
| 52 |
]; |
| 53 |
foreach ( $divisors as $divisor => $shorthand ) { |
| 54 |
if ( abs( $number ) < ( $divisor * 1000 ) ) { |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
if ( $detail ) { |
| 59 |
return [ |
| 60 |
'value' => number_format( $number / $divisor, $precision, '.', '' ), |
| 61 |
'divisor' => $divisor, |
| 62 |
'abbreviation' => $shorthand, |
| 63 |
'base' => 1000, |
| 64 |
]; |
| 65 |
} else { |
| 66 |
return 0 + number_format( $number / $divisor, $precision, '.', '' ) . $separator . $shorthand; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get a shortened data. |
| 72 |
* |
| 73 |
* @param float $number The data to shorten. |
| 74 |
* @param integer $precision Optional. The decimal numbers. |
| 75 |
* @param boolean $detail Optional. Give the detail of the shortening. |
| 76 |
* @param string $separator Optional. Unit separator. |
| 77 |
* @return string|array The shortened data. |
| 78 |
* @since 1.0.0 |
| 79 |
*/ |
| 80 |
public static function data_shorten( $number, $precision = 2, $detail = false, $separator = '' ) { |
| 81 |
$divisors = [ |
| 82 |
pow( 1024, 0 ) => esc_html_x( 'B', 'Abbreviation - Stands for "byte".', 'decalog' ), |
| 83 |
pow( 1024, 1 ) => esc_html_x( 'KB', 'Abbreviation - Stands for "kilobytes".', 'decalog' ), |
| 84 |
pow( 1024, 2 ) => esc_html_x( 'MB', 'Abbreviation - Stands for "megabytes".', 'decalog' ), |
| 85 |
pow( 1024, 3 ) => esc_html_x( 'GB', 'Abbreviation - Stands for "gigabytes".', 'decalog' ), |
| 86 |
pow( 1024, 4 ) => esc_html_x( 'TB', 'Abbreviation - Stands for "terabytes".', 'decalog' ), |
| 87 |
pow( 1024, 5 ) => esc_html_x( 'PB', 'Abbreviation - Stands for "petabytes".', 'decalog' ), |
| 88 |
pow( 1024, 6 ) => esc_html_x( 'EB', 'Abbreviation - Stands for "exabytes".', 'decalog' ), |
| 89 |
]; |
| 90 |
foreach ( $divisors as $divisor => $shorthand ) { |
| 91 |
if ( abs( $number ) < ( $divisor * 1024 ) ) { |
| 92 |
break; |
| 93 |
} |
| 94 |
} |
| 95 |
if ( $detail ) { |
| 96 |
return [ |
| 97 |
'value' => number_format( $number / $divisor, $precision, '.', '' ), |
| 98 |
'divisor' => $divisor, |
| 99 |
'abbreviation' => $shorthand, |
| 100 |
'base' => 1024, |
| 101 |
]; |
| 102 |
} else { |
| 103 |
return 0 + number_format( $number / $divisor, $precision, '.', '' ) . $separator . $shorthand; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
} |