|
| 1 | +/* |
| 2 | + * Copyright 2014-2017 MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifdef HAVE_CONFIG_H |
| 18 | +# include "config.h" |
| 19 | +#endif |
| 20 | + |
| 21 | +#include <php.h> |
| 22 | +#include "phongo_compat.h" |
| 23 | +#include "php_phongo.h" |
| 24 | +#include "php_bson.h" |
| 25 | + |
| 26 | +typedef enum { |
| 27 | + PHONGO_JSON_MODE_LEGACY, |
| 28 | + PHONGO_JSON_MODE_CANONICAL, |
| 29 | + PHONGO_JSON_MODE_RELAXED, |
| 30 | +} php_phongo_json_mode_t; |
| 31 | + |
| 32 | +/* {{{ proto string MongoDB\BSON\fromPHP(array|object $value) |
| 33 | + Returns the BSON representation of a PHP value */ |
| 34 | +PHP_FUNCTION(MongoDB_BSON_fromPHP) |
| 35 | +{ |
| 36 | + zval *data; |
| 37 | + bson_t *bson; |
| 38 | + |
| 39 | + SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(this_ptr) SUPPRESS_UNUSED_WARNING(return_value_used) /* We don't use these */ |
| 40 | + |
| 41 | + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "A", &data) == FAILURE) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + bson = bson_new(); |
| 46 | + php_phongo_zval_to_bson(data, PHONGO_BSON_NONE, bson, NULL TSRMLS_CC); |
| 47 | + |
| 48 | + PHONGO_RETVAL_STRINGL((const char *) bson_get_data(bson), bson->len); |
| 49 | + bson_destroy(bson); |
| 50 | +} /* }}} */ |
| 51 | + |
| 52 | +/* {{{ proto array|object MongoDB\BSON\toPHP(string $bson [, array $typemap = array()]) |
| 53 | + Returns the PHP representation of a BSON value, optionally converting it into a custom class */ |
| 54 | +PHP_FUNCTION(MongoDB_BSON_toPHP) |
| 55 | +{ |
| 56 | + char *data; |
| 57 | + phongo_zpp_char_len data_len; |
| 58 | + zval *typemap = NULL; |
| 59 | + php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER; |
| 60 | + |
| 61 | + SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(this_ptr) SUPPRESS_UNUSED_WARNING(return_value_used) /* We don't use these */ |
| 62 | + |
| 63 | + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a!", &data, &data_len, &typemap) == FAILURE) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (!php_phongo_bson_typemap_to_state(typemap, &state.map TSRMLS_CC)) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (!php_phongo_bson_to_zval_ex((const unsigned char *)data, data_len, &state)) { |
| 72 | + zval_ptr_dtor(&state.zchild); |
| 73 | + RETURN_NULL(); |
| 74 | + } |
| 75 | + |
| 76 | +#if PHP_VERSION_ID >= 70000 |
| 77 | + RETURN_ZVAL(&state.zchild, 0, 1); |
| 78 | +#else |
| 79 | + RETURN_ZVAL(state.zchild, 0, 1); |
| 80 | +#endif |
| 81 | +} /* }}} */ |
| 82 | + |
| 83 | +/* {{{ proto string MongoDB\BSON\fromJSON(string $json) |
| 84 | + Returns the BSON representation of a JSON value */ |
| 85 | +PHP_FUNCTION(MongoDB_BSON_fromJSON) |
| 86 | +{ |
| 87 | + char *json; |
| 88 | + phongo_zpp_char_len json_len; |
| 89 | + bson_t bson = BSON_INITIALIZER; |
| 90 | + bson_error_t error; |
| 91 | + |
| 92 | + SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(this_ptr) SUPPRESS_UNUSED_WARNING(return_value_used) /* We don't use these */ |
| 93 | + |
| 94 | + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &json, &json_len) == FAILURE) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if (bson_init_from_json(&bson, (const char *)json, json_len, &error)) { |
| 99 | + PHONGO_RETVAL_STRINGL((const char *) bson_get_data(&bson), bson.len); |
| 100 | + bson_destroy(&bson); |
| 101 | + } else { |
| 102 | + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "%s", error.domain == BSON_ERROR_JSON ? error.message : "Error parsing JSON"); |
| 103 | + } |
| 104 | +} /* }}} */ |
| 105 | + |
| 106 | +static void phongo_bson_to_json(INTERNAL_FUNCTION_PARAMETERS, php_phongo_json_mode_t mode) |
| 107 | +{ |
| 108 | + char *data; |
| 109 | + phongo_zpp_char_len data_len; |
| 110 | + const bson_t *bson; |
| 111 | + bool eof = false; |
| 112 | + bson_reader_t *reader; |
| 113 | + char *json = NULL; |
| 114 | + size_t json_len; |
| 115 | + |
| 116 | + SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(this_ptr) SUPPRESS_UNUSED_WARNING(return_value_used) /* We don't use these */ |
| 117 | + |
| 118 | + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == FAILURE) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + reader = bson_reader_new_from_data((const unsigned char *)data, data_len); |
| 123 | + bson = bson_reader_read(reader, NULL); |
| 124 | + |
| 125 | + if (!bson) { |
| 126 | + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not read document from BSON reader"); |
| 127 | + bson_reader_destroy(reader); |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + if (mode == PHONGO_JSON_MODE_LEGACY) { |
| 132 | + json = bson_as_json(bson, &json_len); |
| 133 | + } else if (mode == PHONGO_JSON_MODE_CANONICAL) { |
| 134 | + json = bson_as_canonical_json(bson, &json_len); |
| 135 | + } else if (mode == PHONGO_JSON_MODE_RELAXED) { |
| 136 | + json = bson_as_relaxed_json(bson, &json_len); |
| 137 | + } |
| 138 | + |
| 139 | + if (!json) { |
| 140 | + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not convert BSON document to a JSON string"); |
| 141 | + bson_reader_destroy(reader); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + PHONGO_RETVAL_STRINGL(json, json_len); |
| 146 | + bson_free(json); |
| 147 | + |
| 148 | + if (bson_reader_read(reader, &eof) || !eof) { |
| 149 | + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Reading document did not exhaust input buffer"); |
| 150 | + } |
| 151 | + |
| 152 | + bson_reader_destroy(reader); |
| 153 | +} /* }}} */ |
| 154 | + |
| 155 | +/* {{{ proto string MongoDB\BSON\toJSON(string $bson) |
| 156 | + Returns the legacy extended JSON representation of a BSON value */ |
| 157 | +PHP_FUNCTION(MongoDB_BSON_toJSON) |
| 158 | +{ |
| 159 | + phongo_bson_to_json(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHONGO_JSON_MODE_LEGACY); |
| 160 | +} /* }}} */ |
| 161 | + |
| 162 | +/* {{{ proto string MongoDB\BSON\toCanonicalJSON(string $bson) |
| 163 | + Returns the canonical JSON representation of a BSON value */ |
| 164 | +PHP_FUNCTION(MongoDB_BSON_toCanonicalJSON) |
| 165 | +{ |
| 166 | + phongo_bson_to_json(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHONGO_JSON_MODE_CANONICAL); |
| 167 | +} /* }}} */ |
| 168 | + |
| 169 | +/* {{{ proto string MongoDB\BSON\toRelaxedJSON(string $bson) |
| 170 | + Returns the relaxed JSON representation of a BSON value */ |
| 171 | +PHP_FUNCTION(MongoDB_BSON_toRelaxedJSON) |
| 172 | +{ |
| 173 | + phongo_bson_to_json(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHONGO_JSON_MODE_RELAXED); |
| 174 | +} /* }}} */ |
| 175 | + |
| 176 | +/* |
| 177 | + * Local variables: |
| 178 | + * tab-width: 4 |
| 179 | + * c-basic-offset: 4 |
| 180 | + * End: |
| 181 | + * vim600: noet sw=4 ts=4 fdm=marker |
| 182 | + * vim<600: noet sw=4 ts=4 |
| 183 | + */ |
0 commit comments