Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
12 / 15
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PdfDictionary
80.00% covered (warning)
80.00%
12 / 15
80.00% covered (warning)
80.00%
4 / 5
11.97
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 has
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toPdf
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
8.32
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core;
6
7/**
8 * Represents a PDF dictionary object: << /Key value ... >>
9 *
10 * Keys are plain strings (no leading slash needed; added during serialization).
11 * Values may be any Serializable, scalar int/float/bool, or null.
12 */
13class PdfDictionary implements Serializable
14{
15    /** @param array<string, mixed> $entries */
16    public function __construct(public array $entries = []) {}
17
18    /**
19     * Set or replace an entry. Returns $this for fluent chaining.
20     */
21    public function set(string $key, mixed $value): self
22    {
23        $this->entries[$key] = $value;
24        return $this;
25    }
26
27    /**
28     * Get an entry value by key.
29     */
30    public function get(string $key): mixed
31    {
32        return $this->entries[$key] ?? null;
33    }
34
35    /**
36     * Check whether a key exists.
37     */
38    public function has(string $key): bool
39    {
40        return array_key_exists($key, $this->entries);
41    }
42
43    public function toPdf(): string
44    {
45        $lines = ['<<'];
46        foreach ($this->entries as $key => $value) {
47            $serialized = match (true) {
48                $value instanceof Serializable => $value->toPdf(),
49                is_int($value), is_float($value) => (new PdfNumber($value))->toPdf(),
50                is_bool($value) => (new PdfBoolean($value))->toPdf(),
51                is_null($value) => 'null',
52                default => (string) $value,
53            };
54            $lines[] = '/' . $key . ' ' . $serialized;
55        }
56        $lines[] = '>>';
57
58        return implode("\n", $lines);
59    }
60}