Skip to content

PHPC-729: Implement __toString() for Javascript and Binary #350

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

Merged
merged 1 commit into from
Jul 15, 2016
Merged
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
17 changes: 17 additions & 0 deletions src/BSON/Binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ PHP_METHOD(Binary, __set_state)
}
/* }}} */

/* {{{ proto string Binary::__toString()
Return the Binary's data string. */
PHP_METHOD(Binary, __toString)
{
php_phongo_binary_t *intern;

if (zend_parse_parameters_none() == FAILURE) {
return;
}

intern = Z_BINARY_OBJ_P(getThis());

PHONGO_RETURN_STRINGL(intern->data, intern->data_len);
}
/* }}} */

/* {{{ proto void Binary::__wakeup()
*/
PHP_METHOD(Binary, __wakeup)
Expand Down Expand Up @@ -201,6 +217,7 @@ ZEND_END_ARG_INFO()
static zend_function_entry php_phongo_binary_me[] = {
PHP_ME(Binary, __construct, ai_Binary___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Binary, __set_state, ai_Binary___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Binary, __toString, ai_Binary_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Binary, __wakeup, ai_Binary_void, ZEND_ACC_PUBLIC)
PHP_ME(Binary, getData, ai_Binary_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Binary, getType, ai_Binary_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
Expand Down
17 changes: 17 additions & 0 deletions src/BSON/Javascript.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ PHP_METHOD(Javascript, __set_state)
}
/* }}} */

/* {{{ proto string Javascript::__toString()
Return the Javascript's code string. */
PHP_METHOD(Javascript, __toString)
{
php_phongo_javascript_t *intern;

if (zend_parse_parameters_none() == FAILURE) {
return;
}

intern = Z_JAVASCRIPT_OBJ_P(getThis());

PHONGO_RETURN_STRINGL(intern->code, intern->code_len);
}
/* }}} */

/* {{{ proto void Javascript::__wakeup()
*/
PHP_METHOD(Javascript, __wakeup)
Expand Down Expand Up @@ -181,6 +197,7 @@ ZEND_END_ARG_INFO()
static zend_function_entry php_phongo_javascript_me[] = {
PHP_ME(Javascript, __construct, ai_Javascript___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Javascript, __set_state, ai_Javascript___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Javascript, __toString, ai_Javascript_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Javascript, __wakeup, ai_Javascript_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
Expand Down
14 changes: 14 additions & 0 deletions tests/bson/bson-binary-tostring-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
MongoDB\BSON\Binary::__toString()
--FILE--
<?php

$binary = new MongoDB\BSON\Binary('foobar', MongoDB\BSON\Binary::TYPE_GENERIC);
var_dump((string) $binary);

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
string(6) "foobar"
===DONE===
18 changes: 18 additions & 0 deletions tests/bson/bson-javascript-tostring-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
MongoDB\BSON\Javascript::__toString()
--FILE--
<?php

$js = new MongoDB\BSON\Javascript('function foo() { return 1; }');
var_dump((string) $js);

$js = new MongoDB\BSON\Javascript('function foo() { return bar; }', ['bar' => 1]);
var_dump((string) $js);

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
string(28) "function foo() { return 1; }"
string(30) "function foo() { return bar; }"
===DONE===