Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
6 / 7 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| PdfNumber | |
85.71% |
6 / 7 |
|
50.00% |
1 / 2 |
5.07 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toPdf | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
4.07 | |||
| 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 | } |