Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ObjectRegistry
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getAll
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core\File;
6
7use Phpdftk\Pdf\Core\PdfObject;
8
9/**
10 * Tracks all PdfObject instances and assigns sequential object numbers.
11 *
12 * Numbering starts at 1 because object 0 is reserved by the PDF spec as the
13 * free-list head in the cross-reference table (ISO 32000-2 section 7.5.4). Generation
14 * numbers are always 0 for objects in a new document -- non-zero generations
15 * only appear in incremental updates that reuse deleted object numbers.
16 */
17class ObjectRegistry
18{
19    /** @var array<int, PdfObject> */
20    private array $objects = [];
21    private int $nextObjectNumber = 1;
22
23    /**
24     * Register a PdfObject, assign it an object number, and return that number.
25     */
26    public function register(PdfObject $object): int
27    {
28        $objectNumber = $this->nextObjectNumber++;
29        $object->objectNumber = $objectNumber;
30        $object->generationNumber = 0;
31        $this->objects[$objectNumber] = $object;
32        return $objectNumber;
33    }
34
35    /**
36     * Return all registered objects, keyed by object number.
37     *
38     * @return array<int, PdfObject>
39     */
40    public function getAll(): array
41    {
42        return $this->objects;
43    }
44
45    /**
46     * Return the total count of objects including the free list head (object 0).
47     */
48    public function getSize(): int
49    {
50        return $this->nextObjectNumber; // includes object 0
51    }
52}