Value.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_Memory
  16. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. /**
  20. * String value object
  21. *
  22. * It's an OO string wrapper.
  23. * Used to intercept string updates.
  24. *
  25. * @package Zend_Memory
  26. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. * @todo also implement Countable for PHP 5.1 but not yet to stay 5.0 compatible
  29. */
  30. class Zend_Memory_Value implements ArrayAccess {
  31. /**
  32. * Value
  33. *
  34. * @var string
  35. */
  36. private $_value;
  37. /**
  38. * Container
  39. *
  40. * @var Zend_Memory_Container_Interface
  41. */
  42. private $_container;
  43. /**
  44. * Boolean flag which signals to trace value modifications
  45. *
  46. * @var boolean
  47. */
  48. private $_trace;
  49. /**
  50. * Object constructor
  51. *
  52. * @param string $value
  53. * @param Zend_Memory_Container_Movable $container
  54. */
  55. public function __construct($value, Zend_Memory_Container_Movable $container)
  56. {
  57. $this->_container = $container;
  58. $this->_value = (string)$value;
  59. /**
  60. * Object is marked as just modified by memory manager
  61. * So we don't need to trace followed object modifications and
  62. * object is processed (and marked as traced) when another
  63. * memory object is modified.
  64. *
  65. * It reduces overall numberr of calls necessary to modification trace
  66. */
  67. $this->_trace = false;
  68. }
  69. /**
  70. * ArrayAccess interface method
  71. * returns true if string offset exists
  72. *
  73. * @param integer $offset
  74. * @return boolean
  75. */
  76. public function offsetExists($offset)
  77. {
  78. return $offset >= 0 && $offset < strlen($this->_value);
  79. }
  80. /**
  81. * ArrayAccess interface method
  82. * Get character at $offset position
  83. *
  84. * @param integer $offset
  85. * @return string
  86. */
  87. public function offsetGet($offset)
  88. {
  89. return $this->_value[$offset];
  90. }
  91. /**
  92. * ArrayAccess interface method
  93. * Set character at $offset position
  94. *
  95. * @param integer $offset
  96. * @param string $char
  97. */
  98. public function offsetSet($offset, $char)
  99. {
  100. $this->_value[$offset] = $char;
  101. if ($this->_trace) {
  102. $this->_trace = false;
  103. $this->_container->processUpdate();
  104. }
  105. }
  106. /**
  107. * ArrayAccess interface method
  108. * Unset character at $offset position
  109. *
  110. * @param integer $offset
  111. */
  112. public function offsetUnset($offset)
  113. {
  114. unset($this->_value[$offset]);
  115. if ($this->_trace) {
  116. $this->_trace = false;
  117. $this->_container->processUpdate();
  118. }
  119. }
  120. /**
  121. * To string conversion
  122. *
  123. * @return string
  124. */
  125. public function __toString()
  126. {
  127. return $this->_value;
  128. }
  129. /**
  130. * Get string value reference
  131. *
  132. * _Must_ be used for value access before PHP v 5.2
  133. * or _may_ be used for performance considerations
  134. *
  135. * @internal
  136. * @return string
  137. */
  138. public function &getRef()
  139. {
  140. return $this->_value;
  141. }
  142. /**
  143. * Start modifications trace
  144. *
  145. * _Must_ be used for value access before PHP v 5.2
  146. * or _may_ be used for performance considerations
  147. *
  148. * @internal
  149. */
  150. public function startTrace()
  151. {
  152. $this->_trace = true;
  153. }
  154. }