foldershare-8.x-1.2/src/Utilities/FormatUtilities.php
src/Utilities/FormatUtilities.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | <?php namespace Drupal\foldershare\Utilities; use Drupal\Core\Render\Markup; use Drupal\Component\Render\FormattableMarkup; /** * Defines utility functions to help with formatting. * * The functions in this class help format error messages and number values * indicating the number of bytes used by something. * * <B>Warning:</B> This class is strictly internal to the FolderShare * module. The class's existance, name, and content may change from * release to release without any promise of backwards compatability. * * @ingroup foldershare */ final class FormatUtilities { /*--------------------------------------------------------------------- * * Functions. * *---------------------------------------------------------------------*/ /** * Formats a number with a bytes suffix. * * The given number is formatted and returned as markup that may include * a suffix like 'K' for kilobytes, 'M' for megabytes, or 'G' for * gigabytes. * * An optional argument selects whether the unit of 'k' should * be the current standard 1000, or the older convention of 1024. * * Optionally, suffixes may be full words instead of abbreviations. * * @param mixed $number * The number to format. * @param int $kunit * (optional, default = 1000) Either 1000 (ISO standard kilobyte) or * 1024 (legacy kibibyte). * @param bool $fullWord * (optional, default = FALSE) When FALSE, use an abbreviation suffix, * like "KB". When TRUE, use a full word suffix, like "Kilobytes". * @param int $decimalDigits * (optional, default = 2) The number of decimal digits for the resulting * number. * * @return \Drupal\Core\StringTranslation\TranslatableMarkup * Returns a representation of the number, along with a suffix * that summarizes the value using the selected 'k' units. * * @internal * Drupal's format_size() function is widely used to format values in * units of kilobytes, megabytes, etc. Unfortunately, it considers a * "kilobyte" equal to 1024 bytes, which is not compliant with the * 1998 International Electrotechnical Commission (IEC) and * International System of Quantities (ISO) standards. By those standards, * a "kilobyte" equals 1000 bytes. This definition retains the SI units * convention that "k" is for 1000 (e.g. kilometer, kilogram). * * The following code is loosely based upon format_size(). It corrects * the math for the selected "k" units (kilo or kibi) and uses the * corresponding correct suffixes. It also supports a requested number * of decimal digits. * * The following code also ignores the $langcode, which format_size() * incorrectly uses to make the output translatable - even though it is * NOT subject to translation since it is numeric and the suffixes are * defined by international standards. * @endinternal */ public static function formatBytes( $number , int $kunit = 1000, bool $fullWord = FALSE, int $decimalDigits = 2) { // Validate. switch ( $kunit ) { case 1000: case 1024: $kunit = (float) $kunit ; break ; default : $kunit = 1000.0; break ; } if ( $decimalDigits < 0) { $decimalDigits = 0; } if ( $number < $kunit ) { // The quantity is smaller than the 'k' unit. Report it as-is. return \Drupal::translation()->formatPlural( $number , '1 byte' , '@count bytes' ); } // Find the proper 'k'. Values larger than the largest 'k' unit // will just be large numbers ahead of the largest prefix (e.g. // "879 YB"). $value = (((float) $number ) / $kunit ); foreach ([ 'K' , 'M' , 'G' , 'T' , 'P' , 'E' , 'Z' , 'Y' ] as $unit ) { if ( $value < $kunit ) { // The value is now less than the next unit, stop reducing. break ; } $value /= $kunit ; } // Simplify the remainder with the proper number of decimal digits. $fmt = '%1.' . $decimalDigits . 'f' ; $formattedValue = sprintf( $fmt , $value ); // If using abbrevations, return the formatted value and suffix. if ( $fullWord === FALSE) { // Use abbreviations for suffixes. switch ( $kunit ) { default : case 1000: $suffix = $unit . 'B' ; break ; case 1024: $suffix = $unit . 'iB' ; break ; } return new FormattableMarkup( '@value @suffix' , [ '@value' => $formattedValue , '@suffix' => $suffix , ]); } // When using full words, return the formatted value and word, // using singular and plural forms as appropriate. $singular = []; $plural = []; switch ( $kunit ) { default : case 1000: $singular = [ 'K' => 'Kilobyte' , 'M' => 'Megabyte' , 'G' => 'Gigabyte' , 'T' => 'Terabyte' , 'P' => 'Petabyte' , 'E' => 'Exabyte' , 'Z' => 'Zettabyte' , 'Y' => 'Yottabyte' , ]; $plural = [ 'K' => 'Kilobytes' , 'M' => 'Megabytes' , 'G' => 'Gigabytes' , 'T' => 'Terabytes' , 'P' => 'Petabytes' , 'E' => 'Exabytes' , 'Z' => 'Zettabytes' , 'Y' => 'Yottabytes' , ]; break ; case 1024: $singular = [ 'K' => 'Kibibyte' , 'M' => 'Mibibyte' , 'G' => 'Gibibyte' , 'T' => 'Tebibyte' , 'P' => 'Pebibyte' , 'E' => 'Exbibyte' , 'Z' => 'Zebibyte' , 'Y' => 'Yobibyte' , ]; $plural = [ 'K' => 'Kibibytes' , 'M' => 'Mibibytes' , 'G' => 'Gibibytes' , 'T' => 'Tebibytes' , 'P' => 'Pebibytes' , 'E' => 'Exbibytes' , 'Z' => 'Zebibytes' , 'Y' => 'Yobibytes' , ]; break ; } return new FormattableMarkup( '@value @suffix' , [ '@value' => $formattedValue , '@suffix' => ( $value < 1) ? $singular [ $unit ] : $plural [ $unit ], ]); } /** * Returns standardized markup for a summary, body, & details message. * * Formatted messages are intended for use in exceptions or as * messages posted on a page via Drupal's messenger. They follow * a three-part convention: * * - Summary - a short statement of the essential issue. * * - Body - a longer multi-sentence description of the problem and how * a user might solve it. * * - Details summary - a brief summary of the details section. * * - Details - a much longer detailed explanation. * * The summary is required, but the body and details are optional. * * @param string|\Drupal\Component\Render\MarkupInterface $summary * The short summary of the issue. * @param string|\Drupal\Component\Render\MarkupInterface $body * (optional, default = NULL = none) The longer description of the issue * and how to resolve it. * @param string|\Drupal\Component\Render\MarkupInterface $detailsSummary * (optional, default = NULL = none) A brief summary of the details * section. This could be "Details:", for instance. * @param string|\Drupal\Component\Render\MarkupInterface $details * (optional, default = NULL = none) The much longer detailed information. * * @return \Drupal\Core\Render\Markup * Returns a Markup object suitable for use in messages. Casting the * object to a (string) creates a conventional string message. */ public static function createFormattedMessage( $summary , $body = NULL, $detailsSummary = NULL, $details = NULL) { $markup = '<span class="foldershare-message-summary">' . (string) $summary . "</span> \n" ; if ( $body !== NULL) { $markup .= '<p class="foldershare-message-body">' . (string) $body . "</p> \n" ; } if ( $details !== NULL) { $markup .= '<details class="foldershare-message-details">' ; if ( $detailsSummary !== NULL) { $markup .= '<summary>' . (string) $detailsSummary . "</summary> \n" ; } $markup .= '<p>' . (string) $details . '</p></details>' ; } return Markup::create( $markup ); } } |