File indexing completed on 2024-12-22 05:36:52

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Memory
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 
0023 /**
0024  * String value object
0025  *
0026  * It's an OO string wrapper.
0027  * Used to intercept string updates.
0028  *
0029  * @category   Zend
0030  * @package    Zend_Memory
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  * @todo       also implement Countable for PHP 5.1 but not yet to stay 5.0 compatible
0034  */
0035 class Zend_Memory_Value implements ArrayAccess {
0036     /**
0037      * Value
0038      *
0039      * @var string
0040      */
0041     private $_value;
0042 
0043     /**
0044      * Container
0045      *
0046      * @var Zend_Memory_Container_Interface
0047      */
0048     private $_container;
0049 
0050     /**
0051      * Boolean flag which signals to trace value modifications
0052      *
0053      * @var boolean
0054      */
0055     private $_trace;
0056 
0057 
0058     /**
0059      * Object constructor
0060      *
0061      * @param string $value
0062      * @param Zend_Memory_Container_Movable $container
0063      */
0064     public function __construct($value, Zend_Memory_Container_Movable $container)
0065     {
0066         $this->_container = $container;
0067 
0068         $this->_value = (string)$value;
0069 
0070         /**
0071          * Object is marked as just modified by memory manager
0072          * So we don't need to trace followed object modifications and
0073          * object is processed (and marked as traced) when another
0074          * memory object is modified.
0075          *
0076          * It reduces overall numberr of calls necessary to modification trace
0077          */
0078         $this->_trace = false;
0079     }
0080 
0081 
0082     /**
0083      * ArrayAccess interface method
0084      * returns true if string offset exists
0085      *
0086      * @param integer $offset
0087      * @return boolean
0088      */
0089     public function offsetExists($offset)
0090     {
0091         return $offset >= 0  &&  $offset < strlen($this->_value);
0092     }
0093 
0094     /**
0095      * ArrayAccess interface method
0096      * Get character at $offset position
0097      *
0098      * @param integer $offset
0099      * @return string
0100      */
0101     public function offsetGet($offset)
0102     {
0103         return $this->_value[$offset];
0104     }
0105 
0106     /**
0107      * ArrayAccess interface method
0108      * Set character at $offset position
0109      *
0110      * @param integer $offset
0111      * @param string $char
0112      */
0113     public function offsetSet($offset, $char)
0114     {
0115         $this->_value[$offset] = $char;
0116 
0117         if ($this->_trace) {
0118             $this->_trace = false;
0119             $this->_container->processUpdate();
0120         }
0121     }
0122 
0123     /**
0124      * ArrayAccess interface method
0125      * Unset character at $offset position
0126      *
0127      * @param integer $offset
0128      */
0129     public function offsetUnset($offset)
0130     {
0131         unset($this->_value[$offset]);
0132 
0133         if ($this->_trace) {
0134             $this->_trace = false;
0135             $this->_container->processUpdate();
0136         }
0137     }
0138 
0139 
0140     /**
0141      * To string conversion
0142      *
0143      * @return string
0144      */
0145     public function __toString()
0146     {
0147         return $this->_value;
0148     }
0149 
0150 
0151     /**
0152      * Get string value reference
0153      *
0154      * _Must_ be used for value access before PHP v 5.2
0155      * or _may_ be used for performance considerations
0156      *
0157      * @internal
0158      * @return string
0159      */
0160     public function &getRef()
0161     {
0162         return $this->_value;
0163     }
0164 
0165     /**
0166      * Start modifications trace
0167      *
0168      * _Must_ be used for value access before PHP v 5.2
0169      * or _may_ be used for performance considerations
0170      *
0171      * @internal
0172      */
0173     public function startTrace()
0174     {
0175         $this->_trace = true;
0176     }
0177 }