PageRenderTime 60ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Pdf/BinaryParser/DataSource/String.php

https://bitbucket.org/aboozar/zf2
PHP | 112 lines | 37 code | 17 blank | 58 comment | 2 complexity | 8441541dd11df687b96fbf4b605615e3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Pdf
  9. */
  10. namespace Zend\Pdf\BinaryParser\DataSource;
  11. use Zend\Pdf;
  12. use Zend\Pdf\Exception;
  13. /**
  14. * Concrete subclass of {@link \Zend\Pdf\BinaryParser\DataSource\AbstractDataSource}
  15. * that provides an interface to binary strings.
  16. *
  17. * @package Zend_PDF
  18. * @subpackage Zend_PDF_BinaryParser
  19. */
  20. class String extends AbstractDataSource
  21. {
  22. /**** Instance Variables ****/
  23. /**
  24. * The string to parse.
  25. * @var string
  26. */
  27. protected $_string = '';
  28. /**** Public Interface ****/
  29. /* Concrete Class Implementation */
  30. /**
  31. * Object constructor.
  32. *
  33. * Verifies that the string is not empty.
  34. *
  35. * @param string $string String to parse.
  36. */
  37. public function __construct($string)
  38. {
  39. if (empty($string)) {
  40. throw new Exception\InvalidArgumentException('String is empty');
  41. }
  42. $this->_size = strlen($string);
  43. $this->_string = $string;
  44. }
  45. /**
  46. * Object destructor.
  47. */
  48. public function __destruct()
  49. {
  50. $this->_string = '';
  51. }
  52. /**
  53. * Returns the specified number of raw bytes from the string at the byte
  54. * offset of the current read position.
  55. *
  56. * Advances the read position by the number of bytes read.
  57. *
  58. * Throws an exception if there is insufficient data to completely fulfill
  59. * the request.
  60. *
  61. * @param integer $byteCount Number of bytes to read.
  62. * @return string
  63. * @throws \Zend\Pdf\Exception\ExceptionInterface
  64. */
  65. public function readBytes($byteCount)
  66. {
  67. if (($this->_offset + $byteCount) > $this->_size) {
  68. throw new Exception\LengthException("Insufficient data to read $byteCount bytes");
  69. }
  70. $bytes = substr($this->_string, $this->_offset, $byteCount);
  71. $this->_offset += $byteCount;
  72. return $bytes;
  73. }
  74. /**
  75. * Returns the entire string.
  76. *
  77. * Preserves the current read position.
  78. *
  79. * @return string
  80. */
  81. public function readAllBytes()
  82. {
  83. return $this->_string;
  84. }
  85. /* Object Magic Methods */
  86. /**
  87. * Returns a string containing the parsed string's length.
  88. *
  89. * @return string
  90. */
  91. public function __toString()
  92. {
  93. return "String ($this->_size bytes)";
  94. }
  95. }