/framework/vendor/zend/Zend/Memory/Value.php

http://zoop.googlecode.com/ · PHP · 177 lines · 48 code · 20 blank · 109 comment · 3 complexity · 7fa1d907e8b5ec9b03fcb9e3115ecc6d MD5 · raw file

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