PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-conversion.php

class-conversion.php in Sessions 2.3.1, at includes/system/class-conversion.php

107 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 POSessions\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".', 'sessions' ),
47 pow( 1000, 2 ) => esc_html_x( 'M', 'Abbreviation - Stands for "million".', 'sessions' ),
48 pow( 1000, 3 ) => esc_html_x( 'B', 'Abbreviation - Stands for "billion".', 'sessions' ),
49 pow( 1000, 4 ) => esc_html_x( 'T', 'Abbreviation - Stands for "trillion".', 'sessions' ),
50 pow( 1000, 5 ) => esc_html_x( 'Qa', 'Abbreviation - Stands for "quadrillion".', 'sessions' ),
51 pow( 1000, 6 ) => esc_html_x( 'Qi', 'Abbreviation - Stands for "quintillion".', 'sessions' ),
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".', 'sessions' ),
83 pow( 1024, 1 ) => esc_html_x( 'KB', 'Abbreviation - Stands for "kilobytes".', 'sessions' ),
84 pow( 1024, 2 ) => esc_html_x( 'MB', 'Abbreviation - Stands for "megabytes".', 'sessions' ),
85 pow( 1024, 3 ) => esc_html_x( 'GB', 'Abbreviation - Stands for "gigabytes".', 'sessions' ),
86 pow( 1024, 4 ) => esc_html_x( 'TB', 'Abbreviation - Stands for "terabytes".', 'sessions' ),
87 pow( 1024, 5 ) => esc_html_x( 'PB', 'Abbreviation - Stands for "petabytes".', 'sessions' ),
88 pow( 1024, 6 ) => esc_html_x( 'EB', 'Abbreviation - Stands for "exabytes".', 'sessions' ),
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 }