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