Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| PdfNumber | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toPdf | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Pdf\Core; |
| 6 | |
| 7 | /** |
| 8 | * Represents a PDF numeric object (integer or real number). |
| 9 | */ |
| 10 | class PdfNumber implements Serializable |
| 11 | { |
| 12 | public function __construct(public readonly int|float $value) {} |
| 13 | |
| 14 | public function toPdf(): string |
| 15 | { |
| 16 | if (is_int($this->value)) { |
| 17 | return (string) $this->value; |
| 18 | } |
| 19 | |
| 20 | // Format float without trailing zeros but with enough precision. |
| 21 | // PDF reals should not use scientific notation. |
| 22 | $formatted = rtrim(rtrim(sprintf('%.6f', $this->value), '0'), '.'); |
| 23 | // Edge case: -0 should be 0 |
| 24 | if ($formatted === '-0' || $formatted === '') { |
| 25 | $formatted = '0'; |
| 26 | } |
| 27 | |
| 28 | return $formatted; |
| 29 | } |
| 30 | } |