Value.php 4.0 KB

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