String.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @package Zend_Pdf
  16. * @subpackage FileParser
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Pdf_FileParserDataSource */
  21. require_once 'Zend/Pdf/FileParserDataSource.php';
  22. /**
  23. * Concrete subclass of {@link Zend_Pdf_FileParserDataSource} that provides an
  24. * interface to binary strings.
  25. *
  26. * @package Zend_Pdf
  27. * @subpackage FileParser
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Pdf_FileParserDataSource_String extends Zend_Pdf_FileParserDataSource
  32. {
  33. /**** Instance Variables ****/
  34. /**
  35. * The string to parse.
  36. * @var string
  37. */
  38. protected $_string = '';
  39. /**** Public Interface ****/
  40. /* Concrete Class Implementation */
  41. /**
  42. * Object constructor.
  43. *
  44. * Verifies that the string is not empty.
  45. *
  46. * @param string $string String to parse.
  47. */
  48. public function __construct($string)
  49. {
  50. if (empty($string)) {
  51. throw new Zend_Pdf_Exception('String is empty',
  52. Zend_Pdf_Exception::PARAMETER_VALUE_OUT_OF_RANGE);
  53. }
  54. $this->_size = strlen($string);
  55. $this->_string = $string;
  56. }
  57. /**
  58. * Object destructor.
  59. */
  60. public function __destruct()
  61. {
  62. $this->_string = '';
  63. }
  64. /**
  65. * Returns the specified number of raw bytes from the string at the byte
  66. * offset of the current read position.
  67. *
  68. * Advances the read position by the number of bytes read.
  69. *
  70. * Throws an exception if there is insufficient data to completely fulfill
  71. * the request.
  72. *
  73. * @param integer $byteCount Number of bytes to read.
  74. * @return string
  75. * @throws Zend_Pdf_Exception
  76. */
  77. public function readBytes($byteCount)
  78. {
  79. if (($this->_offset + $byteCount) > $this->_size) {
  80. throw new Zend_Pdf_Exception("Insufficient data to read $byteCount bytes",
  81. Zend_Pdf_Exception::INSUFFICIENT_DATA);
  82. }
  83. $bytes = substr($this->_string, $this->_offset, $byteCount);
  84. $this->_offset += $byteCount;
  85. return $bytes;
  86. }
  87. /**
  88. * Returns the entire string.
  89. *
  90. * Preserves the current read position.
  91. *
  92. * @return string
  93. */
  94. public function readAllBytes()
  95. {
  96. return $this->_string;
  97. }
  98. /* Object Magic Methods */
  99. /**
  100. * Returns a string containing the parsed string's length.
  101. *
  102. * @return string
  103. */
  104. public function __toString()
  105. {
  106. return "String ($this->_size bytes)";
  107. }
  108. }