Skip to content

Added recursivetreeiterator::setPostfix() method #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion ext/spl/spl_iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ typedef struct _spl_recursive_it_object {
zend_function *nextElement;
zend_class_entry *ce;
smart_str prefix[6];
smart_str postfix[1];
} spl_recursive_it_object;

typedef struct _spl_recursive_it_iterator {
Expand Down Expand Up @@ -900,6 +901,8 @@ static void spl_RecursiveIteratorIterator_free_storage(void *_object TSRMLS_DC)
smart_str_free(&object->prefix[4]);
smart_str_free(&object->prefix[5]);

smart_str_free(&object->postfix[0]);

efree(object);
}
/* }}} */
Expand All @@ -920,6 +923,8 @@ static zend_object_value spl_RecursiveIteratorIterator_new_ex(zend_class_entry *
smart_str_appendl(&intern->prefix[3], "|-", 2);
smart_str_appendl(&intern->prefix[4], "\\-", 2);
smart_str_appendl(&intern->prefix[5], "", 0);

smart_str_appendl(&intern->postfix[0], "", 0);
}

zend_object_std_init(&intern->std, class_type TSRMLS_CC);
Expand Down Expand Up @@ -1039,7 +1044,11 @@ static void spl_recursive_tree_iterator_get_entry(spl_recursive_it_object * obje

static void spl_recursive_tree_iterator_get_postfix(spl_recursive_it_object * object, zval * return_value TSRMLS_DC)
{
RETVAL_STRINGL("", 0, 1);
smart_str str = {0};
smart_str_appendl(&str, object->postfix[0].c, object->postfix[0].len);
smart_str_0(&str);

RETVAL_STRINGL(str.c, str.len, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't you just return a copy of object->postfix[0].c?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, you could. The code is pretty much taken from the get_prefix() to make it easier to implement the same structure as the prefix in a later stage.

}

/* {{{ proto void RecursiveTreeIterator::__construct(RecursiveIterator|IteratorAggregate it [, int flags = RTIT_BYPASS_KEY [, int cit_flags = CIT_CATCH_GET_CHILD [, mode = RIT_SELF_FIRST ]]]) throws InvalidArgumentException
Expand Down Expand Up @@ -1082,6 +1091,22 @@ SPL_METHOD(RecursiveTreeIterator, getPrefix)
spl_recursive_tree_iterator_get_prefix(object, return_value TSRMLS_CC);
} /* }}} */

/* {{{ proto void RecursiveTreeIterator::setPostfix(string prefix)
Sets postfix as used in getPostfix() */
SPL_METHOD(RecursiveTreeIterator, setPostfix)
{
spl_recursive_it_object *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
char* postfix;
int postfix_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &postfix, &postfix_len) == FAILURE) {
return;
}

smart_str_free(&object->postfix[0]);
smart_str_appendl(&object->postfix[0], postfix, postfix_len);
} /* }}} */

/* {{{ proto string RecursiveTreeIterator::getEntry()
Returns the string presentation built for current element */
SPL_METHOD(RecursiveTreeIterator, getEntry)
Expand Down Expand Up @@ -1255,6 +1280,7 @@ static const zend_function_entry spl_funcs_RecursiveTreeIterator[] = {
SPL_ME(RecursiveTreeIterator, getPrefix, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
SPL_ME(RecursiveTreeIterator, setPrefixPart, arginfo_recursive_tree_it_setPrefixPart, ZEND_ACC_PUBLIC)
SPL_ME(RecursiveTreeIterator, getEntry, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
SPL_ME(RecursiveTreeIterator, setPostfix, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
SPL_ME(RecursiveTreeIterator, getPostfix, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
Expand Down
88 changes: 88 additions & 0 deletions ext/spl/tests/recursive_tree_iterator_setpostfix.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--TEST--
SPL: RecursiveTreeIterator::setPostfix()
--CREDITS--
Joshua Thijssen (jthijssen@noxlogic.nl)
--FILE--
<?php

$arr = array(
0 => array(
"a",
1,
),
"a" => array(
2,
"b",
3 => array(
4,
"c",
),
"3" => array(
4,
"c",
),
),
);

$it = new RecursiveArrayIterator($arr);
$it = new RecursiveTreeIterator($it);

echo "----\n";
echo $it->getPostfix();
echo "\n\n";

echo "----\n";
$it->setPostfix("POSTFIX");
echo $it->getPostfix();
echo "\n\n";

echo "----\n";
foreach($it as $k => $v) {
echo "[$k] => $v\n";
}

echo "----\n";
$it->setPostfix("");
echo $it->getPostfix();
echo "\n\n";

echo "----\n";
foreach($it as $k => $v) {
echo "[$k] => $v\n";
}



?>
===DONE===
--EXPECTF--
----


----
POSTFIX

----
[0] => |-ArrayPOSTFIX
[0] => | |-aPOSTFIX
[1] => | \-1POSTFIX
[a] => \-ArrayPOSTFIX
[0] => |-2POSTFIX
[1] => |-bPOSTFIX
[3] => \-ArrayPOSTFIX
[0] => |-4POSTFIX
[1] => \-cPOSTFIX
----


----
[0] => |-Array
[0] => | |-a
[1] => | \-1
[a] => \-Array
[0] => |-2
[1] => |-b
[3] => \-Array
[0] => |-4
[1] => \-c
===DONE===