@@ -83,6 +83,8 @@ zend_class_entry* phongo_exception_from_phongo_domain(php_phongo_error_domain_t
8383 switch (domain ) {
8484 case PHONGO_ERROR_INVALID_ARGUMENT :
8585 return php_phongo_invalidargumentexception_ce ;
86+ case PHONGO_ERROR_LOGIC :
87+ return php_phongo_logicexception_ce ;
8688 case PHONGO_ERROR_RUNTIME :
8789 return php_phongo_runtimeexception_ce ;
8890 case PHONGO_ERROR_UNEXPECTED_VALUE :
@@ -1671,6 +1673,11 @@ static void php_phongo_cursor_iterator_rewind(zend_object_iterator *iter TSRMLS_
16711673 php_phongo_cursor_t * cursor = cursor_it -> cursor ;
16721674 const bson_t * doc ;
16731675
1676+ if (cursor_it -> current > 0 ) {
1677+ phongo_throw_exception (PHONGO_ERROR_LOGIC TSRMLS_CC , "Cursors cannot rewind after starting iteration" );
1678+ return ;
1679+ }
1680+
16741681 php_phongo_cursor_free_current (cursor );
16751682
16761683 doc = mongoc_cursor_current (cursor -> cursor );
@@ -1694,18 +1701,26 @@ zend_object_iterator_funcs php_phongo_cursor_iterator_funcs = {
16941701
16951702zend_object_iterator * php_phongo_cursor_get_iterator (zend_class_entry * ce , zval * object , int by_ref TSRMLS_DC ) /* {{{ */
16961703{
1704+ php_phongo_cursor_t * cursor = zend_object_store_get_object (object TSRMLS_CC );
16971705 php_phongo_cursor_iterator * cursor_it = NULL ;
16981706
16991707 if (by_ref ) {
17001708 zend_error (E_ERROR , "An iterator cannot be used with foreach by reference" );
17011709 }
17021710
1711+ if (cursor -> got_iterator ) {
1712+ phongo_throw_exception (PHONGO_ERROR_LOGIC TSRMLS_CC , "Cursors cannot yield multiple iterators" );
1713+ return NULL ;
1714+ }
1715+
1716+ cursor -> got_iterator = 1 ;
1717+
17031718 cursor_it = ecalloc (1 , sizeof (php_phongo_cursor_iterator ));
17041719
17051720 Z_ADDREF_P (object );
17061721 cursor_it -> intern .data = (void * )object ;
17071722 cursor_it -> intern .funcs = & php_phongo_cursor_iterator_funcs ;
1708- cursor_it -> cursor = ( php_phongo_cursor_t * ) zend_object_store_get_object ( object TSRMLS_CC ) ;
1723+ cursor_it -> cursor = cursor ;
17091724 /* cursor_it->current should already be allocated to zero */
17101725
17111726 php_phongo_cursor_free_current (cursor_it -> cursor );
@@ -1818,6 +1833,7 @@ PHP_MINIT_FUNCTION(mongodb)
18181833 PHP_MINIT (WriteResult )(INIT_FUNC_ARGS_PASSTHRU );
18191834
18201835 PHP_MINIT (Exception )(INIT_FUNC_ARGS_PASSTHRU );
1836+ PHP_MINIT (LogicException )(INIT_FUNC_ARGS_PASSTHRU );
18211837 PHP_MINIT (RuntimeException )(INIT_FUNC_ARGS_PASSTHRU );
18221838 PHP_MINIT (UnexpectedValueException )(INIT_FUNC_ARGS_PASSTHRU );
18231839 PHP_MINIT (InvalidArgumentException )(INIT_FUNC_ARGS_PASSTHRU );
0 commit comments