summaryrefslogtreecommitdiff
path: root/src/value.cpp
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-07 15:21:51 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-07 15:21:51 +0100
commit8c6652ae30202ba50f989a3b4d6c7255e4351251 (patch)
treea1cedea97187cd66082c688ae40e5202945017a3 /src/value.cpp
parent834c9d477a3f73ca42d13cb777d451b63f1cbf3a (diff)
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
Diffstat (limited to 'src/value.cpp')
-rw-r--r--src/value.cpp107
1 files changed, 84 insertions, 23 deletions
diff --git a/src/value.cpp b/src/value.cpp
index 53d8c0a..75a67d4 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -1405,6 +1405,40 @@ int Value::size() const
}
/**
+ * Convert the object to a map with string index and Php::Value value
+ * @return std::map
+ */
+std::map<std::string,Php::Value> Value::mapValue() const
+{
+ // check type
+ if (isArray())
+ {
+ // result variable
+ std::map<std::string,Php::Value> result;
+
+ // @todo loop through the zval key/value pairs, and return a map
+
+ // done
+ return result;
+ }
+ else if (isObject())
+ {
+ // result variable
+ std::map<std::string,Php::Value> result;
+
+ // @todo convert the properties to a map
+
+ // done
+ return result;
+ }
+ else
+ {
+ // return an empty map
+ return std::map<std::string,Php::Value>();
+ }
+}
+
+/**
* Does the array contain a certain index?
* @param index
* @return bool
@@ -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 **)&current) != 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 **)&current) != FAILURE)
+ if (isArray() && zend_hash_index_find(Z_ARRVAL_P(_val), index, (void **)&current) != 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);
@@ -1603,6 +1638,32 @@ const Value &Value::set(const char *key, int size, const Value &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 **)&current) != 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
* @param index