From 6b0bbb294ffbf03938795ca13e3724941b31caba Mon Sep 17 00:00:00 2001 From: CismonX Date: Fri, 20 Apr 2018 20:59:21 +0800 Subject: [PATCH] Add `toPairs()` and `toCollection()`. --- src/collections_methods.c | 34 +++++++++++++++++++++++++++++++--- tests/020-to-collection.phpt | 10 ++++++++++ tests/021-to-pairs.phpt | 12 ++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 tests/020-to-collection.phpt create mode 100644 tests/021-to-pairs.phpt diff --git a/src/collections_methods.c b/src/collections_methods.c index 8e6baf5..5705750 100644 --- a/src/collections_methods.c +++ b/src/collections_methods.c @@ -17,7 +17,8 @@ #define NEW_COLLECTION_OBJ(name) \ NEW_OBJ(name, collections_collection_ce, collection_handlers) #define NEW_PAIR_OBJ(name) \ - NEW_OBJ(name, collections_pair_ce, &std_object_handlers) + NEW_OBJ(name, collections_pair_ce, &std_object_handlers); \ + object_properties_init(name, collections_pair_ce) #define IS_COLLECTION_P(zval) \ Z_TYPE_P(zval) == IS_OBJECT && Z_OBJCE_P(zval) == collections_collection_ce @@ -1142,12 +1143,39 @@ PHP_METHOD(Collection, toArray) PHP_METHOD(Collection, toCollection) { - + zval* dest; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJECT_OF_CLASS(dest, collections_collection_ce) + ZEND_PARSE_PARAMETERS_END(); + zend_array* current = COLLECTION_FETCH_EX(); + zend_array* dest_arr = COLLECTION_FETCH(dest); + SEPARATE_COLLECTION(dest_arr, dest); + ZEND_HASH_FOREACH_BUCKET(current, Bucket* bucket) + if (bucket->key) + zend_hash_add(dest_arr, bucket->key, &bucket->val); + else + zend_hash_next_index_insert(dest_arr, &bucket->val); + ZEND_HASH_FOREACH_END(); + RETVAL_ZVAL(dest, 1, 0); } PHP_METHOD(Collection, toPairs) { - + zend_array* current = COLLECTION_FETCH_EX(); + ARRAY_NEW_EX(new_collection, current); + ZEND_HASH_FOREACH_BUCKET(current, Bucket* bucket) + NEW_PAIR_OBJ(obj); + zval pair, key; + ZVAL_OBJ(&pair, obj); + if (bucket->key) + ZVAL_STR(&key, bucket->key); + else + ZVAL_LONG(&key, bucket->h); + PAIR_UPDATE_FIRST(&pair, &key); + PAIR_UPDATE_SECOND(&pair, &bucket->val); + zend_hash_next_index_insert(new_collection, &pair); + ZEND_HASH_FOREACH_END(); + RETVAL_NEW_COLLECTION(new_collection); } PHP_METHOD(Collection, union) diff --git a/tests/020-to-collection.phpt b/tests/020-to-collection.phpt new file mode 100644 index 0000000..4ab06d2 --- /dev/null +++ b/tests/020-to-collection.phpt @@ -0,0 +1,10 @@ +--TEST-- +Test Collection::toCollection(). +--FILE-- +toCollection($collection); +if ($collection->toArray() != ['a', 'b', 'foo', 'bar']) + echo 'Collection::toCollection() failed.', PHP_EOL; +?> +--EXPECT-- diff --git a/tests/021-to-pairs.phpt b/tests/021-to-pairs.phpt new file mode 100644 index 0000000..4d1f8dd --- /dev/null +++ b/tests/021-to-pairs.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test Collection::toPairs(). +--FILE-- + 'b', 'c' => 'd'])->toPairs(); +$test = ''; +foreach ($pairs as $pair) + $test .= $pair->first . ',' . $pair->second . ';'; +if ($test != 'a,b;c,d;') + echo 'Collection::toPairs() failed.', PHP_EOL; +?> +--EXPECT--