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 properties method for Php::Value #116

Open
wants to merge 3 commits 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
7 changes: 7 additions & 0 deletions include/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,13 @@ class Value : private HashParent
return result;
}

/**
* Get object property names.
* @param only_public
* @return std::vector
*/
std::vector<std::string> properties(bool only_public = true) const;

/**
* Define the iterator type
*/
Expand Down
3 changes: 2 additions & 1 deletion zend/includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#include <list>
#include <exception>
#include <type_traits>

#include <algorithm>

// for debug
#include <iostream>

Expand Down
46 changes: 46 additions & 0 deletions zend/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,52 @@ std::map<std::string,Php::Value> Value::mapValue() const
return result;
}

/**
* Get object property names.
* @param only_public
* @return std::vector
*/
std::vector<std::string> Value::properties(bool only_public) const {
if (isObject()) {
std::set<std::string> result;

// we need the TSRMLS_CC variable
TSRMLS_FETCH();

HashTable *table = Z_OBJPROP_P(_val);
Bucket *position = nullptr;

// move to first position
zend_hash_internal_pointer_reset_ex(table, &position);

do {
char *string_key;
unsigned int str_len;
unsigned long num_key;

// get the current key
int type = zend_hash_get_current_key_ex(table, &string_key, &str_len, &num_key, 0, &position);

// if key is not found, the iterator is at an invalid position
if (type == HASH_KEY_NON_EXISTANT) continue;

std::string key = std::string(string_key, str_len - 1);
if (key[0] == '\0') {
// if only_public is true, only store public property
if (only_public) continue;
key = key.substr(key.find('\0', 1) + 1);
}
result.insert(std::move(key));

// move the iterator forward
} while (zend_hash_move_forward_ex(table, &position) == SUCCESS);

return std::vector<std::string>(result.begin(), result.end());

}
return std::vector<std::string>();
}

/**
* Internal helper method to retrieve an iterator
* @param begin Should the iterator start at the begin
Expand Down