Skip to content

Commit 6bfddea

Browse files
committed
Merge pull request #454
2 parents 8df2da8 + 96f24c9 commit 6bfddea

31 files changed

+788
-0
lines changed

src/BSON/Binary.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
/* PHP Core stuff */
3232
#include <php.h>
3333
#include <php_ini.h>
34+
#include <ext/json/php_json.h>
35+
#include <ext/standard/base64.h>
3436
#include <ext/standard/info.h>
3537
#include <Zend/zend_interfaces.h>
3638
#include <ext/spl/spl_iterators.h>
@@ -187,6 +189,42 @@ PHP_METHOD(Binary, getType)
187189
}
188190
/* }}} */
189191

192+
/* {{{ proto array Binary::jsonSerialize()
193+
*/
194+
PHP_METHOD(Binary, jsonSerialize)
195+
{
196+
php_phongo_binary_t *intern;
197+
char type[3];
198+
int type_len;
199+
200+
if (zend_parse_parameters_none() == FAILURE) {
201+
return;
202+
}
203+
204+
intern = Z_BINARY_OBJ_P(getThis());
205+
206+
array_init_size(return_value, 2);
207+
208+
#if PHP_VERSION_ID >= 70000
209+
{
210+
zend_string *data = php_base64_encode((unsigned char *)intern->data, intern->data_len);
211+
ADD_ASSOC_STRINGL(return_value, "$binary", ZSTR_VAL(data), ZSTR_LEN(data));
212+
zend_string_free(data);
213+
}
214+
#else
215+
{
216+
int data_len = 0;
217+
unsigned char *data = php_base64_encode((unsigned char *)intern->data, intern->data_len, &data_len);
218+
ADD_ASSOC_STRINGL(return_value, "$binary", (char *)data, data_len);
219+
efree(data);
220+
}
221+
#endif
222+
223+
type_len = snprintf(type, sizeof(type), "%02x", intern->type);
224+
ADD_ASSOC_STRINGL(return_value, "$type", type, type_len);
225+
}
226+
/* }}} */
227+
190228
/* {{{ proto string Binary::serialize()
191229
*/
192230
PHP_METHOD(Binary, serialize)
@@ -299,6 +337,7 @@ static zend_function_entry php_phongo_binary_me[] = {
299337
PHP_ME(Binary, __construct, ai_Binary___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
300338
PHP_ME(Binary, __set_state, ai_Binary___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
301339
PHP_ME(Binary, __toString, ai_Binary_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
340+
PHP_ME(Binary, jsonSerialize, ai_Binary_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
302341
PHP_ME(Binary, serialize, ai_Binary_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
303342
PHP_ME(Binary, unserialize, ai_Binary_unserialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
304343
PHP_ME(Binary, getData, ai_Binary_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
@@ -399,6 +438,7 @@ PHP_MINIT_FUNCTION(Binary)
399438
php_phongo_binary_ce->create_object = php_phongo_binary_create_object;
400439
PHONGO_CE_FINAL(php_phongo_binary_ce);
401440

441+
zend_class_implements(php_phongo_binary_ce TSRMLS_CC, 1, php_json_serializable_ce);
402442
zend_class_implements(php_phongo_binary_ce TSRMLS_CC, 1, php_phongo_type_ce);
403443
zend_class_implements(php_phongo_binary_ce TSRMLS_CC, 1, zend_ce_serializable);
404444

src/BSON/Decimal128.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/* PHP Core stuff */
3232
#include <php.h>
3333
#include <php_ini.h>
34+
#include <ext/json/php_json.h>
3435
#include <ext/standard/info.h>
3536
#include <Zend/zend_interfaces.h>
3637
#include <ext/spl/spl_iterators.h>
@@ -151,6 +152,25 @@ PHP_METHOD(Decimal128, __toString)
151152
}
152153
/* }}} */
153154

155+
/* {{{ proto array Decimal128::jsonSerialize()
156+
*/
157+
PHP_METHOD(Decimal128, jsonSerialize)
158+
{
159+
php_phongo_decimal128_t *intern;
160+
char outbuf[BSON_DECIMAL128_STRING] = "";
161+
162+
if (zend_parse_parameters_none() == FAILURE) {
163+
return;
164+
}
165+
166+
intern = Z_DECIMAL128_OBJ_P(getThis());
167+
168+
array_init_size(return_value, 1);
169+
bson_decimal128_to_string(&intern->decimal, outbuf);
170+
ADD_ASSOC_STRING(return_value, "$numberDecimal", outbuf);
171+
}
172+
/* }}} */
173+
154174
/* {{{ proto string Decimal128::serialize()
155175
*/
156176
PHP_METHOD(Decimal128, serialize)
@@ -262,6 +282,7 @@ static zend_function_entry php_phongo_decimal128_me[] = {
262282
PHP_ME(Decimal128, __construct, ai_Decimal128___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
263283
PHP_ME(Decimal128, __set_state, ai_Decimal128___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
264284
PHP_ME(Decimal128, __toString, ai_Decimal128_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
285+
PHP_ME(Decimal128, jsonSerialize, ai_Decimal128_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
265286
PHP_ME(Decimal128, serialize, ai_Decimal128_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
266287
PHP_ME(Decimal128, unserialize, ai_Decimal128_unserialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
267288
PHP_FE_END
@@ -353,6 +374,7 @@ PHP_MINIT_FUNCTION(Decimal128)
353374
php_phongo_decimal128_ce->create_object = php_phongo_decimal128_create_object;
354375
PHONGO_CE_FINAL(php_phongo_decimal128_ce);
355376

377+
zend_class_implements(php_phongo_decimal128_ce TSRMLS_CC, 1, php_json_serializable_ce);
356378
zend_class_implements(php_phongo_decimal128_ce TSRMLS_CC, 1, php_phongo_type_ce);
357379
zend_class_implements(php_phongo_decimal128_ce TSRMLS_CC, 1, zend_ce_serializable);
358380

src/BSON/Javascript.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/* PHP Core stuff */
3232
#include <php.h>
3333
#include <php_ini.h>
34+
#include <ext/json/php_json.h>
3435
#include <ext/standard/info.h>
3536
#include <Zend/zend_interfaces.h>
3637
#include <ext/spl/spl_iterators.h>
@@ -215,6 +216,39 @@ PHP_METHOD(Javascript, getScope)
215216
}
216217
/* }}} */
217218

219+
/* {{{ proto array Javascript::jsonSerialize()
220+
*/
221+
PHP_METHOD(Javascript, jsonSerialize)
222+
{
223+
php_phongo_javascript_t *intern;
224+
225+
if (zend_parse_parameters_none() == FAILURE) {
226+
return;
227+
}
228+
229+
intern = Z_JAVASCRIPT_OBJ_P(getThis());
230+
231+
array_init_size(return_value, 2);
232+
ADD_ASSOC_STRINGL(return_value, "$code", intern->code, intern->code_len);
233+
234+
if (intern->scope && intern->scope->len) {
235+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
236+
237+
if (phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
238+
#if PHP_VERSION_ID >= 70000
239+
Z_ADDREF(state.zchild);
240+
ADD_ASSOC_ZVAL_EX(return_value, "$scope", &state.zchild);
241+
#else
242+
Z_ADDREF_P(state.zchild);
243+
ADD_ASSOC_ZVAL_EX(return_value, "$scope", state.zchild);
244+
#endif
245+
}
246+
247+
zval_ptr_dtor(&state.zchild);
248+
}
249+
}
250+
/* }}} */
251+
218252
/* {{{ proto string Javascript::serialize()
219253
*/
220254
PHP_METHOD(Javascript, serialize)
@@ -351,6 +385,7 @@ static zend_function_entry php_phongo_javascript_me[] = {
351385
PHP_ME(Javascript, __construct, ai_Javascript___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
352386
PHP_ME(Javascript, __set_state, ai_Javascript___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
353387
PHP_ME(Javascript, __toString, ai_Javascript_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
388+
PHP_ME(Javascript, jsonSerialize, ai_Javascript_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
354389
PHP_ME(Javascript, serialize, ai_Javascript_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
355390
PHP_ME(Javascript, unserialize, ai_Javascript_unserialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
356391
PHP_ME(Javascript, getCode, ai_Javascript_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
@@ -493,6 +528,7 @@ PHP_MINIT_FUNCTION(Javascript)
493528
php_phongo_javascript_ce->create_object = php_phongo_javascript_create_object;
494529
PHONGO_CE_FINAL(php_phongo_javascript_ce);
495530

531+
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, php_json_serializable_ce);
496532
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, php_phongo_type_ce);
497533
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, zend_ce_serializable);
498534

src/BSON/MaxKey.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/* PHP Core stuff */
3232
#include <php.h>
3333
#include <php_ini.h>
34+
#include <ext/json/php_json.h>
3435
#include <ext/standard/info.h>
3536
#include <Zend/zend_interfaces.h>
3637
#include <ext/spl/spl_iterators.h>
@@ -60,6 +61,19 @@ PHP_METHOD(MaxKey, __set_state)
6061
}
6162
/* }}} */
6263

64+
/* {{{ proto array MaxKey::jsonSerialize()
65+
*/
66+
PHP_METHOD(MaxKey, jsonSerialize)
67+
{
68+
if (zend_parse_parameters_none() == FAILURE) {
69+
return;
70+
}
71+
72+
array_init_size(return_value, 1);
73+
ADD_ASSOC_LONG_EX(return_value, "$maxKey", 1);
74+
}
75+
/* }}} */
76+
6377
/* {{{ proto string MaxKey::serialize()
6478
*/
6579
PHP_METHOD(MaxKey, serialize)
@@ -101,6 +115,7 @@ ZEND_END_ARG_INFO()
101115

102116
static zend_function_entry php_phongo_maxkey_me[] = {
103117
PHP_ME(MaxKey, __set_state, ai_MaxKey___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
118+
PHP_ME(MaxKey, jsonSerialize, ai_MaxKey_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
104119
PHP_ME(MaxKey, serialize, ai_MaxKey_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
105120
PHP_ME(MaxKey, unserialize, ai_MaxKey_unserialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
106121
PHP_FE_END
@@ -157,6 +172,7 @@ PHP_MINIT_FUNCTION(MaxKey)
157172
php_phongo_maxkey_ce->create_object = php_phongo_maxkey_create_object;
158173
PHONGO_CE_FINAL(php_phongo_maxkey_ce);
159174

175+
zend_class_implements(php_phongo_maxkey_ce TSRMLS_CC, 1, php_json_serializable_ce);
160176
zend_class_implements(php_phongo_maxkey_ce TSRMLS_CC, 1, php_phongo_type_ce);
161177
zend_class_implements(php_phongo_maxkey_ce TSRMLS_CC, 1, zend_ce_serializable);
162178

src/BSON/MinKey.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/* PHP Core stuff */
3232
#include <php.h>
3333
#include <php_ini.h>
34+
#include <ext/json/php_json.h>
3435
#include <ext/standard/info.h>
3536
#include <Zend/zend_interfaces.h>
3637
#include <ext/spl/spl_iterators.h>
@@ -60,6 +61,19 @@ PHP_METHOD(MinKey, __set_state)
6061
}
6162
/* }}} */
6263

64+
/* {{{ proto array MinKey::jsonSerialize()
65+
*/
66+
PHP_METHOD(MinKey, jsonSerialize)
67+
{
68+
if (zend_parse_parameters_none() == FAILURE) {
69+
return;
70+
}
71+
72+
array_init_size(return_value, 1);
73+
ADD_ASSOC_LONG_EX(return_value, "$minKey", 1);
74+
}
75+
/* }}} */
76+
6377
/* {{{ proto string MinKey::serialize()
6478
*/
6579
PHP_METHOD(MinKey, serialize)
@@ -101,6 +115,7 @@ ZEND_END_ARG_INFO()
101115

102116
static zend_function_entry php_phongo_minkey_me[] = {
103117
PHP_ME(MinKey, __set_state, ai_MinKey___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
118+
PHP_ME(MinKey, jsonSerialize, ai_MinKey_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
104119
PHP_ME(MinKey, serialize, ai_MinKey_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
105120
PHP_ME(MinKey, unserialize, ai_MinKey_unserialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
106121
PHP_FE_END
@@ -158,6 +173,7 @@ PHP_MINIT_FUNCTION(MinKey)
158173
php_phongo_minkey_ce->create_object = php_phongo_minkey_create_object;
159174
PHONGO_CE_FINAL(php_phongo_minkey_ce);
160175

176+
zend_class_implements(php_phongo_minkey_ce TSRMLS_CC, 1, php_json_serializable_ce);
161177
zend_class_implements(php_phongo_minkey_ce TSRMLS_CC, 1, php_phongo_type_ce);
162178
zend_class_implements(php_phongo_minkey_ce TSRMLS_CC, 1, zend_ce_serializable);
163179

src/BSON/ObjectID.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/* PHP Core stuff */
3232
#include <php.h>
3333
#include <php_ini.h>
34+
#include <ext/json/php_json.h>
3435
#include <ext/standard/info.h>
3536
#include <Zend/zend_interfaces.h>
3637
#include <ext/spl/spl_iterators.h>
@@ -197,6 +198,23 @@ PHP_METHOD(ObjectID, __toString)
197198
}
198199
/* }}} */
199200

201+
/* {{{ proto array ObjectID::jsonSerialize()
202+
*/
203+
PHP_METHOD(ObjectID, jsonSerialize)
204+
{
205+
php_phongo_objectid_t *intern;
206+
207+
if (zend_parse_parameters_none() == FAILURE) {
208+
return;
209+
}
210+
211+
intern = Z_OBJECTID_OBJ_P(getThis());
212+
213+
array_init_size(return_value, 1);
214+
ADD_ASSOC_STRINGL(return_value, "$oid", intern->oid, 24);
215+
}
216+
/* }}} */
217+
200218
/* {{{ proto string ObjectID::serialize()
201219
*/
202220
PHP_METHOD(ObjectID, serialize)
@@ -306,6 +324,7 @@ static zend_function_entry php_phongo_objectid_me[] = {
306324
PHP_ME(ObjectID, getTimestamp, ai_ObjectID_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
307325
PHP_ME(ObjectID, __set_state, ai_ObjectID___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
308326
PHP_ME(ObjectID, __toString, ai_ObjectID_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
327+
PHP_ME(ObjectID, jsonSerialize, ai_ObjectID_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
309328
PHP_ME(ObjectID, serialize, ai_ObjectID_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
310329
PHP_ME(ObjectID, unserialize, ai_ObjectID_unserialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
311330
PHP_FE_END
@@ -405,6 +424,7 @@ PHP_MINIT_FUNCTION(ObjectID)
405424
php_phongo_objectid_ce->create_object = php_phongo_objectid_create_object;
406425
PHONGO_CE_FINAL(php_phongo_objectid_ce);
407426

427+
zend_class_implements(php_phongo_objectid_ce TSRMLS_CC, 1, php_json_serializable_ce);
408428
zend_class_implements(php_phongo_objectid_ce TSRMLS_CC, 1, php_phongo_type_ce);
409429
zend_class_implements(php_phongo_objectid_ce TSRMLS_CC, 1, zend_ce_serializable);
410430

src/BSON/Regex.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/* PHP Core stuff */
3232
#include <php.h>
3333
#include <php_ini.h>
34+
#include <ext/json/php_json.h>
3435
#include <ext/standard/info.h>
3536
#include <Zend/zend_interfaces.h>
3637
#include <ext/spl/spl_iterators.h>
@@ -206,6 +207,24 @@ PHP_METHOD(Regex, __toString)
206207
}
207208
/* }}} */
208209

210+
/* {{{ proto array Regex::jsonSerialize()
211+
*/
212+
PHP_METHOD(Regex, jsonSerialize)
213+
{
214+
php_phongo_regex_t *intern;
215+
216+
if (zend_parse_parameters_none() == FAILURE) {
217+
return;
218+
}
219+
220+
intern = Z_REGEX_OBJ_P(getThis());
221+
222+
array_init_size(return_value, 2);
223+
ADD_ASSOC_STRINGL(return_value, "$regex", intern->pattern, intern->pattern_len);
224+
ADD_ASSOC_STRINGL(return_value, "$options", intern->flags, intern->flags_len);
225+
}
226+
/* }}} */
227+
209228
/* {{{ proto string Regex::serialize()
210229
*/
211230
PHP_METHOD(Regex, serialize)
@@ -317,6 +336,7 @@ static zend_function_entry php_phongo_regex_me[] = {
317336
PHP_ME(Regex, __construct, ai_Regex___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
318337
PHP_ME(Regex, __set_state, ai_Regex___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
319338
PHP_ME(Regex, __toString, ai_Regex_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
339+
PHP_ME(Regex, jsonSerialize, ai_Regex_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
320340
PHP_ME(Regex, serialize, ai_Regex_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
321341
PHP_ME(Regex, unserialize, ai_Regex_unserialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
322342
PHP_ME(Regex, getPattern, ai_Regex_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
@@ -445,6 +465,7 @@ PHP_MINIT_FUNCTION(Regex)
445465

446466
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, php_phongo_type_ce);
447467
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, zend_ce_serializable);
468+
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, php_json_serializable_ce);
448469

449470
memcpy(&php_phongo_handler_regex, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
450471
php_phongo_handler_regex.get_properties = php_phongo_regex_get_properties;

0 commit comments

Comments
 (0)