Audio
1 day ago
Video
1 day ago
AttachmentMetaData.php
1 day ago
AttachmentUrl.php
1 day ago
AudioPlayer.php
1 day ago
AvailableSizes.php
1 day ago
Dimensions.php
1 day ago
Download.php
1 day ago
ExifData.php
1 day ago
FileLinkToUrl.php
1 day ago
FileName.php
1 day ago
FileSize.php
1 day ago
Link.php
1 day ago
NestedAttachmentMetaData.php
1 day ago
NumberFormat.php
1 day ago
PostMimeType.php
1 day ago
PreviewViewLink.php
1 day ago
VideoEmbed.php
1 day ago
NumberFormat.php
41 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Formatter\Media; |
| 6 | |
| 7 | use AC\Formatter; |
| 8 | use AC\Type\Value; |
| 9 | |
| 10 | class NumberFormat implements Formatter |
| 11 | { |
| 12 | private int $decimals; |
| 13 | |
| 14 | private string $prefix; |
| 15 | |
| 16 | private string $suffix; |
| 17 | |
| 18 | public function __construct(int $decimals = 0, string $prefix = '', string $suffix = '') |
| 19 | { |
| 20 | $this->decimals = $decimals; |
| 21 | $this->prefix = $prefix; |
| 22 | $this->suffix = $suffix; |
| 23 | } |
| 24 | |
| 25 | public function format(Value $value) |
| 26 | { |
| 27 | $number = $value->get_value(); |
| 28 | |
| 29 | if ($number > 0) { |
| 30 | $formatted_number = number_format($number, $this->decimals); |
| 31 | |
| 32 | return $value->with_value( |
| 33 | sprintf('%s%s%s', $this->prefix, $formatted_number, $this->suffix) |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | return $value; |
| 38 | } |
| 39 | |
| 40 | } |
| 41 |