PdfType.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\CrossReference\CrossReferenceException;
  11. use Fpdi\PdfParser\PdfParser;
  12. use Fpdi\PdfParser\PdfParserException;
  13. /**
  14. * A class defining a PDF data type
  15. */
  16. class PdfType
  17. {
  18. /**
  19. * Resolves a PdfType value to its value.
  20. *
  21. * This method is used to evaluate indirect and direct object references until a final value is reached.
  22. *
  23. * @param PdfType $value
  24. * @param PdfParser $parser
  25. * @param bool $stopAtIndirectObject
  26. * @return PdfType
  27. * @throws CrossReferenceException
  28. * @throws PdfParserException
  29. */
  30. public static function resolve(PdfType $value, PdfParser $parser, $stopAtIndirectObject = false)
  31. {
  32. if ($value instanceof PdfIndirectObject) {
  33. if ($stopAtIndirectObject === true) {
  34. return $value;
  35. }
  36. return self::resolve($value->value, $parser, $stopAtIndirectObject);
  37. }
  38. if ($value instanceof PdfIndirectObjectReference) {
  39. return self::resolve($parser->getIndirectObject($value->value), $parser, $stopAtIndirectObject);
  40. }
  41. return $value;
  42. }
  43. /**
  44. * Ensure that a value is an instance of a specific PDF type.
  45. *
  46. * @param string $type
  47. * @param PdfType $value
  48. * @param string $errorMessage
  49. * @return mixed
  50. * @throws PdfTypeException
  51. */
  52. protected static function ensureType($type, $value, $errorMessage)
  53. {
  54. if (!($value instanceof $type)) {
  55. throw new PdfTypeException(
  56. $errorMessage,
  57. PdfTypeException::INVALID_DATA_TYPE
  58. );
  59. }
  60. return $value;
  61. }
  62. /**
  63. * The value of the PDF type.
  64. *
  65. * @var mixed
  66. */
  67. public $value;
  68. }