ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
BaseToComponent.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 
32 abstract class BaseToComponent implements ToComponent
33 {
34  public const SIZE_FACTOR = 1000;
35  protected const DATE_FORMAT = 'd.m.Y H:i';
36  protected \ilLanguage $language;
37  protected \ILIAS\UI\Factory $ui_factory;
39  protected \ilObjUser $acting_user;
40 
41  public function __construct(
42  ?ActionGenerator $action_generator = null
43  ) {
44  global $DIC;
45  $this->action_generator = $action_generator ?? new NullActionGenerator();
46  $this->language = $DIC->language();
47  $this->language->loadLanguageModule('irss');
48  $this->ui_factory = $DIC->ui()->factory();
49  $this->acting_user = $DIC->user();
50  }
51 
52 
53  protected function formatSize(int $size): string
54  {
55  $unit = match (true) {
56  $size > self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR => DataSize::GB,
57  $size > self::SIZE_FACTOR * self::SIZE_FACTOR => DataSize::MB,
58  $size > self::SIZE_FACTOR => DataSize::KB,
59  default => DataSize::Byte,
60  };
61 
62  $size = (int) (round((float) $size / (float) $unit, 2) * (float) $unit);
63 
64  return (string) (new DataSize($size, $unit));
65  }
66 
67  protected function formatDate(\DateTimeImmutable $date): string
68  {
69  return $date
70  ->setTimezone(new \DateTimeZone($this->acting_user->getTimeZone()))
71  ->format(self::DATE_FORMAT);
72  }
73 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:30
global $DIC
Definition: feed.php:28
__construct(?ActionGenerator $action_generator=null)