Skip to content
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

Add isList, isMap & isRef methods for Php::Value #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions include/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@ class Value : private HashParent
bool isObject() const { return type() == Type::Object; }
bool isArray() const { return type() == Type::Array; }
bool isCallable() const;
bool isList() const;
bool isMap() const { return type() == Type::Array && !isList(); }
bool isRef() const;

/**
* Get access to the raw buffer - you can use this for direct reading and
Expand Down
32 changes: 32 additions & 0 deletions zend/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,38 @@ bool Value::isCallable() const
return zend_is_callable(_val, 0, NULL TSRMLS_CC);
}

/**
* Check if the variable holds something that is list
* @return bool
*/
bool Value::isList() const
{
// must be an array
if (!isArray()) return false;

// get the number of elements
ulong count = zend_hash_num_elements(Z_ARRVAL_P(_val));

// zero length array
if (count == 0) return true;

// count == 1 and a[0] exists
if (count == 1 && contains(0)) return true;

// a[0] exists, a[count - 1] exists and the next index is count
return contains(0) && contains(count - 1) &&
zend_hash_next_free_element(Z_ARRVAL_P(_val)) == count;
}

/**
* Check if the variable holds something that is ref
* @return bool
*/
bool Value::isRef() const
{
return Z_ISREF_P(_val);
}

/**
* Make a clone of the type
* @return Value
Expand Down