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

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:16 +0000 (10 hours ago)

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