|
| 1 | +/* |
| 2 | + +---------------------------------------------------------------------------+ |
| 3 | + | PHP Driver for MongoDB | |
| 4 | + +---------------------------------------------------------------------------+ |
| 5 | + | Copyright 2013-2016 MongoDB, Inc. | |
| 6 | + | | |
| 7 | + | Licensed under the Apache License, Version 2.0 (the "License"); | |
| 8 | + | you may not use this file except in compliance with the License. | |
| 9 | + | You may obtain a copy of the License at | |
| 10 | + | | |
| 11 | + | http://www.apache.org/licenses/LICENSE-2.0 | |
| 12 | + | | |
| 13 | + | Unless required by applicable law or agreed to in writing, software | |
| 14 | + | distributed under the License is distributed on an "AS IS" BASIS, | |
| 15 | + | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 16 | + | See the License for the specific language governing permissions and | |
| 17 | + | limitations under the License. | |
| 18 | + +---------------------------------------------------------------------------+ |
| 19 | + | Copyright (c) 2014-2016 MongoDB, Inc. | |
| 20 | + +---------------------------------------------------------------------------+ |
| 21 | +*/ |
| 22 | + |
| 23 | +#ifdef HAVE_CONFIG_H |
| 24 | +# include "config.h" |
| 25 | +#endif |
| 26 | + |
| 27 | +/* External libs */ |
| 28 | +#include <bson.h> |
| 29 | +#include <mongoc.h> |
| 30 | + |
| 31 | +/* PHP Core stuff */ |
| 32 | +#include <php.h> |
| 33 | +#include <php_ini.h> |
| 34 | +#include <ext/standard/info.h> |
| 35 | +#include <Zend/zend_interfaces.h> |
| 36 | +#include <ext/spl/spl_iterators.h> |
| 37 | +/* Our Compatability header */ |
| 38 | +#include "phongo_compat.h" |
| 39 | + |
| 40 | +/* Our stuffz */ |
| 41 | +#include "php_phongo.h" |
| 42 | +#include "php_bson.h" |
| 43 | + |
| 44 | + |
| 45 | +PHONGO_API zend_class_entry *php_phongo_decimal128_ce; |
| 46 | + |
| 47 | +zend_object_handlers php_phongo_handler_decimal128; |
| 48 | + |
| 49 | +/* {{{ proto BSON\Decimal128 Decimal128::__construct(string $value) |
| 50 | + Construct a new BSON Decimal128 type */ |
| 51 | +PHP_METHOD(Decimal128, __construct) |
| 52 | +{ |
| 53 | + php_phongo_decimal128_t *intern; |
| 54 | + zend_error_handling error_handling; |
| 55 | + char *value; |
| 56 | + phongo_zpp_char_len value_len; |
| 57 | + |
| 58 | + |
| 59 | + zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC); |
| 60 | + intern = Z_DECIMAL128_OBJ_P(getThis()); |
| 61 | + |
| 62 | + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &value, &value_len) == FAILURE) { |
| 63 | + zend_restore_error_handling(&error_handling TSRMLS_CC); |
| 64 | + return; |
| 65 | + } |
| 66 | + zend_restore_error_handling(&error_handling TSRMLS_CC); |
| 67 | + |
| 68 | + if ( ! bson_decimal128_from_string(value, &intern->decimal)) { |
| 69 | + phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error parsing decimal string: %s", value); |
| 70 | + return; |
| 71 | + } |
| 72 | +} |
| 73 | +/* }}} */ |
| 74 | + |
| 75 | +/* {{{ proto void Decimal128::__toString() |
| 76 | + */ |
| 77 | +PHP_METHOD(Decimal128, __toString) |
| 78 | +{ |
| 79 | + php_phongo_decimal128_t *intern; |
| 80 | + char outbuf[BSON_DECIMAL128_STRING]; |
| 81 | + |
| 82 | + intern = Z_DECIMAL128_OBJ_P(getThis()); |
| 83 | + |
| 84 | + if (zend_parse_parameters_none() == FAILURE) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + bson_decimal128_to_string(&intern->decimal, outbuf); |
| 89 | + |
| 90 | + PHONGO_RETURN_STRING(outbuf); |
| 91 | +} |
| 92 | +/* }}} */ |
| 93 | + |
| 94 | + |
| 95 | +/* {{{ BSON\Decimal128 */ |
| 96 | + |
| 97 | +ZEND_BEGIN_ARG_INFO_EX(ai_Decimal128___construct, 0, 0, 2) |
| 98 | + ZEND_ARG_INFO(0, value) |
| 99 | +ZEND_END_ARG_INFO(); |
| 100 | + |
| 101 | +ZEND_BEGIN_ARG_INFO_EX(ai_Decimal128___toString, 0, 0, 0) |
| 102 | +ZEND_END_ARG_INFO(); |
| 103 | + |
| 104 | + |
| 105 | +static zend_function_entry php_phongo_decimal128_me[] = { |
| 106 | + PHP_ME(Decimal128, __construct, ai_Decimal128___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) |
| 107 | + PHP_ME(Decimal128, __toString, ai_Decimal128___toString, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) |
| 108 | + PHP_ME(Manager, __wakeUp, NULL, ZEND_ACC_PUBLIC) |
| 109 | + PHP_FE_END |
| 110 | +}; |
| 111 | + |
| 112 | +/* }}} */ |
| 113 | + |
| 114 | + |
| 115 | +/* {{{ php_phongo_decimal128_t object handlers */ |
| 116 | +static void php_phongo_decimal128_free_object(phongo_free_object_arg *object TSRMLS_DC) /* {{{ */ |
| 117 | +{ |
| 118 | + php_phongo_decimal128_t *intern = Z_OBJ_DECIMAL128(object); |
| 119 | + |
| 120 | + zend_object_std_dtor(&intern->std TSRMLS_CC); |
| 121 | + |
| 122 | +#if PHP_VERSION_ID < 70000 |
| 123 | + efree(intern); |
| 124 | +#endif |
| 125 | +} /* }}} */ |
| 126 | + |
| 127 | +phongo_create_object_retval php_phongo_decimal128_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */ |
| 128 | +{ |
| 129 | + php_phongo_decimal128_t *intern = NULL; |
| 130 | + |
| 131 | + intern = PHONGO_ALLOC_OBJECT_T(php_phongo_decimal128_t, class_type); |
| 132 | + |
| 133 | + zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 134 | + object_properties_init(&intern->std, class_type); |
| 135 | + |
| 136 | +#if PHP_VERSION_ID >= 70000 |
| 137 | + intern->std.handlers = &php_phongo_handler_decimal128; |
| 138 | + |
| 139 | + return &intern->std; |
| 140 | +#else |
| 141 | + { |
| 142 | + zend_object_value retval; |
| 143 | + retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_decimal128_free_object, NULL TSRMLS_CC); |
| 144 | + retval.handlers = &php_phongo_handler_decimal128; |
| 145 | + |
| 146 | + return retval; |
| 147 | + } |
| 148 | +#endif |
| 149 | +} /* }}} */ |
| 150 | + |
| 151 | +HashTable *php_phongo_decimal128_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */ |
| 152 | +{ |
| 153 | + php_phongo_decimal128_t *intern; |
| 154 | + char outbuf[BSON_DECIMAL128_STRING] = ""; |
| 155 | +#if PHP_VERSION_ID >= 70000 |
| 156 | + zval retval; |
| 157 | +#else |
| 158 | + zval retval = zval_used_for_init; |
| 159 | +#endif |
| 160 | + |
| 161 | + intern = Z_DECIMAL128_OBJ_P(object); |
| 162 | + *is_temp = 1; |
| 163 | + array_init_size(&retval, 1); |
| 164 | + |
| 165 | + bson_decimal128_to_string(&intern->decimal, outbuf); |
| 166 | + ADD_ASSOC_STRING(&retval, "dec", outbuf); |
| 167 | + |
| 168 | + return Z_ARRVAL(retval); |
| 169 | +} /* }}} */ |
| 170 | +/* }}} */ |
| 171 | + |
| 172 | +/* {{{ PHP_MINIT_FUNCTION */ |
| 173 | +PHP_MINIT_FUNCTION(Decimal128) |
| 174 | +{ |
| 175 | + zend_class_entry ce; |
| 176 | + (void)type;(void)module_number; |
| 177 | + |
| 178 | + INIT_NS_CLASS_ENTRY(ce, BSON_NAMESPACE, "Decimal128", php_phongo_decimal128_me); |
| 179 | + php_phongo_decimal128_ce = zend_register_internal_class(&ce TSRMLS_CC); |
| 180 | + php_phongo_decimal128_ce->create_object = php_phongo_decimal128_create_object; |
| 181 | + PHONGO_CE_INIT(php_phongo_decimal128_ce); |
| 182 | + |
| 183 | + zend_class_implements(php_phongo_decimal128_ce TSRMLS_CC, 1, php_phongo_type_ce); |
| 184 | + |
| 185 | + memcpy(&php_phongo_handler_decimal128, phongo_get_std_object_handlers(), sizeof(zend_object_handlers)); |
| 186 | + php_phongo_handler_decimal128.get_debug_info = php_phongo_decimal128_get_debug_info; |
| 187 | +#if PHP_VERSION_ID >= 70000 |
| 188 | + php_phongo_handler_decimal128.free_obj = php_phongo_decimal128_free_object; |
| 189 | + php_phongo_handler_decimal128.offset = XtOffsetOf(php_phongo_decimal128_t, std); |
| 190 | +#endif |
| 191 | + |
| 192 | + return SUCCESS; |
| 193 | +} |
| 194 | +/* }}} */ |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | +/* |
| 199 | + * Local variables: |
| 200 | + * tab-width: 4 |
| 201 | + * c-basic-offset: 4 |
| 202 | + * End: |
| 203 | + * vim600: noet sw=4 ts=4 fdm=marker |
| 204 | + * vim<600: noet sw=4 ts=4 |
| 205 | + */ |
0 commit comments