diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-09 22:37:41 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-09 22:37:41 +0100 |
commit | 49d88d98a0656233f15923d31ea67a1ed229e514 (patch) | |
tree | 04bb753e4d5434c46bd90048e52d22260052334d /src/value.cpp | |
parent | bdca5b0454534413f10d13211885c086fafff35a (diff) |
work in progress on iterators
Diffstat (limited to 'src/value.cpp')
-rw-r--r-- | src/value.cpp | 96 |
1 files changed, 51 insertions, 45 deletions
diff --git a/src/value.cpp b/src/value.cpp index 752d77e..f8ee3e9 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -1467,67 +1467,73 @@ int Value::size() const */ std::map<std::string,Php::Value> Value::mapValue() const { - // loop through the zval key/value pairs, and return a map // result variable std::map<std::string,Php::Value> result; // check type - if (isArray() || isObject()) + if (isArray()) { - zval **value; - char *key; - unsigned long ind; + // get access to the hast table + HashTable *arr = Z_ARRVAL_P(_val); - // get access to the internal hash table of _val - // see Zend/zend_API.h 723: HASH_OF(_val) - HashTable *arr = isArray() ? Z_ARRVAL_P(_val) : Z_OBJ_HT_P(_val)->get_properties((_val) TSRMLS_CC); + // reset iterator to beginning of the hash table + zend_hash_internal_pointer_reset(arr); + // pointer that will be set to hash key and index + char *key; + unsigned long ind; - // similarly php: reset($array): - // The definition of this and the following functions can be found in Zend/zend_hash.h 174 - // Maybe make it optional? - // If the following line to remove, then repeated calling the Value::mapValue() will return an empty map - zend_hash_internal_pointer_reset(arr); + // key type + int hash_key_type; - if (isArray()) + // loop through the recors + while ((hash_key_type = zend_hash_get_current_key(arr, &key, &ind, 0)) != HASH_KEY_NON_EXISTENT) { - unsigned int hash_key_type; - while( (hash_key_type = zend_hash_get_current_key(arr, &key, &ind, 0)) != HASH_KEY_NON_EXISTENT ) - { - zend_hash_get_current_data(arr, (void **) &value); + // required variable + zval **value; + + // retrieve data + zend_hash_get_current_data(arr, (void **) &value); - if(HASH_KEY_IS_LONG == hash_key_type) - { - result[std::to_string(ind)] = Value(*value); - } - else // hash_key_type == HASH_KEY_IS_STRING - { - result[key] = Value(*value); - } - - // next iteration - zend_hash_move_forward(arr); - } + // check the type of key + if (HASH_KEY_IS_LONG != hash_key_type) result[key] = Value(*value); + else result[std::to_string(ind)] = Value(*value); + + // next iteration + zend_hash_move_forward(arr); } - else + } + else if (isObject()) + { + // get access to the hast table + HashTable *arr = Z_OBJ_HT_P(_val)->get_properties(_val); + + // reset iterator to beginning of the hash table + zend_hash_internal_pointer_reset(arr); + + // pointer that will be set to hash key and index + char *key; + unsigned long ind; + + // loop through the records + while( zend_hash_get_current_key(arr, &key, &ind, 0) != HASH_KEY_NON_EXISTENT ) { - // For obtaining a hashtable of the object meets function void rebuild_object_properties(zend_object *zobj) - // Zend/zend_object_handlers.c 66 - // hashtable of object's properties always has string (no integer) keys - while( zend_hash_get_current_key(arr, &key, &ind, 0) != HASH_KEY_NON_EXISTENT ) + // if property is accessible (i.e. propertie access type is public. See rebuild_object_properties ) + if('\0' != *key) { - // if propertie is accessible (i.e. propertie access type is public. See rebuild_object_properties ) - if('\0' != *key) - { - zend_hash_get_current_data(arr, (void **) &value); - result[key] = Value(*value); - } - - // next iteration - zend_hash_move_forward(arr); + // required variable + zval **value; + + // retrieve property + zend_hash_get_current_data(arr, (void **) &value); + + // append to mape + result[key] = Value(*value); } - } + // next iteration + zend_hash_move_forward(arr); + } } // done |