class ByteSizeMarkup

Same name in other branches
  1. 10 core/lib/Drupal/Core/StringTranslation/ByteSizeMarkup.php \Drupal\Core\StringTranslation\ByteSizeMarkup

A class to generate translatable markup for the given byte count.

Hierarchy

Expanded class hierarchy of ByteSizeMarkup

16 files declare their use of ByteSizeMarkup
ByteSizeMarkupTest.php in core/tests/Drupal/Tests/Core/StringTranslation/ByteSizeMarkupTest.php
editor.admin.inc in core/modules/editor/editor.admin.inc
Administration functions for editor.module.
file.module in core/modules/file/file.module
Defines a "managed_file" Form API field and a "file" field for Field module.
FileFieldValidateTest.php in core/modules/file/tests/src/Functional/FileFieldValidateTest.php
FileItem.php in core/modules/file/src/Plugin/Field/FieldType/FileItem.php

... See full list

File

core/lib/Drupal/Core/StringTranslation/ByteSizeMarkup.php, line 12

Namespace

Drupal\Core\StringTranslation
View source
final class ByteSizeMarkup {
    
    /**
     * This class should not be instantiated.
     */
    private function __construct() {
    }
    
    /**
     * Gets the TranslatableMarkup object for the provided size.
     *
     * @return \Drupal\Core\StringTranslation\TranslatableMarkup
     *   The translatable markup.
     *
     * @throws \LogicException
     *   Thrown when an invalid unit size is used.
     */
    public static function create(float|int $size, ?string $langcode = NULL, ?TranslationInterface $stringTranslation = NULL) : TranslatableMarkup {
        $options = [
            'langcode' => $langcode,
        ];
        $absolute_size = abs($size);
        if ($absolute_size < Bytes::KILOBYTE) {
            return new PluralTranslatableMarkup($size, '1 byte', '@count bytes', [], $options, $stringTranslation);
        }
        // Create a multiplier to preserve the sign of $size.
        $sign = $absolute_size / $size;
        foreach ([
            'KB',
            'MB',
            'GB',
            'TB',
            'PB',
            'EB',
            'ZB',
            'YB',
        ] as $unit) {
            $absolute_size /= Bytes::KILOBYTE;
            $rounded_size = round($absolute_size, 2);
            if ($rounded_size < Bytes::KILOBYTE) {
                break;
            }
        }
        $args = [
            '@size' => $rounded_size * $sign,
        ];
        // At this point $markup must be set.
        return match ($unit) {    'KB' => new TranslatableMarkup('@size KB', $args, $options, $stringTranslation),
            'MB' => new TranslatableMarkup('@size MB', $args, $options, $stringTranslation),
            'GB' => new TranslatableMarkup('@size GB', $args, $options, $stringTranslation),
            'TB' => new TranslatableMarkup('@size TB', $args, $options, $stringTranslation),
            'PB' => new TranslatableMarkup('@size PB', $args, $options, $stringTranslation),
            'EB' => new TranslatableMarkup('@size EB', $args, $options, $stringTranslation),
            'ZB' => new TranslatableMarkup('@size ZB', $args, $options, $stringTranslation),
            'YB' => new TranslatableMarkup('@size YB', $args, $options, $stringTranslation),
            default => throw new \LogicException("Unexpected unit value"),
        
        };
    }

}

Members

Title Sort descending Modifiers Object type Summary
ByteSizeMarkup::create public static function Gets the TranslatableMarkup object for the provided size.
ByteSizeMarkup::__construct private function This class should not be instantiated.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.