ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Number.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
25class Number extends Column implements C\Number
26{
27 protected string $unit_position = self::UNIT_POSITION_AFT;
28 protected int $decimals = 0;
29 protected string $unit = '';
30
31 protected string $delim_decimal = ',';
32 protected string $delim_thousands = '';
33
34 private const POSSIBLE_UNIT_POSITIONS = [
35 self::UNIT_POSITION_FORE,
36 self::UNIT_POSITION_AFT
37 ];
38
39 public function withDecimals(int $number_of_decimals): self
40 {
41 $clone = clone $this;
42 $clone->decimals = $number_of_decimals;
43 return $clone;
44 }
45
46 public function getDecimals(): int
47 {
48 return $this->decimals;
49 }
50
51 public function withUnit(string $unit, string $unit_position = self::UNIT_POSITION_AFT): self
52 {
53 $this->checkArgIsElement(
54 'unit_position',
56 self::POSSIBLE_UNIT_POSITIONS,
57 'UNIT_POSITION_FORE | UNIT_POSITION_AFT'
58 );
59
60 $clone = clone $this;
61 $clone->unit = $unit;
62 $clone->unit_position = $unit_position;
63 return $clone;
64 }
65
66 public function format($value): string
67 {
68 $value = number_format(
69 $value,
70 $this->decimals,
71 $this->delim_decimal,
72 $this->delim_thousands
73 );
74
75 if ($this->unit === '') {
76 return $value;
77 }
78
79 if ($this->unit_position === self::UNIT_POSITION_FORE) {
80 $value = $this->unit . ' ' . $value;
81 }
82 if ($this->unit_position === self::UNIT_POSITION_AFT) {
83 return $value . ' ' . $this->unit;
84 }
85 return $value;
86 }
87
91 public function getOrderingLabels(): array
92 {
93 return [
94 $this->asc_label ?? $this->getTitle() . self::SEPERATOR . $this->lng->txt('order_option_numerical_ascending'),
95 $this->desc_label ?? $this->getTitle() . self::SEPERATOR . $this->lng->txt('order_option_numerical_descending')
96 ];
97 }
98}
withUnit(string $unit, string $unit_position=self::UNIT_POSITION_AFT)
Definition: Number.php:51