PdfIndirectObject.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * This file is part of FPDI
  4. *
  5. * @package Fpdi
  6. * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
  7. * @license http://opensource.org/licenses/mit-license The MIT License
  8. */
  9. namespace Fpdi\PdfParser\Type;
  10. use Fpdi\PdfParser\PdfParser;
  11. use Fpdi\PdfParser\StreamReader;
  12. use Fpdi\PdfParser\Tokenizer;
  13. /**
  14. * Class representing an indirect object
  15. */
  16. class PdfIndirectObject extends PdfType
  17. {
  18. /**
  19. * Parses an indirect object from a tokenizer, parser and stream-reader.
  20. *
  21. * @param int $objectNumberToken
  22. * @param int $objectGenerationNumberToken
  23. * @param PdfParser $parser
  24. * @param Tokenizer $tokenizer
  25. * @param StreamReader $reader
  26. * @return bool|self
  27. * @throws PdfTypeException
  28. */
  29. public static function parse(
  30. $objectNumberToken,
  31. $objectGenerationNumberToken,
  32. PdfParser $parser,
  33. Tokenizer $tokenizer,
  34. StreamReader $reader
  35. ) {
  36. $value = $parser->readValue();
  37. if ($value === false) {
  38. return false;
  39. }
  40. $nextToken = $tokenizer->getNextToken();
  41. if ($nextToken === 'stream') {
  42. $value = PdfStream::parse($value, $reader, $parser);
  43. } elseif ($nextToken !== false) {
  44. $tokenizer->pushStack($nextToken);
  45. }
  46. $v = new self();
  47. $v->objectNumber = (int) $objectNumberToken;
  48. $v->generationNumber = (int) $objectGenerationNumberToken;
  49. $v->value = $value;
  50. return $v;
  51. }
  52. /**
  53. * Helper method to create an instance.
  54. *
  55. * @param int $objectNumber
  56. * @param int $generationNumber
  57. * @param PdfType $value
  58. * @return self
  59. */
  60. public static function create($objectNumber, $generationNumber, PdfType $value)
  61. {
  62. $v = new self();
  63. $v->objectNumber = (int) $objectNumber;
  64. $v->generationNumber = (int) $generationNumber;
  65. $v->value = $value;
  66. return $v;
  67. }
  68. /**
  69. * Ensures that the passed value is a PdfIndirectObject instance.
  70. *
  71. * @param mixed $indirectObject
  72. * @return self
  73. * @throws PdfTypeException
  74. */
  75. public static function ensure($indirectObject)
  76. {
  77. return PdfType::ensureType(self::class, $indirectObject, 'Indirect object expected.');
  78. }
  79. /**
  80. * The object number.
  81. *
  82. * @var int
  83. */
  84. public $objectNumber;
  85. /**
  86. * The generation number.
  87. *
  88. * @var int
  89. */
  90. public $generationNumber;
  91. }