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/dateformat - dateformat_create.cpp (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 73 76 96.1 %
Date: 2015-09-02 Functions: 3 3 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: Kirti Velankar <kirtig@yahoo-inc.com>                       |
      14             :    |          Gustavo Lopes <cataphract@php.net>                          |
      15             :    +----------------------------------------------------------------------+
      16             : */
      17             : 
      18             : #include "../intl_cppshims.h"
      19             : 
      20             : #include <unicode/timezone.h>
      21             : #include <unicode/calendar.h>
      22             : #include <unicode/datefmt.h>
      23             : 
      24             : extern "C" {
      25             : #include <unicode/ustring.h>
      26             : #include <unicode/udat.h>
      27             : 
      28             : #include "php_intl.h"
      29             : #include "dateformat_create.h"
      30             : #include "dateformat_class.h"
      31             : #define USE_TIMEZONE_POINTER 1
      32             : #include "../timezone/timezone_class.h"
      33             : #include "../intl_convert.h"
      34             : }
      35             : 
      36             : #include "dateformat_helpers.h"
      37             : #include "zend_exceptions.h"
      38             : 
      39             : /* {{{ */
      40         365 : static int datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor)
      41             : {
      42             :         zval            *object;
      43             : 
      44             :         const char      *locale_str;
      45         365 :         size_t          locale_len              = 0;
      46         365 :         Locale          locale;
      47         365 :     zend_long   date_type               = 0;
      48         365 :     zend_long   time_type               = 0;
      49         365 :         zval            *calendar_zv    = NULL;
      50         365 :         Calendar        *calendar               = NULL;
      51             :         zend_long       calendar_type;
      52             :         bool            calendar_owned;
      53         365 :         zval            *timezone_zv    = NULL;
      54         365 :         TimeZone        *timezone               = NULL;
      55             :         bool            explicit_tz;
      56         365 :     char*       pattern_str             = NULL;
      57         365 :     size_t      pattern_str_len = 0;
      58         365 :     UChar*      svalue                  = NULL;         /* UTF-16 pattern_str */
      59         365 :     int32_t     slength                 = 0;
      60             :         IntlDateFormatter_object* dfo;
      61         365 :         int zpp_flags = is_constructor ? ZEND_PARSE_PARAMS_THROW : 0;
      62             : 
      63         365 :         intl_error_reset(NULL);
      64         365 :         object = return_value;
      65             :         /* Parse parameters. */
      66         365 :     if (zend_parse_parameters_ex(zpp_flags, ZEND_NUM_ARGS(), "sll|zzs",
      67             :                         &locale_str, &locale_len, &date_type, &time_type, &timezone_zv,
      68             :                         &calendar_zv, &pattern_str, &pattern_str_len) == FAILURE) {
      69             :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_create: "
      70           1 :                                 "unable to parse input parameters", 0);
      71           1 :                 return FAILURE;
      72             :     }
      73             : 
      74         364 :         INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
      75         364 :         if (locale_len == 0) {
      76           8 :                 locale_str = intl_locale_get_default();
      77             :         }
      78         364 :         locale = Locale::createFromName(locale_str);
      79             : 
      80         364 :         DATE_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
      81             : 
      82         364 :         if (DATE_FORMAT_OBJECT(dfo) != NULL) {
      83             :                 intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR,
      84           1 :                                 "datefmt_create: cannot call constructor twice", 0);
      85           1 :                 return FAILURE;
      86             :         }
      87             : 
      88             :         /* process calendar */
      89         363 :         if (datefmt_process_calendar_arg(calendar_zv, locale, "datefmt_create",
      90             :                         INTL_DATA_ERROR_P(dfo), calendar, calendar_type,
      91             :                         calendar_owned)
      92             :                         == FAILURE) {
      93           3 :                 goto error;
      94             :         }
      95             : 
      96             :         /* process timezone */
      97         714 :         explicit_tz = timezone_zv != NULL && Z_TYPE_P(timezone_zv) != IS_NULL;
      98             : 
      99         360 :         if (explicit_tz || calendar_owned ) {
     100             :                 //we have an explicit time zone or a non-object calendar
     101             :                 timezone = timezone_process_timezone_argument(timezone_zv,
     102         357 :                                 INTL_DATA_ERROR_P(dfo), "datefmt_create");
     103         357 :                 if (timezone == NULL) {
     104           3 :                         goto error;
     105             :                 }
     106             :         }
     107             : 
     108             :         /* Convert pattern (if specified) to UTF-16. */
     109         357 :         if (pattern_str && pattern_str_len > 0) {
     110             :                 intl_convert_utf8_to_utf16(&svalue, &slength,
     111           4 :                                 pattern_str, pattern_str_len, &INTL_DATA_ERROR_CODE(dfo));
     112           4 :                 if (U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
     113             :                         /* object construction -> only set global error */
     114             :                         intl_error_set(NULL, INTL_DATA_ERROR_CODE(dfo), "datefmt_create: "
     115           1 :                                         "error converting pattern to UTF-16", 0);
     116           1 :                         goto error;
     117             :                 }
     118             :         }
     119             : 
     120         359 :         if (pattern_str && pattern_str_len > 0) {
     121             :                 DATE_FORMAT_OBJECT(dfo) = udat_open(UDAT_IGNORE, UDAT_IGNORE,
     122             :                                 locale_str, NULL, 0, svalue, slength,
     123           3 :                                 &INTL_DATA_ERROR_CODE(dfo));
     124             :         } else {
     125             :                 DATE_FORMAT_OBJECT(dfo) = udat_open((UDateFormatStyle)time_type,
     126             :                                 (UDateFormatStyle)date_type, locale_str, NULL, 0, svalue,
     127         353 :                                 slength, &INTL_DATA_ERROR_CODE(dfo));
     128             :         }
     129             : 
     130         356 :     if (!U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
     131         356 :                 DateFormat *df = (DateFormat*)DATE_FORMAT_OBJECT(dfo);
     132         356 :                 if (calendar_owned) {
     133         351 :                         df->adoptCalendar(calendar);
     134         351 :                         calendar_owned = false;
     135             :                 } else {
     136           5 :                         df->setCalendar(*calendar);
     137             :                 }
     138             : 
     139         356 :                 if (timezone != NULL) {
     140         353 :                         df->adoptTimeZone(timezone);
     141             :                 }
     142             :     } else {
     143             :                 intl_error_set(NULL, INTL_DATA_ERROR_CODE(dfo), "datefmt_create: date "
     144           0 :                                 "formatter creation failed", 0);
     145           0 :                 goto error;
     146             :         }
     147             : 
     148             :         /* Set the class variables */
     149         356 :         dfo->date_type                       = date_type;
     150         356 :         dfo->time_type                       = time_type;
     151         356 :         dfo->calendar                        = calendar_type;
     152         356 :         dfo->requested_locale        = estrdup(locale_str);
     153             : 
     154             : error:
     155         363 :         if (svalue) {
     156           3 :                 efree(svalue);
     157             :         }
     158         363 :         if (timezone != NULL && DATE_FORMAT_OBJECT(dfo) == NULL) {
     159           1 :                 delete timezone;
     160             :         }
     161         363 :         if (calendar != NULL && calendar_owned) {
     162           4 :                 delete calendar;
     163             :         }
     164             : 
     165         363 :         return U_FAILURE(intl_error_get_code(NULL)) ? FAILURE : SUCCESS;
     166             : }
     167             : /* }}} */
     168             : 
     169             : /* {{{ proto IntlDateFormatter IntlDateFormatter::create(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern] )
     170             :  * Create formatter. }}} */
     171             : /* {{{ proto IntlDateFormatter datefmt_create(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern)
     172             :  * Create formatter.
     173             :  */
     174         334 : U_CFUNC PHP_FUNCTION( datefmt_create )
     175             : {
     176         334 :     object_init_ex( return_value, IntlDateFormatter_ce_ptr );
     177         334 :         if (datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) == FAILURE) {
     178           3 :                 zval_ptr_dtor(return_value);
     179           3 :                 RETURN_NULL();
     180             :         }
     181             : }
     182             : /* }}} */
     183             : 
     184             : /* {{{ proto void IntlDateFormatter::__construct(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern])
     185             :  * IntlDateFormatter object constructor.
     186             :  */
     187          31 : U_CFUNC PHP_METHOD( IntlDateFormatter, __construct )
     188             : {
     189             :         zend_error_handling error_handling;
     190             : 
     191          31 :         zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
     192             :         /* return_value param is being changed, therefore we will always return
     193             :          * NULL here */
     194          31 :         return_value = getThis();
     195          31 :         if (datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1) == FAILURE) {
     196           6 :                 if (!EG(exception)) {
     197           0 :                         zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
     198             :                 }
     199             :         }
     200          31 :         zend_restore_error_handling(&error_handling);
     201          31 : }
     202             : /* }}} */

Generated by: LCOV version 1.10

Generated at Wed, 02 Sep 2015 17:19:11 +0000 (2 days ago)

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