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/msgformat - msgformat.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 54 61 88.5 %
Date: 2016-08-28 Functions: 5 5 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: Stanislav Malyshev <stas@zend.com>                          |
      14             :    +----------------------------------------------------------------------+
      15             :  */
      16             : 
      17             : #ifdef HAVE_CONFIG_H
      18             : #include "config.h"
      19             : #endif
      20             : 
      21             : #include <unicode/ustring.h>
      22             : #include <unicode/umsg.h>
      23             : 
      24             : #include "php_intl.h"
      25             : #include "msgformat_class.h"
      26             : #include "msgformat_data.h"
      27             : #include "intl_convert.h"
      28             : 
      29             : /* {{{ */
      30          65 : static int msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor)
      31             : {
      32             :         const char* locale;
      33             :         char*       pattern;
      34          65 :         size_t      locale_len = 0, pattern_len = 0;
      35          65 :         UChar*      spattern     = NULL;
      36          65 :         int         spattern_len = 0;
      37             :         zval*       object;
      38             :         MessageFormatter_object* mfo;
      39          65 :         int zpp_flags = is_constructor ? ZEND_PARSE_PARAMS_THROW : 0;
      40          65 :         intl_error_reset( NULL );
      41             : 
      42          65 :         object = return_value;
      43             :         /* Parse parameters. */
      44          65 :         if( zend_parse_parameters_ex( zpp_flags, ZEND_NUM_ARGS(), "ss",
      45             :                 &locale, &locale_len, &pattern, &pattern_len ) == FAILURE )
      46             :         {
      47          10 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
      48             :                         "msgfmt_create: unable to parse input parameters", 0 );
      49          10 :                 return FAILURE;
      50             :         }
      51             : 
      52          55 :         INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
      53          55 :         MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
      54             : 
      55             :         /* Convert pattern (if specified) to UTF-16. */
      56         104 :         if(pattern && pattern_len) {
      57          52 :                 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
      58          52 :                 INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16");
      59             :         } else {
      60           3 :                 spattern_len = 0;
      61           3 :                 spattern = NULL;
      62             :         }
      63             : 
      64          52 :         if(locale_len == 0) {
      65           3 :                 locale = intl_locale_get_default();
      66             :         }
      67             : 
      68             : #ifdef MSG_FORMAT_QUOTE_APOS
      69          52 :         if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
      70           0 :                 INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format");
      71             :         }
      72             : #endif
      73             : 
      74          52 :         if ((mfo)->mf_data.orig_format) {
      75           0 :                 msgformat_data_free(&mfo->mf_data);
      76             :         }
      77             : 
      78          52 :         (mfo)->mf_data.orig_format = estrndup(pattern, pattern_len);
      79          52 :         (mfo)->mf_data.orig_format_len = pattern_len;
      80             : 
      81             :         /* Create an ICU message formatter. */
      82          52 :         MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(mfo));
      83             : 
      84          52 :         if(spattern) {
      85          49 :                 efree(spattern);
      86             :         }
      87             : 
      88          52 :         INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed");
      89          36 :         return SUCCESS;
      90             : }
      91             : /* }}} */
      92             : 
      93             : /* {{{ proto MessageFormatter MesssageFormatter::create( string $locale, string $pattern )
      94             :  * Create formatter. }}} */
      95             : /* {{{ proto MessageFormatter msgfmt_create( string $locale, string $pattern )
      96             :  * Create formatter.
      97             :  */
      98          50 : PHP_FUNCTION( msgfmt_create )
      99             : {
     100          50 :         object_init_ex( return_value, MessageFormatter_ce_ptr );
     101          50 :         if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) == FAILURE) {
     102          21 :                 zval_ptr_dtor(return_value);
     103          21 :                 RETURN_NULL();
     104             :         }
     105             : }
     106             : /* }}} */
     107             : 
     108             : /* {{{ proto void MessageFormatter::__construct( string $locale, string $pattern )
     109             :  * MessageFormatter object constructor.
     110             :  */
     111          15 : PHP_METHOD( MessageFormatter, __construct )
     112             : {
     113             :         zend_error_handling error_handling;
     114             : 
     115          15 :         zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
     116          30 :         return_value = getThis();
     117          15 :         if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1) == FAILURE) {
     118           8 :                 if (!EG(exception)) {
     119           5 :                         zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
     120             :                 }
     121             :         }
     122          15 :         zend_restore_error_handling(&error_handling);
     123          15 : }
     124             : /* }}} */
     125             : 
     126             : /* {{{ proto int MessageFormatter::getErrorCode()
     127             :  * Get formatter's last error code. }}} */
     128             : /* {{{ proto int msgfmt_get_error_code( MessageFormatter $nf )
     129             :  * Get formatter's last error code.
     130             :  */
     131           1 : PHP_FUNCTION( msgfmt_get_error_code )
     132             : {
     133           1 :         zval*                    object  = NULL;
     134           1 :         MessageFormatter_object*  mfo     = NULL;
     135             : 
     136             :         /* Parse parameters. */
     137           2 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
     138             :                 &object, MessageFormatter_ce_ptr ) == FAILURE )
     139             :         {
     140           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     141             :                         "msgfmt_get_error_code: unable to parse input params", 0 );
     142             : 
     143           1 :                 RETURN_FALSE;
     144             :         }
     145             : 
     146           0 :         mfo = Z_INTL_MESSAGEFORMATTER_P( object );
     147             : 
     148             :         /* Return formatter's last error code. */
     149           0 :         RETURN_LONG( INTL_DATA_ERROR_CODE(mfo) );
     150             : }
     151             : /* }}} */
     152             : 
     153             : /* {{{ proto string MessageFormatter::getErrorMessage( )
     154             :  * Get text description for formatter's last error code. }}} */
     155             : /* {{{ proto string msgfmt_get_error_message( MessageFormatter $coll )
     156             :  * Get text description for formatter's last error code.
     157             :  */
     158           1 : PHP_FUNCTION( msgfmt_get_error_message )
     159             : {
     160           1 :         zend_string*             message = NULL;
     161           1 :         zval*                    object  = NULL;
     162           1 :         MessageFormatter_object*  mfo     = NULL;
     163             : 
     164             :         /* Parse parameters. */
     165           2 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
     166             :                 &object, MessageFormatter_ce_ptr ) == FAILURE )
     167             :         {
     168           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     169             :                         "msgfmt_get_error_message: unable to parse input params", 0 );
     170             : 
     171           1 :                 RETURN_FALSE;
     172             :         }
     173             : 
     174           0 :         mfo = Z_INTL_MESSAGEFORMATTER_P( object );
     175             : 
     176             :         /* Return last error message. */
     177           0 :         message = intl_error_get_message( &mfo->mf_data.error );
     178           0 :         RETURN_STR(message);
     179             : }
     180             : /* }}} */
     181             : 
     182             : /*
     183             :  * Local variables:
     184             :  * tab-width: 4
     185             :  * c-basic-offset: 4
     186             :  * End:
     187             :  * vim600: noet sw=4 ts=4 fdm=marker
     188             :  * vim<600: noet sw=4 ts=4
     189             :  */

Generated by: LCOV version 1.10

Generated at Sun, 28 Aug 2016 17:10:02 +0000 (2 days ago)

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