-
Notifications
You must be signed in to change notification settings - Fork 208
PHPC-459: BSON objects should implement JsonSerializable #454
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
/* PHP Core stuff */ | ||
#include <php.h> | ||
#include <php_ini.h> | ||
#include <ext/json/php_json.h> | ||
#include <ext/standard/info.h> | ||
#include <Zend/zend_interfaces.h> | ||
#include <ext/spl/spl_iterators.h> | ||
|
@@ -206,6 +207,24 @@ PHP_METHOD(Regex, __toString) | |
} | ||
/* }}} */ | ||
|
||
/* {{{ proto array Regex::jsonSerialize() | ||
*/ | ||
PHP_METHOD(Regex, jsonSerialize) | ||
{ | ||
php_phongo_regex_t *intern; | ||
|
||
if (zend_parse_parameters_none() == FAILURE) { | ||
return; | ||
} | ||
|
||
intern = Z_REGEX_OBJ_P(getThis()); | ||
|
||
array_init_size(return_value, 2); | ||
ADD_ASSOC_STRINGL(return_value, "$regex", intern->pattern, intern->pattern_len); | ||
ADD_ASSOC_STRINGL(return_value, "$options", intern->flags, intern->flags_len); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left a comment in DRIVERS-331 asking whether we should alphabetically order the regex flag characters (as should be done for raw BSON to support binary comparisons). The implementation here leaves the flags as-is. If we do start alphabetically ordering the characters in the future, we'll likely do so when the Regex object is initialized and this code shouldn't need to change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The relevant PHPC ticket for flag ordering is PHPC-829. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And HHVM is at HHVM-264. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that we should order the flags in the constructor/initialiser only. That should be all what we need to do about this I believe. The HHVM ticket is HHVM-264. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I concur. Nothing to do here and we have our respective tickets. |
||
} | ||
/* }}} */ | ||
|
||
/* {{{ proto string Regex::serialize() | ||
*/ | ||
PHP_METHOD(Regex, serialize) | ||
|
@@ -317,6 +336,7 @@ static zend_function_entry php_phongo_regex_me[] = { | |
PHP_ME(Regex, __construct, ai_Regex___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) | ||
PHP_ME(Regex, __set_state, ai_Regex___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) | ||
PHP_ME(Regex, __toString, ai_Regex_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) | ||
PHP_ME(Regex, jsonSerialize, ai_Regex_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) | ||
PHP_ME(Regex, serialize, ai_Regex_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) | ||
PHP_ME(Regex, unserialize, ai_Regex_unserialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) | ||
PHP_ME(Regex, getPattern, ai_Regex_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) | ||
|
@@ -445,6 +465,7 @@ PHP_MINIT_FUNCTION(Regex) | |
|
||
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, php_phongo_type_ce); | ||
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, zend_ce_serializable); | ||
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, php_json_serializable_ce); | ||
|
||
memcpy(&php_phongo_handler_regex, phongo_get_std_object_handlers(), sizeof(zend_object_handlers)); | ||
php_phongo_handler_regex.get_properties = php_phongo_regex_get_properties; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the point of initialising this with a empty string in the code segment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was previously done in
php_phongo_decimal128_get_properties()
as insurance that the string was null-terminated even ifbson_decimal128_to_string()
decided not to touch it. Based on my reading ofbson_decimal128_to_string()
, does always assign a null-terminated string to its output buffer, but it wasn't documented as doing so and I wanted to be defensive. The alternative was amemset()
on the array, but I didn't think that was necessary.__toString()
andserialize()
currently usechar outbuf[BSON_DECIMAL128_STRING]
without any assignment, but I wanted to update those later.