colder                                   Wed, 05 Jan 2011 15:01:18 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=307129

Log:
Implement SplObjectStorage::removeAllExcept (Patch by Matthey Turland)

Changed paths:
    U   php/php-src/branches/PHP_5_3/ext/spl/spl_observer.c
    A   
php/php-src/branches/PHP_5_3/ext/spl/tests/SplObjectStorage_removeAllExcept_basic.phpt
    A   
php/php-src/branches/PHP_5_3/ext/spl/tests/SplObjectStorage_removeAllExcept_invalid_parameter.phpt
    U   php/php-src/trunk/ext/spl/spl_observer.c
    A   
php/php-src/trunk/ext/spl/tests/SplObjectStorage_removeAllExcept_basic.phpt
    A   
php/php-src/trunk/ext/spl/tests/SplObjectStorage_removeAllExcept_invalid_parameter.phpt

Modified: php/php-src/branches/PHP_5_3/ext/spl/spl_observer.c
===================================================================
--- php/php-src/branches/PHP_5_3/ext/spl/spl_observer.c	2011-01-05 14:57:13 UTC (rev 307128)
+++ php/php-src/branches/PHP_5_3/ext/spl/spl_observer.c	2011-01-05 15:01:18 UTC (rev 307129)
@@ -485,6 +485,36 @@
 	RETURN_LONG(zend_hash_num_elements(&intern->storage));
 } /* }}} */

+/* {{{ proto bool SplObjectStorage::removeAllExcept(SplObjectStorage $os)
+ Remove elements not common to both this SplObjectStorage instance and $os */
+SPL_METHOD(SplObjectStorage, removeAllExcept)
+{
+	zval *obj;
+	spl_SplObjectStorage *intern = (spl_SplObjectStorage *)zend_object_store_get_object(getThis() TSRMLS_CC);
+	spl_SplObjectStorage *other;
+	spl_SplObjectStorageElement *element;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
+		return;
+	}
+
+	other = (spl_SplObjectStorage *)zend_object_store_get_object(obj TSRMLS_CC);
+
+	zend_hash_internal_pointer_reset(&intern->storage);
+	while (zend_hash_get_current_data(&intern->storage, (void **)&element) == SUCCESS) {
+		if (!spl_object_storage_contains(other, element->obj TSRMLS_CC)) {
+			spl_object_storage_detach(intern, element->obj TSRMLS_CC);
+		}
+		zend_hash_move_forward(&intern->storage);
+	}
+
+	zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
+	intern->index = 0;
+
+	RETURN_LONG(zend_hash_num_elements(&intern->storage));
+}
+/* }}} */
+
 /* {{{ proto bool SplObjectStorage::contains($obj)
  Determine whethe an object is contained in the storage */
 SPL_METHOD(SplObjectStorage, contains)
@@ -827,6 +857,7 @@
 	SPL_ME(SplObjectStorage,  contains,    arginfo_Object,        0)
 	SPL_ME(SplObjectStorage,  addAll,      arginfo_Object,        0)
 	SPL_ME(SplObjectStorage,  removeAll,   arginfo_Object,        0)
+	SPL_ME(SplObjectStorage,  removeAllExcept, arginfo_Object,    0)
 	SPL_ME(SplObjectStorage,  getInfo,     arginfo_splobject_void,0)
 	SPL_ME(SplObjectStorage,  setInfo,     arginfo_setInfo,       0)
 	/* Countable */

Added: php/php-src/branches/PHP_5_3/ext/spl/tests/SplObjectStorage_removeAllExcept_basic.phpt
===================================================================
--- php/php-src/branches/PHP_5_3/ext/spl/tests/SplObjectStorage_removeAllExcept_basic.phpt	                        (rev 0)
+++ php/php-src/branches/PHP_5_3/ext/spl/tests/SplObjectStorage_removeAllExcept_basic.phpt	2011-01-05 15:01:18 UTC (rev 307129)
@@ -0,0 +1,27 @@
+--TEST--
+Check that SplObjectStorage::removeUncommon functions when receiving proper input
+--CREDITS--
+Matthew Turland (m...@matthewturland.com)
+--FILE--
+<?php
+
+    $a = (object) 'a';
+    $b = (object) 'b';
+    $c = (object) 'c';
+
+   $foo = new SplObjectStorage;
+    $foo->attach($a);
+    $foo->attach($b);
+
+    $bar = new SplObjectStorage;
+    $bar->attach($b);
+    $bar->attach($c);
+
+    $foo->removeAllExcept($bar);
+    var_dump($foo->contains($a));
+    var_dump($foo->contains($b));
+
+?>
+--EXPECT--
+bool(false)
+bool(true)

Added: php/php-src/branches/PHP_5_3/ext/spl/tests/SplObjectStorage_removeAllExcept_invalid_parameter.phpt
===================================================================
--- php/php-src/branches/PHP_5_3/ext/spl/tests/SplObjectStorage_removeAllExcept_invalid_parameter.phpt	                        (rev 0)
+++ php/php-src/branches/PHP_5_3/ext/spl/tests/SplObjectStorage_removeAllExcept_invalid_parameter.phpt	2011-01-05 15:01:18 UTC (rev 307129)
@@ -0,0 +1,44 @@
+--TEST--
+Check that SplObjectStorage::removeAllExcept generate a warning and returns NULL when passed non-object param
+--CREDITS--
+Matthew Turland (m...@matthewturland.com)
+Based on work done at PHPNW Testfest 2009 by Simon Westcott (swestc...@gmail.com)
+--FILE--
+<?php
+
+$data_provider = array(
+   array(),
+   true,
+   "string",
+   12345,
+   1.2345,
+   NULL
+);
+
+foreach($data_provider as $input) {
+
+   $s = new SplObjectStorage();
+
+   var_dump($s->removeAllExcept($input));
+}
+
+?>
+--EXPECTF--
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, array given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, boolean given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, %unicode_string_optional% given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, integer given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, double given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, null given in %s on line %d
+NULL
+

Modified: php/php-src/trunk/ext/spl/spl_observer.c
===================================================================
--- php/php-src/trunk/ext/spl/spl_observer.c	2011-01-05 14:57:13 UTC (rev 307128)
+++ php/php-src/trunk/ext/spl/spl_observer.c	2011-01-05 15:01:18 UTC (rev 307129)
@@ -580,6 +580,36 @@
 	RETURN_LONG(zend_hash_num_elements(&intern->storage));
 } /* }}} */

+/* {{{ proto bool SplObjectStorage::removeAllExcept(SplObjectStorage $os)
+ Remove elements not common to both this SplObjectStorage instance and $os */
+SPL_METHOD(SplObjectStorage, removeAllExcept)
+{
+	zval *obj;
+	spl_SplObjectStorage *intern = (spl_SplObjectStorage *)zend_object_store_get_object(getThis() TSRMLS_CC);
+	spl_SplObjectStorage *other;
+	spl_SplObjectStorageElement *element;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
+		return;
+	}
+
+	other = (spl_SplObjectStorage *)zend_object_store_get_object(obj TSRMLS_CC);
+
+	zend_hash_internal_pointer_reset(&intern->storage);
+	while (zend_hash_get_current_data(&intern->storage, (void **)&element) == SUCCESS) {
+		if (!spl_object_storage_contains(other, getThis(), element->obj TSRMLS_CC)) {
+			spl_object_storage_detach(intern, getThis(), element->obj TSRMLS_CC);
+		}
+		zend_hash_move_forward(&intern->storage);
+	}
+
+	zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
+	intern->index = 0;
+
+	RETURN_LONG(zend_hash_num_elements(&intern->storage));
+}
+/* }}} */
+
 /* {{{ proto bool SplObjectStorage::contains($obj)
  Determine whethe an object is contained in the storage */
 SPL_METHOD(SplObjectStorage, contains)
@@ -940,6 +970,7 @@
 	SPL_ME(SplObjectStorage,  contains,    arginfo_Object,        0)
 	SPL_ME(SplObjectStorage,  addAll,      arginfo_Object,        0)
 	SPL_ME(SplObjectStorage,  removeAll,   arginfo_Object,        0)
+	SPL_ME(SplObjectStorage,  removeAllExcept,   arginfo_Object,  0)
 	SPL_ME(SplObjectStorage,  getInfo,     arginfo_splobject_void,0)
 	SPL_ME(SplObjectStorage,  setInfo,     arginfo_setInfo,       0)
 	SPL_ME(SplObjectStorage,  getHash,     arginfo_getHash,       0)

Added: php/php-src/trunk/ext/spl/tests/SplObjectStorage_removeAllExcept_basic.phpt
===================================================================
--- php/php-src/trunk/ext/spl/tests/SplObjectStorage_removeAllExcept_basic.phpt	                        (rev 0)
+++ php/php-src/trunk/ext/spl/tests/SplObjectStorage_removeAllExcept_basic.phpt	2011-01-05 15:01:18 UTC (rev 307129)
@@ -0,0 +1,27 @@
+--TEST--
+Check that SplObjectStorage::removeUncommon functions when receiving proper input
+--CREDITS--
+Matthew Turland (m...@matthewturland.com)
+--FILE--
+<?php
+
+    $a = (object) 'a';
+    $b = (object) 'b';
+    $c = (object) 'c';
+
+   $foo = new SplObjectStorage;
+    $foo->attach($a);
+    $foo->attach($b);
+
+    $bar = new SplObjectStorage;
+    $bar->attach($b);
+    $bar->attach($c);
+
+    $foo->removeAllExcept($bar);
+    var_dump($foo->contains($a));
+    var_dump($foo->contains($b));
+
+?>
+--EXPECT--
+bool(false)
+bool(true)

Added: php/php-src/trunk/ext/spl/tests/SplObjectStorage_removeAllExcept_invalid_parameter.phpt
===================================================================
--- php/php-src/trunk/ext/spl/tests/SplObjectStorage_removeAllExcept_invalid_parameter.phpt	                        (rev 0)
+++ php/php-src/trunk/ext/spl/tests/SplObjectStorage_removeAllExcept_invalid_parameter.phpt	2011-01-05 15:01:18 UTC (rev 307129)
@@ -0,0 +1,44 @@
+--TEST--
+Check that SplObjectStorage::removeAllExcept generate a warning and returns NULL when passed non-object param
+--CREDITS--
+Matthew Turland (m...@matthewturland.com)
+Based on work done at PHPNW Testfest 2009 by Simon Westcott (swestc...@gmail.com)
+--FILE--
+<?php
+
+$data_provider = array(
+   array(),
+   true,
+   "string",
+   12345,
+   1.2345,
+   NULL
+);
+
+foreach($data_provider as $input) {
+
+   $s = new SplObjectStorage();
+
+   var_dump($s->removeAllExcept($input));
+}
+
+?>
+--EXPECTF--
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, array given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, boolean given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, %unicode_string_optional% given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, integer given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, double given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeAllExcept() expects parameter 1 to be SplObjectStorage, null given in %s on line %d
+NULL
+
-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to