|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | Copyright © The PHP Group and Contributors. | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | This source file is subject to the Modified BSD License that is | |
| 6 | + | bundled with this package in the file LICENSE, and is available | |
| 7 | + | through the World Wide Web at <https://www.php.net/license/>. | |
| 8 | + | | |
| 9 | + | SPDX-License-Identifier: BSD-3-Clause | |
| 10 | + +----------------------------------------------------------------------+ |
| 11 | + | Authors: David Carlier <devnexen@gmail.com> | |
| 12 | + +----------------------------------------------------------------------+ |
| 13 | +*/ |
| 14 | + |
| 15 | +extern "C" { |
| 16 | +#include "php.h" |
| 17 | +#include "zend_API.h" |
| 18 | +#include "../intl_common.h" |
| 19 | +} |
| 20 | + |
| 21 | +#if U_ICU_VERSION_MAJOR_NUM >= 63 |
| 22 | +#include <unicode/numberformatter.h> |
| 23 | +#include <unicode/unistr.h> |
| 24 | +#include <unicode/locid.h> |
| 25 | +#include "../intl_convertcpp.h" |
| 26 | + |
| 27 | +extern "C" { |
| 28 | +#include "../php_intl.h" |
| 29 | +#include "numberformatter_arginfo.h" |
| 30 | +} |
| 31 | +#include "numberformatter_class.h" |
| 32 | + |
| 33 | +using icu::number::NumberFormatter; |
| 34 | +using icu::number::UnlocalizedNumberFormatter; |
| 35 | +using icu::UnicodeString; |
| 36 | + |
| 37 | +static zend_object_handlers numberformatter_handlers; |
| 38 | +zend_class_entry *IntlNumberFormatter_ce_ptr; |
| 39 | + |
| 40 | +static void numberformatter_free_obj(zend_object *object) |
| 41 | +{ |
| 42 | + IntlNumberFormatter_object *nfo = php_intl_numberformatter_fetch_object(object); |
| 43 | + |
| 44 | + delete NUMBERFORMATTER_OBJECT(nfo); |
| 45 | + NUMBERFORMATTER_OBJECT(nfo) = nullptr; |
| 46 | + intl_error_reset(INTL_DATA_ERROR_P(nfo)); |
| 47 | + |
| 48 | + zend_object_std_dtor(&nfo->zo); |
| 49 | +} |
| 50 | + |
| 51 | +static zend_object *numberformatter_create_object(zend_class_entry *ce) |
| 52 | +{ |
| 53 | + IntlNumberFormatter_object *nfo = reinterpret_cast<IntlNumberFormatter_object *>(zend_object_alloc(sizeof(IntlNumberFormatter_object), ce)); |
| 54 | + |
| 55 | + intl_error_init(INTL_DATA_ERROR_P(nfo)); |
| 56 | + NUMBERFORMATTER_OBJECT(nfo) = nullptr; |
| 57 | + |
| 58 | + zend_object_std_init(&nfo->zo, ce); |
| 59 | + object_properties_init(&nfo->zo, ce); |
| 60 | + nfo->zo.handlers = &numberformatter_handlers; |
| 61 | + |
| 62 | + return &nfo->zo; |
| 63 | +} |
| 64 | + |
| 65 | +U_CFUNC PHP_METHOD(IntlNumberFormatter, __construct) |
| 66 | +{ |
| 67 | + ZEND_PARSE_PARAMETERS_NONE(); |
| 68 | + |
| 69 | + zend_throw_error(nullptr, "Cannot directly construct %s, use createFromSkeleton method instead", ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name)); |
| 70 | +} |
| 71 | + |
| 72 | +U_CFUNC PHP_METHOD(IntlNumberFormatter, createFromSkeleton) |
| 73 | +{ |
| 74 | + char *skeleton, *locale; |
| 75 | + size_t skeleton_len, locale_len; |
| 76 | + |
| 77 | + ZEND_PARSE_PARAMETERS_START(2, 2) |
| 78 | + Z_PARAM_STRING(skeleton, skeleton_len) |
| 79 | + Z_PARAM_STRING(locale, locale_len) |
| 80 | + ZEND_PARSE_PARAMETERS_END(); |
| 81 | + |
| 82 | + intl_error_reset(nullptr); |
| 83 | + |
| 84 | + INTL_CHECK_LOCALE_LEN(locale_len); |
| 85 | + |
| 86 | + if (locale_len == 0) { |
| 87 | + locale = (char *)intl_locale_get_default(); |
| 88 | + } |
| 89 | + |
| 90 | + if (icu::Locale(locale).getISO3Language()[0] == '\0') { |
| 91 | + zend_argument_value_error(2, "\"%s\" is invalid", locale); |
| 92 | + RETURN_THROWS(); |
| 93 | + } |
| 94 | + |
| 95 | + UErrorCode status = U_ZERO_ERROR; |
| 96 | + UnicodeString uskeleton; |
| 97 | + |
| 98 | + intl_stringFromChar(uskeleton, skeleton, skeleton_len, &status); |
| 99 | + INTL_CHECK_STATUS_OR_NULL(status, "Error converting skeleton to UTF-16"); |
| 100 | + |
| 101 | + UnlocalizedNumberFormatter nf = NumberFormatter::forSkeleton(uskeleton, status); |
| 102 | + INTL_CHECK_STATUS_OR_NULL(status, "Number formatter creation failed"); |
| 103 | + |
| 104 | + object_init_ex(return_value, IntlNumberFormatter_ce_ptr); |
| 105 | + NUMBERFORMATTER_OBJECT(Z_INTL_INTLNUMBERFORMATTER_P(return_value)) = new LocalizedNumberFormatter(nf.locale(icu::Locale::createCanonical(locale))); |
| 106 | +} |
| 107 | + |
| 108 | +U_CFUNC PHP_METHOD(IntlNumberFormatter, format) |
| 109 | +{ |
| 110 | + zval *value; |
| 111 | + |
| 112 | + ZEND_PARSE_PARAMETERS_START(1, 1) |
| 113 | + Z_PARAM_NUMBER_OR_STR(value) |
| 114 | + ZEND_PARSE_PARAMETERS_END(); |
| 115 | + |
| 116 | + IntlNumberFormatter_object *nfo = Z_INTL_INTLNUMBERFORMATTER_P(ZEND_THIS); |
| 117 | + intl_errors_reset(INTL_DATA_ERROR_P(nfo)); |
| 118 | + |
| 119 | + UErrorCode status = U_ZERO_ERROR; |
| 120 | + UnicodeString result; |
| 121 | + |
| 122 | + if (Z_TYPE_P(value) == IS_STRING) { |
| 123 | + if (UNEXPECTED(zend_str_has_nul_byte(Z_STR_P(value)))) { |
| 124 | + zend_argument_value_error(1, "must not contain any null bytes"); |
| 125 | + RETURN_THROWS(); |
| 126 | + } |
| 127 | + if (UNEXPECTED(Z_STRLEN_P(value) > INT32_MAX)) { |
| 128 | + zend_argument_value_error(1, "is too long"); |
| 129 | + RETURN_THROWS(); |
| 130 | + } |
| 131 | + result = NUMBERFORMATTER_OBJECT(nfo)->formatDecimal(icu::StringPiece(Z_STRVAL_P(value), static_cast<int32_t>(Z_STRLEN_P(value))), status).toString(status); |
| 132 | + } else if (Z_TYPE_P(value) == IS_DOUBLE) { |
| 133 | + result = NUMBERFORMATTER_OBJECT(nfo)->formatDouble(Z_DVAL_P(value), status).toString(status); |
| 134 | + } else { |
| 135 | + result = NUMBERFORMATTER_OBJECT(nfo)->formatInt(static_cast<int64_t>(Z_LVAL_P(value)), status).toString(status); |
| 136 | + } |
| 137 | + |
| 138 | + if (U_FAILURE(status)) { |
| 139 | + intl_errors_set(INTL_DATA_ERROR_P(nfo), status, "Number formatting failed"); |
| 140 | + RETURN_FALSE; |
| 141 | + } |
| 142 | + |
| 143 | + zend_string *ret = intl_charFromString(result, &status); |
| 144 | + |
| 145 | + if (!ret) { |
| 146 | + intl_errors_set(INTL_DATA_ERROR_P(nfo), status, "Error converting value to UTF-8"); |
| 147 | + RETURN_FALSE; |
| 148 | + } |
| 149 | + |
| 150 | + RETVAL_STR(ret); |
| 151 | +} |
| 152 | + |
| 153 | +U_CFUNC PHP_METHOD(IntlNumberFormatter, getErrorCode) |
| 154 | +{ |
| 155 | + ZEND_PARSE_PARAMETERS_NONE(); |
| 156 | + |
| 157 | + RETURN_LONG(INTL_DATA_ERROR_CODE(Z_INTL_INTLNUMBERFORMATTER_P(ZEND_THIS))); |
| 158 | +} |
| 159 | + |
| 160 | +U_CFUNC PHP_METHOD(IntlNumberFormatter, getErrorMessage) |
| 161 | +{ |
| 162 | + ZEND_PARSE_PARAMETERS_NONE(); |
| 163 | + |
| 164 | + RETURN_STR(intl_error_get_message(INTL_DATA_ERROR_P(Z_INTL_INTLNUMBERFORMATTER_P(ZEND_THIS)))); |
| 165 | +} |
| 166 | + |
| 167 | +void numberformatter_register_class(void) |
| 168 | +{ |
| 169 | + IntlNumberFormatter_ce_ptr = register_class_IntlNumberFormatter(); |
| 170 | + IntlNumberFormatter_ce_ptr->create_object = numberformatter_create_object; |
| 171 | + |
| 172 | + memcpy(&numberformatter_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); |
| 173 | + numberformatter_handlers.offset = offsetof(IntlNumberFormatter_object, zo); |
| 174 | + numberformatter_handlers.free_obj = numberformatter_free_obj; |
| 175 | + numberformatter_handlers.clone_obj = nullptr; |
| 176 | +} |
| 177 | +#endif |
0 commit comments