2
0

String.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * @category Zend
  16. * @package Zend_Pdf
  17. * @subpackage FileParser
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Pdf_FileParserDataSource */
  23. require_once 'Zend/Pdf/FileParserDataSource.php';
  24. /**
  25. * Concrete subclass of {@link Zend_Pdf_FileParserDataSource} that provides an
  26. * interface to binary strings.
  27. *
  28. * @package Zend_Pdf
  29. * @subpackage FileParser
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Pdf_FileParserDataSource_String extends Zend_Pdf_FileParserDataSource
  34. {
  35. /**** Instance Variables ****/
  36. /**
  37. * The string to parse.
  38. * @var string
  39. */
  40. protected $_string = '';
  41. /**** Public Interface ****/
  42. /* Concrete Class Implementation */
  43. /**
  44. * Object constructor.
  45. *
  46. * Verifies that the string is not empty.
  47. *
  48. * @param string $string String to parse.
  49. */
  50. public function __construct($string)
  51. {
  52. if (empty($string)) {
  53. throw new Zend_Pdf_Exception('String is empty',
  54. Zend_Pdf_Exception::PARAMETER_VALUE_OUT_OF_RANGE);
  55. }
  56. $this->_size = strlen($string);
  57. $this->_string = $string;
  58. }
  59. /**
  60. * Object destructor.
  61. */
  62. public function __destruct()
  63. {
  64. $this->_string = '';
  65. }
  66. /**
  67. * Returns the specified number of raw bytes from the string at the byte
  68. * offset of the current read position.
  69. *
  70. * Advances the read position by the number of bytes read.
  71. *
  72. * Throws an exception if there is insufficient data to completely fulfill
  73. * the request.
  74. *
  75. * @param integer $byteCount Number of bytes to read.
  76. * @return string
  77. * @throws Zend_Pdf_Exception
  78. */
  79. public function readBytes($byteCount)
  80. {
  81. if (($this->_offset + $byteCount) > $this->_size) {
  82. throw new Zend_Pdf_Exception("Insufficient data to read $byteCount bytes",
  83. Zend_Pdf_Exception::INSUFFICIENT_DATA);
  84. }
  85. $bytes = substr($this->_string, $this->_offset, $byteCount);
  86. $this->_offset += $byteCount;
  87. return $bytes;
  88. }
  89. /**
  90. * Returns the entire string.
  91. *
  92. * Preserves the current read position.
  93. *
  94. * @return string
  95. */
  96. public function readAllBytes()
  97. {
  98. return $this->_string;
  99. }
  100. /* Object Magic Methods */
  101. /**
  102. * Returns a string containing the parsed string's length.
  103. *
  104. * @return string
  105. */
  106. public function __toString()
  107. {
  108. return "String ($this->_size bytes)";
  109. }
  110. }