@@ -171,33 +171,59 @@ PHP_FUNCTION( numfmt_format_currency )
171
171
size_t currency_len = 0 ;
172
172
UChar * scurrency = NULL ;
173
173
int32_t scurrency_len = 0 ;
174
+ UFormattedNumber * result = NULL ;
174
175
FORMATTER_METHOD_INIT_VARS ;
175
176
176
177
/* Parse parameters. */
177
- if ( zend_parse_method_parameters ( ZEND_NUM_ARGS (), getThis (), "Ods " ,
178
+ if ( zend_parse_method_parameters ( ZEND_NUM_ARGS (), getThis (), "Od|s! " ,
178
179
& object , NumberFormatter_ce_ptr , & number , & currency , & currency_len ) == FAILURE )
179
180
{
180
181
RETURN_THROWS ();
181
182
}
182
183
183
184
/* Fetch the object. */
184
- FORMATTER_METHOD_FETCH_OBJECT ;
185
-
186
- /* Convert currency to UTF-16. */
187
- intl_convert_utf8_to_utf16 (& scurrency , & scurrency_len , currency , currency_len , & INTL_DATA_ERROR_CODE (nfo ));
188
- INTL_METHOD_CHECK_STATUS ( nfo , "Currency conversion to UTF-16 failed" );
189
-
190
- /* Format the number using a fixed-length buffer. */
191
- formatted_len = unum_formatDoubleCurrency (FORMATTER_OBJECT (nfo ), number , scurrency , formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
192
-
193
- /* If the buffer turned out to be too small
194
- * then allocate another buffer dynamically
195
- * and use it to format the number.
196
- */
197
- if (INTL_DATA_ERROR_CODE (nfo ) == U_BUFFER_OVERFLOW_ERROR ) {
198
- intl_error_reset (INTL_DATA_ERROR_P (nfo ));
199
- formatted = eumalloc (formatted_len );
200
- unum_formatDoubleCurrency (FORMATTER_OBJECT (nfo ), number , scurrency , formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
185
+ FORMATTER_METHOD_FETCH_OBJECT2 ;
186
+
187
+ if (FORMATTER_OBJECT (nfo )) {
188
+ if (!currency ) {
189
+ zend_argument_value_error (3 , "currency cannot be null when instantiating NumberFormatter"
190
+ "with a style constant" );
191
+ RETURN_THROWS ();
192
+ }
193
+ /* Convert currency to UTF-16. */
194
+ intl_convert_utf8_to_utf16 (& scurrency , & scurrency_len , currency , currency_len , & INTL_DATA_ERROR_CODE (nfo ));
195
+ INTL_METHOD_CHECK_STATUS ( nfo , "Currency conversion to UTF-16 failed" );
196
+
197
+ /* Format the number using a fixed-length buffer. */
198
+ formatted_len = unum_formatDoubleCurrency (FORMATTER_OBJECT (nfo ), number , scurrency , formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
199
+
200
+ /* If the buffer turned out to be too small
201
+ * then allocate another buffer dynamically
202
+ * and use it to format the number.
203
+ */
204
+ if (INTL_DATA_ERROR_CODE (nfo ) == U_BUFFER_OVERFLOW_ERROR ) {
205
+ intl_error_reset (INTL_DATA_ERROR_P (nfo ));
206
+ formatted = eumalloc (formatted_len );
207
+ unum_formatDoubleCurrency (FORMATTER_OBJECT (nfo ), number , scurrency , formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
208
+ }
209
+ } else {
210
+ /* The new NumberFormatter takes the currency format (aka skeleton)
211
+ * from NumberFormatter::__construct 2nd argument.
212
+ */
213
+ result = unumf_openResult (& INTL_DATA_ERROR_CODE (nfo ));
214
+ INTL_METHOD_CHECK_STATUS (nfo , "Error opening formatter result" );
215
+ unumf_formatDouble (FORMATTER_OBJECT2 (nfo ), number , result , & INTL_DATA_ERROR_CODE (nfo ));
216
+ INTL_METHOD_CHECK_STATUS ( nfo , "Number formatting failed" );
217
+ formatted_len = unumf_resultToString (result , formatted , formatted_len , & INTL_DATA_ERROR_CODE (nfo ));
218
+ /* If the buffer turned out to be too small
219
+ * then allocate another buffer dynamically
220
+ * and use it to format the number.
221
+ */
222
+ if (INTL_DATA_ERROR_CODE (nfo ) == U_BUFFER_OVERFLOW_ERROR ) {
223
+ intl_error_reset (INTL_DATA_ERROR_P (nfo ));
224
+ formatted = eumalloc (formatted_len );
225
+ formatted_len = unumf_resultToString (result , formatted , formatted_len , & INTL_DATA_ERROR_CODE (nfo ));
226
+ }
201
227
}
202
228
203
229
if ( U_FAILURE ( INTL_DATA_ERROR_CODE ((nfo )) ) ) {
@@ -214,6 +240,8 @@ PHP_FUNCTION( numfmt_format_currency )
214
240
if (scurrency ) {
215
241
efree (scurrency );
216
242
}
243
+
244
+ unumf_closeResult (result );
217
245
}
218
246
219
247
/* }}} */
0 commit comments