From 8c6652ae30202ba50f989a3b4d6c7255e4351251 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Fri, 7 Mar 2014 15:21:51 +0100 Subject: update documentation, implemented comparison operator for hashmember class, added Value constructor that receives a Php::Type to initialize as a specific type, added Value constructors to initialize a Value directly from a map or a vector, fixed value comparison operators, added casting constructors to cast a value to a vector or a map --- src/value.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 84 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/value.cpp b/src/value.cpp index 53d8c0a..75a67d4 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -1404,6 +1404,40 @@ int Value::size() const } } +/** + * Convert the object to a map with string index and Php::Value value + * @return std::map + */ +std::map Value::mapValue() const +{ + // check type + if (isArray()) + { + // result variable + std::map result; + + // @todo loop through the zval key/value pairs, and return a map + + // done + return result; + } + else if (isObject()) + { + // result variable + std::map result; + + // @todo convert the properties to a map + + // done + return result; + } + else + { + // return an empty map + return std::map(); + } +} + /** * Does the array contain a certain index? * @param index @@ -1519,26 +1553,14 @@ Value Value::get(const char *key, int size) const } /** - * Set a certain property + * Set a certain property without performing any checks + * This method can be used when it is already known that the object is an array * @param index * @param value * @return Value */ -const Value &Value::set(int index, const Value &value) +const Value &Value::setRaw(int index, const Value &value) { - // the current value - zval **current; - - // check if this index is already in the array, otherwise we return NULL - if (isArray() && zend_hash_index_find(Z_ARRVAL_P(_val), index, (void **)¤t) != FAILURE) - { - // skip if nothing is going to change - if (value._val == *current) return value; - } - - // must be an array - setType(Type::Array); - // if this is not a reference variable, we should detach it to implement copy on write SEPARATE_ZVAL_IF_NOT_REF(&_val); @@ -1552,25 +1574,41 @@ const Value &Value::set(int index, const Value &value) return value; } + /** * Set a certain property - * @param key - * @param size + * @param index * @param value * @return Value */ -const Value &Value::set(const char *key, int size, const Value &value) +const Value &Value::set(int index, const Value &value) { // the current value zval **current; // check if this index is already in the array, otherwise we return NULL - if (isArray() && zend_hash_find(Z_ARRVAL_P(_val), key, size + 1, (void **)¤t) != FAILURE) + if (isArray() && zend_hash_index_find(Z_ARRVAL_P(_val), index, (void **)¤t) != FAILURE) { // skip if nothing is going to change if (value._val == *current) return value; } - + + // must be an array + setType(Type::Array); + + // set property + return setRaw(index, value); +} + +/** + * Set a certain property without running any checks + * @param key + * @param size + * @param value + * @return Value + */ +const Value &Value::setRaw(const char *key, int size, const Value &value) +{ // is this an object? if (isObject()) { @@ -1585,9 +1623,6 @@ const Value &Value::set(const char *key, int size, const Value &value) } else { - // must be an array - setType(Type::Array); - // if this is not a reference variable, we should detach it to implement copy on write SEPARATE_ZVAL_IF_NOT_REF(&_val); @@ -1602,6 +1637,32 @@ const Value &Value::set(const char *key, int size, const Value &value) return value; } +/** + * Set a certain property + * @param key + * @param size + * @param value + * @return Value + */ +const Value &Value::set(const char *key, int size, const Value &value) +{ + // the current value + zval **current; + + // check if this index is already in the array, otherwise we return NULL + if (isArray() && zend_hash_find(Z_ARRVAL_P(_val), key, size + 1, (void **)¤t) != FAILURE) + { + // skip if nothing is going to change + if (value._val == *current) return value; + } + + // this should be an object or an array + if (!isObject()) setType(Type::Array); + + // done + return setRaw(key, size, value); +} + /** * Array access operator * This can be used for accessing arrays -- cgit v1.2.3