@@ -168,84 +168,126 @@ PHP_FUNCTION( resourcebundle_create )
168
168
/* }}} */
169
169
170
170
/* {{{ resourcebundle_array_fetch */
171
- static void resourcebundle_array_fetch (zend_object * object , zval * offset , zval * return_value , int fallback )
171
+ static zval * resource_bundle_array_fetch (
172
+ zend_object * object , zend_string * offset_str , zend_long offset_int ,
173
+ zval * return_value , bool fallback , uint32_t offset_arg_num )
172
174
{
173
- int32_t meindex = 0 ;
174
- char * mekey = NULL ;
175
- bool is_numeric = 0 ;
176
- char * pbuf ;
175
+ int32_t index = 0 ;
176
+ char * key = NULL ;
177
+ bool is_numeric = offset_str == NULL ;
178
+ char * pbuf ;
177
179
ResourceBundle_object * rb ;
178
180
179
181
rb = php_intl_resourcebundle_fetch_object (object );
180
182
intl_error_reset (NULL );
181
183
intl_error_reset (INTL_DATA_ERROR_P (rb ));
182
184
183
- if (Z_TYPE_P (offset ) == IS_LONG ) {
184
- is_numeric = 1 ;
185
- meindex = (int32_t )Z_LVAL_P (offset );
186
- rb -> child = ures_getByIndex ( rb -> me , meindex , rb -> child , & INTL_DATA_ERROR_CODE (rb ) );
187
- } else if (Z_TYPE_P (offset ) == IS_STRING ) {
188
- mekey = Z_STRVAL_P (offset );
189
- rb -> child = ures_getByKey (rb -> me , mekey , rb -> child , & INTL_DATA_ERROR_CODE (rb ) );
185
+ if (offset_str ) {
186
+ // TODO Check is not empty?
187
+ key = ZSTR_VAL (offset_str );
188
+ rb -> child = ures_getByKey (rb -> me , key , rb -> child , & INTL_DATA_ERROR_CODE (rb ) );
190
189
} else {
191
- intl_errors_set (INTL_DATA_ERROR_P (rb ), U_ILLEGAL_ARGUMENT_ERROR ,
192
- "resourcebundle_get: index should be integer or string" , 0 );
193
- RETURN_NULL ();
190
+ if (UNEXPECTED (offset_int < (zend_long )INT32_MIN || offset_int > (zend_long )INT32_MAX )) {
191
+ if (offset_arg_num ) {
192
+ zend_argument_value_error (offset_arg_num , "index must be between %d and %d" , INT32_MIN , INT32_MAX );
193
+ } else {
194
+ zend_value_error ("Index must be between %d and %d" , INT32_MIN , INT32_MAX );
195
+ }
196
+ return NULL ;
197
+ }
198
+ index = (int32_t )offset_int ;
199
+ rb -> child = ures_getByIndex (rb -> me , index , rb -> child , & INTL_DATA_ERROR_CODE (rb ));
194
200
}
195
201
196
202
intl_error_set_code ( NULL , INTL_DATA_ERROR_CODE (rb ) );
197
203
if (U_FAILURE (INTL_DATA_ERROR_CODE (rb ))) {
198
204
if (is_numeric ) {
199
- spprintf ( & pbuf , 0 , "Cannot load resource element %d" , meindex );
205
+ spprintf ( & pbuf , 0 , "Cannot load resource element %d" , index );
200
206
} else {
201
- spprintf ( & pbuf , 0 , "Cannot load resource element '%s'" , mekey );
207
+ spprintf ( & pbuf , 0 , "Cannot load resource element '%s'" , key );
202
208
}
203
209
intl_errors_set_custom_msg ( INTL_DATA_ERROR_P (rb ), pbuf , 1 );
204
210
efree (pbuf );
205
- RETURN_NULL ();
211
+ RETVAL_NULL ();
212
+ return return_value ;
206
213
}
207
214
208
215
if (!fallback && (INTL_DATA_ERROR_CODE (rb ) == U_USING_FALLBACK_WARNING || INTL_DATA_ERROR_CODE (rb ) == U_USING_DEFAULT_WARNING )) {
209
216
UErrorCode icuerror ;
210
217
const char * locale = ures_getLocaleByType ( rb -> me , ULOC_ACTUAL_LOCALE , & icuerror );
211
218
if (is_numeric ) {
212
- spprintf ( & pbuf , 0 , "Cannot load element %d without fallback from to %s" , meindex , locale );
219
+ spprintf (& pbuf , 0 , "Cannot load element %d without fallback from to %s" , index , locale );
213
220
} else {
214
- spprintf ( & pbuf , 0 , "Cannot load element '%s' without fallback from to %s" , mekey , locale );
221
+ spprintf (& pbuf , 0 , "Cannot load element '%s' without fallback from to %s" , key , locale );
215
222
}
216
- intl_errors_set_custom_msg ( INTL_DATA_ERROR_P (rb ), pbuf , 1 );
223
+ intl_errors_set_custom_msg ( INTL_DATA_ERROR_P (rb ), pbuf , 1 );
217
224
efree (pbuf );
218
- RETURN_NULL ();
225
+ RETVAL_NULL ();
226
+ return return_value ;
219
227
}
220
228
221
229
resourcebundle_extract_value ( return_value , rb );
230
+ return return_value ;
222
231
}
223
232
/* }}} */
224
233
225
234
/* {{{ resourcebundle_array_get */
226
235
zval * resourcebundle_array_get (zend_object * object , zval * offset , int type , zval * rv )
227
236
{
228
- if (offset == NULL ) {
229
- php_error ( E_ERROR , "Cannot apply [] to ResourceBundle object" );
237
+ if (offset == NULL ) {
238
+ zend_throw_error (NULL , "Cannot apply [] to ResourceBundle object" );
239
+ return NULL ;
240
+ }
241
+
242
+ if (Z_TYPE_P (offset ) == IS_LONG ) {
243
+ return resource_bundle_array_fetch (object , /* offset_str */ NULL , Z_LVAL_P (offset ), rv , /* fallback */ true, /* arg_num */ 0 );
244
+ } else if (Z_TYPE_P (offset ) == IS_STRING ) {
245
+ return resource_bundle_array_fetch (object , Z_STR_P (offset ), /* offset_int */ 0 , rv , /* fallback */ true, /* arg_num */ 0 );
246
+ } else {
247
+ zend_illegal_container_offset (object -> ce -> name , offset , type );
248
+ return NULL ;
230
249
}
231
- ZVAL_NULL (rv );
232
- resourcebundle_array_fetch (object , offset , rv , 1 );
233
- return rv ;
234
250
}
235
251
/* }}} */
236
252
237
253
/* {{{ Get resource identified by numerical index or key name. */
238
254
PHP_FUNCTION ( resourcebundle_get )
239
255
{
240
- bool fallback = 1 ;
241
- zval * offset ;
242
- zval * object ;
243
-
244
- if (zend_parse_method_parameters (ZEND_NUM_ARGS (), getThis (), "Oz|b" , & object , ResourceBundle_ce_ptr , & offset , & fallback ) == FAILURE ) {
256
+ bool fallback = true;
257
+ zend_object * resource_bundle = NULL ;
258
+ zend_string * offset_str = NULL ;
259
+ zend_long offset_long = 0 ;
260
+
261
+ ZEND_PARSE_PARAMETERS_START (2 , 3 )
262
+ Z_PARAM_OBJ_OF_CLASS (resource_bundle , ResourceBundle_ce_ptr )
263
+ Z_PARAM_STR_OR_LONG (offset_str , offset_long )
264
+ Z_PARAM_OPTIONAL
265
+ Z_PARAM_BOOL (fallback )
266
+ ZEND_PARSE_PARAMETERS_END ();
267
+
268
+ zval * retval = resource_bundle_array_fetch (resource_bundle , offset_str , offset_long , return_value , fallback , /* arg_num */ 2 );
269
+ if (!retval ) {
245
270
RETURN_THROWS ();
246
271
}
272
+ }
273
+ /* }}} */
247
274
248
- resourcebundle_array_fetch (Z_OBJ_P (object ), offset , return_value , fallback );
275
+ PHP_METHOD (ResourceBundle , get )
276
+ {
277
+ bool fallback = true;
278
+ zend_string * offset_str = NULL ;
279
+ zend_long offset_long = 0 ;
280
+
281
+ ZEND_PARSE_PARAMETERS_START (1 , 2 )
282
+ Z_PARAM_STR_OR_LONG (offset_str , offset_long )
283
+ Z_PARAM_OPTIONAL
284
+ Z_PARAM_BOOL (fallback )
285
+ ZEND_PARSE_PARAMETERS_END ();
286
+
287
+ zval * retval = resource_bundle_array_fetch (Z_OBJ_P (ZEND_THIS ), offset_str , offset_long , return_value , fallback , /* arg_num */ 1 );
288
+ if (!retval ) {
289
+ RETURN_THROWS ();
290
+ }
249
291
}
250
292
/* }}} */
251
293
0 commit comments