|
| 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 <Zend/zend_interfaces.h> |
| 23 | +#include <ext/standard/php_var.h> |
| 24 | +#if PHP_VERSION_ID >= 70000 |
| 25 | +# include <zend_smart_str.h> |
| 26 | +#else |
| 27 | +# include <ext/standard/php_smart_str.h> |
| 28 | +#endif |
| 29 | + |
| 30 | +#include "phongo_compat.h" |
| 31 | +#include "php_phongo.h" |
| 32 | + |
| 33 | +zend_class_entry *php_phongo_undefined_ce; |
| 34 | + |
| 35 | +/* {{{ proto string MongoDB\BSON\Undefined::__toString() |
| 36 | + Return the empty string. */ |
| 37 | +static PHP_METHOD(Undefined, __toString) |
| 38 | +{ |
| 39 | + PHONGO_RETURN_STRINGL("", 0); |
| 40 | +} /* }}} */ |
| 41 | + |
| 42 | +/* {{{ proto array MongoDB\BSON\Undefined::jsonSerialize() |
| 43 | +*/ |
| 44 | +static PHP_METHOD(Undefined, jsonSerialize) |
| 45 | +{ |
| 46 | + if (zend_parse_parameters_none() == FAILURE) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + array_init_size(return_value, 1); |
| 51 | + ADD_ASSOC_BOOL_EX(return_value, "$undefined", 1); |
| 52 | +} /* }}} */ |
| 53 | + |
| 54 | +/* {{{ proto string MongoDB\BSON\Undefined::serialize() |
| 55 | +*/ |
| 56 | +static PHP_METHOD(Undefined, serialize) |
| 57 | +{ |
| 58 | + PHONGO_RETURN_STRING(""); |
| 59 | +} /* }}} */ |
| 60 | + |
| 61 | +/* {{{ proto void MongoDB\BSON\Undefined::unserialize(string $serialized) |
| 62 | +*/ |
| 63 | +static PHP_METHOD(Undefined, unserialize) |
| 64 | +{ |
| 65 | + zend_error_handling error_handling; |
| 66 | + char *serialized; |
| 67 | + phongo_zpp_char_len serialized_len; |
| 68 | + |
| 69 | + zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC); |
| 70 | + |
| 71 | + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &serialized_len) == FAILURE) { |
| 72 | + zend_restore_error_handling(&error_handling TSRMLS_CC); |
| 73 | + return; |
| 74 | + } |
| 75 | + zend_restore_error_handling(&error_handling TSRMLS_CC); |
| 76 | +} /* }}} */ |
| 77 | + |
| 78 | +/* {{{ MongoDB\BSON\Undefined function entries */ |
| 79 | +ZEND_BEGIN_ARG_INFO_EX(ai_Undefined_unserialize, 0, 0, 1) |
| 80 | + ZEND_ARG_INFO(0, serialized) |
| 81 | +ZEND_END_ARG_INFO() |
| 82 | + |
| 83 | +ZEND_BEGIN_ARG_INFO_EX(ai_Undefined_void, 0, 0, 0) |
| 84 | +ZEND_END_ARG_INFO() |
| 85 | + |
| 86 | +static zend_function_entry php_phongo_undefined_me[] = { |
| 87 | + /* __set_state intentionally missing */ |
| 88 | + PHP_ME(Undefined, __toString, ai_Undefined_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) |
| 89 | + PHP_ME(Undefined, jsonSerialize, ai_Undefined_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) |
| 90 | + PHP_ME(Undefined, serialize, ai_Undefined_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) |
| 91 | + PHP_ME(Undefined, unserialize, ai_Undefined_unserialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) |
| 92 | + ZEND_NAMED_ME(__construct, PHP_FN(MongoDB_disabled___construct), ai_Undefined_void, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL) |
| 93 | + PHP_FE_END |
| 94 | +}; |
| 95 | +/* }}} */ |
| 96 | + |
| 97 | +/* {{{ MongoDB\BSON\Undefined object handlers */ |
| 98 | +static zend_object_handlers php_phongo_handler_undefined; |
| 99 | + |
| 100 | +static void php_phongo_undefined_free_object(phongo_free_object_arg *object TSRMLS_DC) /* {{{ */ |
| 101 | +{ |
| 102 | + php_phongo_undefined_t *intern = Z_OBJ_UNDEFINED(object); |
| 103 | + |
| 104 | + zend_object_std_dtor(&intern->std TSRMLS_CC); |
| 105 | + |
| 106 | +#if PHP_VERSION_ID < 70000 |
| 107 | + efree(intern); |
| 108 | +#endif |
| 109 | +} /* }}} */ |
| 110 | + |
| 111 | +static phongo_create_object_retval php_phongo_undefined_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */ |
| 112 | +{ |
| 113 | + php_phongo_undefined_t *intern = NULL; |
| 114 | + |
| 115 | + intern = PHONGO_ALLOC_OBJECT_T(php_phongo_undefined_t, class_type); |
| 116 | + zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 117 | + object_properties_init(&intern->std, class_type); |
| 118 | + |
| 119 | +#if PHP_VERSION_ID >= 70000 |
| 120 | + intern->std.handlers = &php_phongo_handler_undefined; |
| 121 | + |
| 122 | + return &intern->std; |
| 123 | +#else |
| 124 | + { |
| 125 | + zend_object_value retval; |
| 126 | + retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_undefined_free_object, NULL TSRMLS_CC); |
| 127 | + retval.handlers = &php_phongo_handler_undefined; |
| 128 | + |
| 129 | + return retval; |
| 130 | + } |
| 131 | +#endif |
| 132 | +} /* }}} */ |
| 133 | +/* }}} */ |
| 134 | + |
| 135 | +void php_phongo_undefined_init_ce(INIT_FUNC_ARGS) /* {{{ */ |
| 136 | +{ |
| 137 | + zend_class_entry ce; |
| 138 | + |
| 139 | + INIT_NS_CLASS_ENTRY(ce, "MongoDB\\BSON", "Undefined", php_phongo_undefined_me); |
| 140 | + php_phongo_undefined_ce = zend_register_internal_class(&ce TSRMLS_CC); |
| 141 | + php_phongo_undefined_ce->create_object = php_phongo_undefined_create_object; |
| 142 | + PHONGO_CE_FINAL(php_phongo_undefined_ce); |
| 143 | + |
| 144 | + zend_class_implements(php_phongo_undefined_ce TSRMLS_CC, 1, php_phongo_json_serializable_ce); |
| 145 | + zend_class_implements(php_phongo_undefined_ce TSRMLS_CC, 1, php_phongo_type_ce); |
| 146 | + zend_class_implements(php_phongo_undefined_ce TSRMLS_CC, 1, zend_ce_serializable); |
| 147 | + |
| 148 | + memcpy(&php_phongo_handler_undefined, phongo_get_std_object_handlers(), sizeof(zend_object_handlers)); |
| 149 | +#if PHP_VERSION_ID >= 70000 |
| 150 | + php_phongo_handler_undefined.free_obj = php_phongo_undefined_free_object; |
| 151 | + php_phongo_handler_undefined.offset = XtOffsetOf(php_phongo_undefined_t, std); |
| 152 | +#endif |
| 153 | +} /* }}} */ |
| 154 | + |
| 155 | +/* |
| 156 | + * Local variables: |
| 157 | + * tab-width: 4 |
| 158 | + * c-basic-offset: 4 |
| 159 | + * End: |
| 160 | + * vim600: noet sw=4 ts=4 fdm=marker |
| 161 | + * vim<600: noet sw=4 ts=4 |
| 162 | + */ |
0 commit comments