PdfHexString.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\StreamReader;
  11. /**
  12. * Class representing a hexadecimal encoded PDF string object
  13. */
  14. class PdfHexString extends PdfType
  15. {
  16. /**
  17. * Parses a hexadecimal string object from the stream reader.
  18. *
  19. * @param StreamReader $streamReader
  20. * @return bool|self
  21. */
  22. public static function parse(StreamReader $streamReader)
  23. {
  24. $bufferOffset = $streamReader->getOffset();
  25. while (true) {
  26. $buffer = $streamReader->getBuffer(false);
  27. $pos = \strpos($buffer, '>', $bufferOffset);
  28. if ($pos === false) {
  29. if (!$streamReader->increaseLength()) {
  30. return false;
  31. }
  32. continue;
  33. }
  34. break;
  35. }
  36. $result = \substr($buffer, $bufferOffset, $pos - $bufferOffset);
  37. $streamReader->setOffset($pos + 1);
  38. $v = new self();
  39. $v->value = $result;
  40. return $v;
  41. }
  42. /**
  43. * Helper method to create an instance.
  44. *
  45. * @param string $string The hex encoded string.
  46. * @return self
  47. */
  48. public static function create($string)
  49. {
  50. $v = new self();
  51. $v->value = $string;
  52. return $v;
  53. }
  54. /**
  55. * Ensures that the passed value is a PdfHexString instance.
  56. *
  57. * @param mixed $hexString
  58. * @return self
  59. * @throws PdfTypeException
  60. */
  61. public static function ensure($hexString)
  62. {
  63. return PdfType::ensureType(self::class, $hexString, 'Hex string value expected.');
  64. }
  65. }