]> granicus.if.org Git - php/commitdiff
- Make SplObjectStorage implement ArrayAccess
authorMarcus Boerger <helly@php.net>
Mon, 28 Jan 2008 22:43:21 +0000 (22:43 +0000)
committerMarcus Boerger <helly@php.net>
Mon, 28 Jan 2008 22:43:21 +0000 (22:43 +0000)
ext/spl/internal/splobjectstorage.inc
ext/spl/spl_observer.c

index c7a56b43e19b4feeddfd06f206515009b8bb5be7..b65aecaa96e475afd9b8b1d38264dc85dbea2bee 100755 (executable)
@@ -20,7 +20,7 @@
  * here therefore has a complexity of O(n) while the actual implementation has
  * complexity O(1).
  */
-class SplObjectStorage implements Iterator, Countable
+class SplObjectStorage implements Iterator, Countable, ArrayAccess
 {
        private $storage = array();
        private $index = 0;
@@ -88,9 +88,9 @@ class SplObjectStorage implements Iterator, Countable
                return count($this->storage);
        }
 
-       /** @param obj object to look for
+       /** @param $obj object to look for
         * @return whether $obj is contained in storage
-         */
+        */
        function contains($obj)
        {
                if (is_object($obj))
@@ -106,7 +106,9 @@ class SplObjectStorage implements Iterator, Countable
                return false;
        }
 
-       /** @param $obj new object to attach to storage if not yet contained
+       /** @param $obj new object to attach to storage or object whose
+        *              associative information is to be replaced
+        * @param $inf associative information stored along the object
         */
        function attach($obj, $inf = NULL)
        {
@@ -133,6 +135,54 @@ class SplObjectStorage implements Iterator, Countable
                        }
                }
        }
+
+       /** @param $obj new object to attach to storage or object whose
+        *              associative information is to be replaced
+        * @param $inf associative information stored along the object
+        * @since 5.3.0
+        */
+       function offsetSet($obj, $inf)
+       {
+               $this->attach($obj, $inf);
+       }
+
+       /** @param $obj Exising object to look for
+        * @return associative information stored with object
+        * @throw UnexpectedValueException if Object $obj is not contained in
+        *                                 storage
+        * @since 5.3.0
+        */
+       function offsetGet($obj)
+       {
+               if (is_object($obj))
+               {
+                       foreach($this->storage as $idx => $element)
+                       {
+                               if ($object === $element[0])
+                               {
+                                       return $element[1];
+                               }
+                       }
+               }
+               throw new UnexpectedValueException('Object not found');
+       }
+
+       /** @param $obj Exising object to look for
+        * @return associative information stored with object
+        * @since 5.3.0
+        */
+       function offsetUnset($obj)
+       {
+               $this->detach($obj);
+       }
+
+       /** @param $obj object to look for
+        * @return whether $obj is contained in storage
+        */
+       function offsetEsists($obj)
+       {
+               return $this->contains($obj);
+       }
 }
 
 ?>
index 65ee0540c858bdfd76139b627c08df25a64a8c2d..8fc1950abc1cc9f1ab075804ade781f6b0841e08 100755 (executable)
@@ -288,6 +288,25 @@ SPL_METHOD(SplObjectStorage, detach)
        intern->index = 0;
 } /* }}} */
 
+/* {{{ proto mixed SplObjectStorage::offsetGet($object) U
+ Returns associated information for a stored object */
+SPL_METHOD(SplObjectStorage, offsetGet)
+{
+       zval *obj;
+       spl_SplObjectStorageElement *element;
+       spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
+       
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
+               return;
+       }
+       element = spl_object_storage_get(intern, obj TSRMLS_CC);
+       if (!element) {
+               zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Object not found");
+       } else {
+               RETURN_ZVAL(element->inf,1, 0);
+       }
+} /* }}} */
+
 /* {{{ proto bool SplObjectStorage::contains($obj) U
  Determine whethe an object is contained in the storage */
 SPL_METHOD(SplObjectStorage, contains)
@@ -565,20 +584,39 @@ ZEND_BEGIN_ARG_INFO(arginfo_setInfo, 0)
        ZEND_ARG_INFO(0, info)
 ZEND_END_ARG_INFO();
 
+static
+ZEND_BEGIN_ARG_INFO_EX(arginfo_offsetGet, 0, 0, 1)
+       ZEND_ARG_INFO(0, object)
+ZEND_END_ARG_INFO()
+
+static
+ZEND_BEGIN_ARG_INFO_EX(arginfo_offsetSet, 0, 0, 2)
+       ZEND_ARG_INFO(0, object)
+       ZEND_ARG_INFO(0, info)
+ZEND_END_ARG_INFO()
+
 static const zend_function_entry spl_funcs_SplObjectStorage[] = {
        SPL_ME(SplObjectStorage,  attach,      arginfo_attach,        0)
        SPL_ME(SplObjectStorage,  detach,      arginfo_Object,        0)
        SPL_ME(SplObjectStorage,  contains,    arginfo_Object,        0)
+       SPL_ME(SplObjectStorage,  getInfo,     NULL,                  0)
+       SPL_ME(SplObjectStorage,  setInfo,     arginfo_setInfo,       0)
+       /* Countable */
        SPL_ME(SplObjectStorage,  count,       NULL,                  0)
+       /* Iterator */
        SPL_ME(SplObjectStorage,  rewind,      NULL,                  0)
        SPL_ME(SplObjectStorage,  valid,       NULL,                  0)
        SPL_ME(SplObjectStorage,  key,         NULL,                  0)
        SPL_ME(SplObjectStorage,  current,     NULL,                  0)
        SPL_ME(SplObjectStorage,  next,        NULL,                  0)
+       /* Serializable */
        SPL_ME(SplObjectStorage,  unserialize, arginfo_Serialized,    0)
        SPL_ME(SplObjectStorage,  serialize,   NULL,                  0)
-       SPL_ME(SplObjectStorage,  getInfo,     NULL,                  0)
-       SPL_ME(SplObjectStorage,  setInfo,     arginfo_setInfo,       0)
+       /* ArrayAccess */
+       SPL_MA(SplObjectStorage, offsetExists, SplObjectStorage, contains, arginfo_offsetGet, 0)
+       SPL_MA(SplObjectStorage, offsetSet,    SplObjectStorage, attach,   arginfo_offsetSet, 0)
+       SPL_MA(SplObjectStorage, offsetUnset,  SplObjectStorage, detach,   arginfo_offsetGet, 0)
+       SPL_ME(SplObjectStorage, offsetGet,    arginfo_offsetGet,     0)
        {NULL, NULL, NULL}
 };
 
@@ -595,6 +633,7 @@ PHP_MINIT_FUNCTION(spl_observer)
        REGISTER_SPL_IMPLEMENTS(SplObjectStorage, Countable);
        REGISTER_SPL_IMPLEMENTS(SplObjectStorage, Iterator);
        REGISTER_SPL_IMPLEMENTS(SplObjectStorage, Serializable);
+       REGISTER_SPL_IMPLEMENTS(SplObjectStorage, ArrayAccess);
 
        return SUCCESS;
 }