PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/intl/resourcebundle - resourcebundle_class.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 134 165 81.2 %
Date: 2019-05-06 Functions: 14 14 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | This source file is subject to version 3.01 of the PHP license,      |
       6             :    | that is bundled with this package in the file LICENSE, and is        |
       7             :    | available through the world-wide-web at the following url:           |
       8             :    | http://www.php.net/license/3_01.txt                                  |
       9             :    | If you did not receive a copy of the PHP license and are unable to   |
      10             :    | obtain it through the world-wide-web, please send a note to          |
      11             :    | license@php.net so we can mail you a copy immediately.               |
      12             :    +----------------------------------------------------------------------+
      13             :    | Authors: Hans-Peter Oeri (University of St.Gallen) <hp@oeri.ch>      |
      14             :    +----------------------------------------------------------------------+
      15             :  */
      16             : 
      17             : #include <stdlib.h>
      18             : #include <unicode/ures.h>
      19             : #include <unicode/uenum.h>
      20             : 
      21             : #include <zend.h>
      22             : #include <Zend/zend_exceptions.h>
      23             : #include <Zend/zend_interfaces.h>
      24             : #include <php.h>
      25             : 
      26             : #include "php_intl.h"
      27             : #include "intl_data.h"
      28             : #include "intl_common.h"
      29             : 
      30             : #include "resourcebundle/resourcebundle.h"
      31             : #include "resourcebundle/resourcebundle_iterator.h"
      32             : #include "resourcebundle/resourcebundle_class.h"
      33             : 
      34             : zend_class_entry *ResourceBundle_ce_ptr = NULL;
      35             : 
      36             : static zend_object_handlers ResourceBundle_object_handlers;
      37             : 
      38             : /* {{{ ResourceBundle_object_free */
      39          38 : static void ResourceBundle_object_free( zend_object *object )
      40             : {
      41          38 :         ResourceBundle_object *rb = php_intl_resourcebundle_fetch_object(object);
      42             : 
      43             :         // only free local errors
      44          38 :         intl_error_reset( INTL_DATA_ERROR_P(rb) );
      45             : 
      46          38 :         if (rb->me) {
      47          33 :                 ures_close( rb->me );
      48             :         }
      49          38 :         if (rb->child) {
      50          17 :                 ures_close( rb->child );
      51             :         }
      52             : 
      53          38 :         zend_object_std_dtor( &rb->zend );
      54          38 : }
      55             : /* }}} */
      56             : 
      57             : /* {{{ ResourceBundle_object_create */
      58          38 : static zend_object *ResourceBundle_object_create( zend_class_entry *ce )
      59             : {
      60             :         ResourceBundle_object *rb;
      61             : 
      62          38 :         rb = zend_object_alloc(sizeof(ResourceBundle_object), ce);
      63             : 
      64          38 :         zend_object_std_init( &rb->zend, ce );
      65          38 :         object_properties_init( &rb->zend, ce);
      66             : 
      67          38 :         intl_error_init( INTL_DATA_ERROR_P(rb) );
      68          38 :         rb->me = NULL;
      69          38 :         rb->child = NULL;
      70             : 
      71          38 :         rb->zend.handlers = &ResourceBundle_object_handlers;
      72             : 
      73          38 :         return &rb->zend;
      74             : }
      75             : /* }}} */
      76             : 
      77             : /* {{{ ResourceBundle_ctor */
      78          21 : static int resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor)
      79             : {
      80             :         const char *bundlename;
      81          21 :         size_t          bundlename_len = 0;
      82             :         const char *locale;
      83          21 :         size_t          locale_len = 0;
      84          21 :         zend_bool       fallback = 1;
      85             : 
      86          21 :         zval                  *object = return_value;
      87          21 :         ResourceBundle_object *rb = Z_INTL_RESOURCEBUNDLE_P( object );
      88             : 
      89          21 :         intl_error_reset( NULL );
      90             : 
      91          21 :         if( zend_parse_parameters( ZEND_NUM_ARGS(), "s!s!|b",
      92             :                 &locale, &locale_len, &bundlename, &bundlename_len, &fallback ) == FAILURE )
      93             :         {
      94           1 :                 return FAILURE;
      95             :         }
      96             : 
      97          20 :         INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
      98             : 
      99          20 :         if (locale == NULL) {
     100           1 :                 locale = intl_locale_get_default();
     101             :         }
     102             : 
     103          20 :         if (bundlename_len >= MAXPATHLEN) {
     104           0 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "Bundle name too long", 0 );
     105           0 :                 zval_ptr_dtor(return_value);
     106           0 :                 ZVAL_NULL(return_value);
     107           0 :                 return FAILURE;
     108             :         }
     109             : 
     110          20 :         if (fallback) {
     111          17 :                 rb->me = ures_open(bundlename, locale, &INTL_DATA_ERROR_CODE(rb));
     112             :         } else {
     113           3 :                 rb->me = ures_openDirect(bundlename, locale, &INTL_DATA_ERROR_CODE(rb));
     114             :         }
     115             : 
     116          20 :         INTL_CTOR_CHECK_STATUS(rb, "resourcebundle_ctor: Cannot load libICU resource bundle");
     117             : 
     118          17 :         if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING ||
     119           1 :                         INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) {
     120             :                 char *pbuf;
     121           0 :                 intl_errors_set_code(NULL, INTL_DATA_ERROR_CODE(rb));
     122           0 :                 spprintf(&pbuf, 0, "resourcebundle_ctor: Cannot load libICU resource "
     123             :                                 "'%s' without fallback from %s to %s",
     124           0 :                                 bundlename ? bundlename : "(default data)", locale,
     125             :                                 ures_getLocaleByType(
     126           0 :                                         rb->me, ULOC_ACTUAL_LOCALE, &INTL_DATA_ERROR_CODE(rb)));
     127           0 :                 intl_errors_set_custom_msg(INTL_DATA_ERROR_P(rb), pbuf, 1);
     128           0 :                 efree(pbuf);
     129           0 :                 return FAILURE;
     130             :         }
     131             : 
     132          16 :         return SUCCESS;
     133             : }
     134             : /* }}} */
     135             : 
     136             : /* {{{ arginfo_resourcebundle__construct */
     137             : ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle___construct, 0, 0, 2 )
     138             :         ZEND_ARG_INFO( 0, locale )
     139             :         ZEND_ARG_INFO( 0, bundlename )
     140             :         ZEND_ARG_INFO( 0, fallback )
     141             : ZEND_END_ARG_INFO()
     142             : /* }}} */
     143             : 
     144             : /* {{{ proto ResourceBundle::__construct( string $locale [, string $bundlename [, bool $fallback = true ]] )
     145             :  * ResourceBundle object constructor
     146             :  */
     147          14 : PHP_METHOD( ResourceBundle, __construct )
     148             : {
     149             :         zend_error_handling error_handling;
     150             : 
     151          14 :         zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
     152          14 :         return_value = ZEND_THIS;
     153          14 :         if (resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1) == FAILURE) {
     154           2 :                 if (!EG(exception)) {
     155           2 :                         zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
     156             :                 }
     157             :         }
     158          14 :         zend_restore_error_handling(&error_handling);
     159          14 : }
     160             : /* }}} */
     161             : 
     162             : /* {{{ proto ResourceBundle ResourceBundle::create( string $locale [, string $bundlename [, bool $fallback = true ]] )
     163             : proto ResourceBundle resourcebundle_create( string $locale [, string $bundlename [, bool $fallback = true ]] )
     164             : */
     165           7 : PHP_FUNCTION( resourcebundle_create )
     166             : {
     167           7 :         object_init_ex( return_value, ResourceBundle_ce_ptr );
     168           7 :         if (resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) == FAILURE) {
     169           3 :                 zval_ptr_dtor(return_value);
     170           3 :                 RETURN_NULL();
     171             :         }
     172             : }
     173             : /* }}} */
     174             : 
     175             : /* {{{ resourcebundle_array_fetch */
     176          47 : static void resourcebundle_array_fetch(zend_object *object, zval *offset, zval *return_value, int fallback)
     177             : {
     178          47 :         int32_t     meindex = 0;
     179          47 :         char *      mekey = NULL;
     180          47 :     zend_bool    is_numeric = 0;
     181             :         char         *pbuf;
     182             :         ResourceBundle_object *rb;
     183             : 
     184          47 :         intl_error_reset( NULL );
     185          47 :         rb = php_intl_resourcebundle_fetch_object(object);
     186             : 
     187          47 :         if(Z_TYPE_P(offset) == IS_LONG) {
     188           5 :                 is_numeric = 1;
     189           5 :                 meindex = (int32_t)Z_LVAL_P(offset);
     190           5 :                 rb->child = ures_getByIndex( rb->me, meindex, rb->child, &INTL_DATA_ERROR_CODE(rb) );
     191          42 :         } else if(Z_TYPE_P(offset) == IS_STRING) {
     192          42 :                 mekey = Z_STRVAL_P(offset);
     193          42 :                 rb->child = ures_getByKey(rb->me, mekey, rb->child, &INTL_DATA_ERROR_CODE(rb) );
     194             :         } else {
     195           0 :                 intl_errors_set(INTL_DATA_ERROR_P(rb), U_ILLEGAL_ARGUMENT_ERROR,
     196             :                         "resourcebundle_get: index should be integer or string", 0);
     197           5 :                 RETURN_NULL();
     198             :         }
     199             : 
     200          47 :         intl_error_set_code( NULL, INTL_DATA_ERROR_CODE(rb) );
     201          47 :         if (U_FAILURE(INTL_DATA_ERROR_CODE(rb))) {
     202           5 :                 if (is_numeric) {
     203           0 :                         spprintf( &pbuf, 0, "Cannot load resource element %d", meindex );
     204             :                 } else {
     205           5 :                         spprintf( &pbuf, 0, "Cannot load resource element '%s'", mekey );
     206             :                 }
     207           5 :                 intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 );
     208           5 :                 efree(pbuf);
     209           5 :                 RETURN_NULL();
     210             :         }
     211             : 
     212          42 :         if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING || INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) {
     213             :                 UErrorCode icuerror;
     214           0 :                 const char * locale = ures_getLocaleByType( rb->me, ULOC_ACTUAL_LOCALE, &icuerror );
     215           0 :                 if (is_numeric) {
     216           0 :                         spprintf( &pbuf, 0, "Cannot load element %d without fallback from to %s", meindex, locale );
     217             :                 } else {
     218           0 :                         spprintf( &pbuf, 0, "Cannot load element '%s' without fallback from to %s", mekey, locale );
     219             :                 }
     220           0 :                 intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 );
     221           0 :                 efree(pbuf);
     222           0 :                 RETURN_NULL();
     223             :         }
     224             : 
     225          42 :         resourcebundle_extract_value( return_value, rb );
     226             : }
     227             : /* }}} */
     228             : 
     229             : /* {{{ resourcebundle_array_get */
     230          15 : zval *resourcebundle_array_get(zend_object *object, zval *offset, int type, zval *rv)
     231             : {
     232          15 :         if(offset == NULL) {
     233           0 :                 php_error( E_ERROR, "Cannot apply [] to ResourceBundle object" );
     234             :         }
     235          15 :         ZVAL_NULL(rv);
     236          15 :         resourcebundle_array_fetch(object, offset, rv, 1);
     237          15 :         return rv;
     238             : }
     239             : /* }}} */
     240             : 
     241             : /* {{{ arginfo_resourcebundle_get */
     242             : ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get, 0, 0, 1 )
     243             :         ZEND_ARG_INFO( 0, index )
     244             :         ZEND_ARG_INFO( 0, fallback )
     245             : ZEND_END_ARG_INFO()
     246             : /* }}} */
     247             : 
     248             : /* {{{ proto mixed ResourceBundle::get( int|string $resindex [, bool $fallback = true ] )
     249             :  * proto mixed resourcebundle_get( ResourceBundle $rb, int|string $resindex [, bool $fallback = true ] )
     250             :  * Get resource identified by numerical index or key name.
     251             :  */
     252          33 : PHP_FUNCTION( resourcebundle_get )
     253             : {
     254          33 :         zend_bool   fallback = 1;
     255             :         zval *          offset;
     256             :         zval *      object;
     257             : 
     258          66 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oz|b",  &object, ResourceBundle_ce_ptr, &offset, &fallback ) == FAILURE) {
     259           1 :                 RETURN_FALSE;
     260             :         }
     261             : 
     262          32 :         resourcebundle_array_fetch(Z_OBJ_P(object), offset, return_value, fallback);
     263             : }
     264             : /* }}} */
     265             : 
     266             : /* {{{ resourcebundle_array_count */
     267           1 : int resourcebundle_array_count(zend_object *object, zend_long *count)
     268             : {
     269           1 :         ResourceBundle_object *rb = php_intl_resourcebundle_fetch_object(object);
     270             : 
     271           1 :         if (rb->me == NULL) {
     272           0 :                 intl_errors_set(&rb->error, U_ILLEGAL_ARGUMENT_ERROR,
     273             :                                 "Found unconstructed ResourceBundle", 0);
     274           0 :                 return 0;
     275             :         }
     276             : 
     277           1 :         *count = ures_getSize( rb->me );
     278             : 
     279           1 :         return SUCCESS;
     280             : }
     281             : /* }}} */
     282             : 
     283             : /* {{{ arginfo_resourcebundle_count */
     284             : ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_count, 0, 0, 0 )
     285             : ZEND_END_ARG_INFO()
     286             : /* }}} */
     287             : 
     288             : /* {{{ proto int ResourceBundle::count()
     289             :  * proto int resourcebundle_count( ResourceBundle $bundle )
     290             :  * Get resources count
     291             :  */
     292           3 : PHP_FUNCTION( resourcebundle_count )
     293             : {
     294             :         int32_t                len;
     295           3 :         RESOURCEBUNDLE_METHOD_INIT_VARS;
     296             : 
     297           6 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O", &object, ResourceBundle_ce_ptr ) == FAILURE ) {
     298           1 :                 RETURN_FALSE;
     299             :         }
     300             : 
     301           2 :         RESOURCEBUNDLE_METHOD_FETCH_OBJECT;
     302             : 
     303           2 :         len = ures_getSize( rb->me );
     304           2 :         RETURN_LONG( len );
     305             : }
     306             : 
     307             : /* {{{ arginfo_resourcebundle_getlocales */
     308             : ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_getlocales, 0, 0, 1 )
     309             :         ZEND_ARG_INFO( 0, bundlename )
     310             : ZEND_END_ARG_INFO()
     311             : /* }}} */
     312             : 
     313             : /* {{{ proto array ResourceBundle::getLocales( string $bundlename )
     314             :  * proto array resourcebundle_locales( string $bundlename )
     315             :  * Get available locales from ResourceBundle name
     316             :  */
     317           3 : PHP_FUNCTION( resourcebundle_locales )
     318             : {
     319             :         char * bundlename;
     320           3 :         size_t    bundlename_len = 0;
     321             :         const char * entry;
     322             :         int entry_len;
     323             :         UEnumeration *icuenum;
     324           3 :         UErrorCode   icuerror = U_ZERO_ERROR;
     325             : 
     326           3 :         intl_errors_reset( NULL );
     327             : 
     328           3 :         if( zend_parse_parameters(ZEND_NUM_ARGS(), "s", &bundlename, &bundlename_len ) == FAILURE )
     329             :         {
     330           2 :                 RETURN_FALSE;
     331             :         }
     332             : 
     333           2 :         if (bundlename_len >= MAXPATHLEN) {
     334           0 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "resourcebundle_locales: bundle name too long", 0 );
     335           0 :                 RETURN_FALSE;
     336             :         }
     337             : 
     338           2 :         if(bundlename_len == 0) {
     339             :                 // fetch default locales list
     340           0 :                 bundlename = NULL;
     341             :         }
     342             : 
     343           2 :         icuenum = ures_openAvailableLocales( bundlename, &icuerror );
     344           2 :         INTL_CHECK_STATUS(icuerror, "Cannot fetch locales list");
     345             : 
     346           2 :         uenum_reset( icuenum, &icuerror );
     347           2 :         INTL_CHECK_STATUS(icuerror, "Cannot iterate locales list");
     348             : 
     349           2 :         array_init( return_value );
     350           8 :         while ((entry = uenum_next( icuenum, &entry_len, &icuerror ))) {
     351           4 :                 add_next_index_stringl( return_value, (char *) entry, entry_len);
     352             :         }
     353           2 :         uenum_close( icuenum );
     354             : }
     355             : /* }}} */
     356             : 
     357             : /* {{{ arginfo_resourcebundle_get_error_code */
     358             : ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get_error_code, 0, 0, 0 )
     359             : ZEND_END_ARG_INFO()
     360             : /* }}} */
     361             : 
     362             : /* {{{ proto string ResourceBundle::getErrorCode( )
     363             :  * proto string resourcebundle_get_error_code( ResourceBundle $bundle )
     364             :  * Get text description for ResourceBundle's last error code.
     365             :  */
     366           1 : PHP_FUNCTION( resourcebundle_get_error_code )
     367             : {
     368           1 :         RESOURCEBUNDLE_METHOD_INIT_VARS;
     369             : 
     370           2 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
     371             :                 &object, ResourceBundle_ce_ptr ) == FAILURE )
     372             :         {
     373           1 :                 RETURN_FALSE;
     374             :         }
     375             : 
     376           0 :         rb = Z_INTL_RESOURCEBUNDLE_P( object );
     377             : 
     378           0 :         RETURN_LONG(INTL_DATA_ERROR_CODE(rb));
     379             : }
     380             : /* }}} */
     381             : 
     382             : /* {{{ arginfo_resourcebundle_get_error_message */
     383             : ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get_error_message, 0, 0, 0 )
     384             : ZEND_END_ARG_INFO()
     385             : /* }}} */
     386             : 
     387             : /* {{{ proto string ResourceBundle::getErrorMessage( )
     388             :  * proto string resourcebundle_get_error_message( ResourceBundle $bundle )
     389             :  * Get text description for ResourceBundle's last error.
     390             :  */
     391           1 : PHP_FUNCTION( resourcebundle_get_error_message )
     392             : {
     393           1 :         zend_string* message = NULL;
     394           1 :         RESOURCEBUNDLE_METHOD_INIT_VARS;
     395             : 
     396           2 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
     397             :                 &object, ResourceBundle_ce_ptr ) == FAILURE )
     398             :         {
     399           1 :                 RETURN_FALSE;
     400             :         }
     401             : 
     402           0 :         rb = Z_INTL_RESOURCEBUNDLE_P( object );
     403           0 :         message = intl_error_get_message(INTL_DATA_ERROR_P(rb));
     404           0 :         RETURN_STR(message);
     405             : }
     406             : /* }}} */
     407             : 
     408             : /* {{{ ResourceBundle_class_functions
     409             :  * Every 'ResourceBundle' class method has an entry in this table
     410             :  */
     411             : static const zend_function_entry ResourceBundle_class_functions[] = {
     412             :         PHP_ME( ResourceBundle, __construct, arginfo_resourcebundle___construct, ZEND_ACC_PUBLIC )
     413             :         ZEND_NAMED_ME( create, ZEND_FN( resourcebundle_create ), arginfo_resourcebundle___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
     414             :         ZEND_NAMED_ME( get, ZEND_FN(resourcebundle_get), arginfo_resourcebundle_get, ZEND_ACC_PUBLIC )
     415             :         ZEND_NAMED_ME( count, ZEND_FN(resourcebundle_count), arginfo_resourcebundle_count, ZEND_ACC_PUBLIC )
     416             :         ZEND_NAMED_ME( getLocales, ZEND_FN(resourcebundle_locales), arginfo_resourcebundle_getlocales, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC )
     417             :         ZEND_NAMED_ME( getErrorCode, ZEND_FN(resourcebundle_get_error_code), arginfo_resourcebundle_get_error_code, ZEND_ACC_PUBLIC )
     418             :         ZEND_NAMED_ME( getErrorMessage, ZEND_FN(resourcebundle_get_error_message), arginfo_resourcebundle_get_error_message, ZEND_ACC_PUBLIC )
     419             :         PHP_FE_END
     420             : };
     421             : /* }}} */
     422             : 
     423             : /* {{{ resourcebundle_register_class
     424             :  * Initialize 'ResourceBundle' class
     425             :  */
     426       23043 : void resourcebundle_register_class( void )
     427             : {
     428             :         zend_class_entry ce;
     429             : 
     430       23043 :         INIT_CLASS_ENTRY( ce, "ResourceBundle", ResourceBundle_class_functions );
     431             : 
     432       23043 :         ce.create_object = ResourceBundle_object_create;
     433       23043 :         ce.get_iterator = resourcebundle_get_iterator;
     434             : 
     435       23043 :         ResourceBundle_ce_ptr = zend_register_internal_class( &ce );
     436             : 
     437       23043 :         ResourceBundle_object_handlers = std_object_handlers;
     438       23043 :         ResourceBundle_object_handlers.offset = XtOffsetOf(ResourceBundle_object, zend);
     439       23043 :         ResourceBundle_object_handlers.clone_obj          = NULL; /* ICU ResourceBundle has no clone implementation */
     440       23043 :         ResourceBundle_object_handlers.free_obj = ResourceBundle_object_free;
     441       23043 :         ResourceBundle_object_handlers.read_dimension = resourcebundle_array_get;
     442       23043 :         ResourceBundle_object_handlers.count_elements = resourcebundle_array_count;
     443             : 
     444       23043 :         zend_class_implements(ResourceBundle_ce_ptr, 1, zend_ce_traversable);
     445       23043 : }
     446             : /* }}} */

Generated by: LCOV version 1.10

Generated at Mon, 06 May 2019 17:58:16 +0000 (15 days ago)

Copyright © 2005-2019 The PHP Group
All rights reserved.