1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-13 10:15:28 +02:00

Add support for a 'type' option in wireBytesStr() function, enabling you to force return value in bytes, mb, kb, gb rather than auto-detected.

This commit is contained in:
Ryan Cramer
2019-12-27 15:52:06 -05:00
parent 12dbe3f250
commit deb2065465
2 changed files with 7 additions and 4 deletions

View File

@@ -10,7 +10,7 @@
* 3. Copy the getModuleInfo() method out of this class and update as appropriate. * 3. Copy the getModuleInfo() method out of this class and update as appropriate.
* 4. Implement an isValidFile($filename) method, and you are done. * 4. Implement an isValidFile($filename) method, and you are done.
* *
* EXAMPLE: /site/modules/FileValidatorSVG.module * EXAMPLE: /site/modules/FileValidatorHTML.module
* *
* class FileValidatorHTML extends FileValidatorModule { * class FileValidatorHTML extends FileValidatorModule {
* public static function getModuleInfo() { * public static function getModuleInfo() {

View File

@@ -694,12 +694,14 @@ function wireIconMarkupFile($filename, $class = '') {
* - `decimal_point` (string|null): Decimal point character, or null to detect from locale (default=null). * - `decimal_point` (string|null): Decimal point character, or null to detect from locale (default=null).
* - `thousands_sep` (string|null): Thousands separator, or null to detect from locale (default=null). * - `thousands_sep` (string|null): Thousands separator, or null to detect from locale (default=null).
* - `small` (bool): If no $small argument was specified, you can optionally specify it in this $options array. * - `small` (bool): If no $small argument was specified, you can optionally specify it in this $options array.
* - `type` (string): To force return value as specific type, specify one of: bytes, kilobytes, megabytes, gigabytes; or just: b, k, m, g. (3.0.148+ only)
* @return string * @return string
* *
*/ */
function wireBytesStr($bytes, $small = false, $options = array()) { function wireBytesStr($bytes, $small = false, $options = array()) {
$defaults = array( $defaults = array(
'type' => '',
'decimals' => 0, 'decimals' => 0,
'decimal_point' => null, 'decimal_point' => null,
'thousands_sep' => null, 'thousands_sep' => null,
@@ -714,19 +716,20 @@ function wireBytesStr($bytes, $small = false, $options = array()) {
$options = array_merge($defaults, $options); $options = array_merge($defaults, $options);
$locale = array(); $locale = array();
$type = empty($options['type']) ? '' : strtolower(substr($options['type'], 0, 1));
// determine size value and units label // determine size value and units label
if($bytes < 1024) { if($bytes < 1024 || $type === 'b') {
$val = $bytes; $val = $bytes;
if($small) { if($small) {
$label = $val > 0 ? __('B', __FILE__) : ''; // bytes $label = $val > 0 ? __('B', __FILE__) : ''; // bytes
} else { } else {
$label = __('bytes', __FILE__); $label = __('bytes', __FILE__);
} }
} else if($bytes < 1000000) { } else if($bytes < 1000000 || $type === 'k') {
$val = $bytes / 1024; $val = $bytes / 1024;
$label = __('kB', __FILE__); // kilobytes $label = __('kB', __FILE__); // kilobytes
} else if($bytes < 1073741824) { } else if($bytes < 1073741824 || $type === 'm') {
$val = $bytes / 1024 / 1024; $val = $bytes / 1024 / 1024;
$label = __('MB', __FILE__); // megabytes $label = __('MB', __FILE__); // megabytes
} else { } else {