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_attrcpp.cpp (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 75 86 87.2 %
Date: 2014-08-04 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       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: Gustavo Lopes <cataphract@php.net>                          |
      14             :    +----------------------------------------------------------------------+
      15             : */
      16             : 
      17             : #include "../intl_cppshims.h"
      18             : 
      19             : #include <unicode/timezone.h>
      20             : #include <unicode/calendar.h>
      21             : #include <unicode/datefmt.h>
      22             : 
      23             : extern "C" {
      24             : #include "../php_intl.h"
      25             : #include "dateformat_class.h"
      26             : #include "dateformat_attrcpp.h"
      27             : #define USE_TIMEZONE_POINTER 1
      28             : #include "../timezone/timezone_class.h"
      29             : #define USE_CALENDAR_POINTER 1
      30             : #include "../calendar/calendar_class.h"
      31             : }
      32             : 
      33             : #include "../intl_convertcpp.h"
      34             : #include "dateformat_helpers.h"
      35             : 
      36          30 : static inline DateFormat *fetch_datefmt(IntlDateFormatter_object *dfo) {
      37          30 :         return (DateFormat *)dfo->datef_data.udatf;
      38             : }
      39             : 
      40             : /* {{{ proto string IntlDateFormatter::getTimeZoneId()
      41             :  * Get formatter timezone_id. }}} */
      42             : /* {{{ proto string datefmt_get_timezone_id(IntlDateFormatter $mf)
      43             :  * Get formatter timezone_id.
      44             :  */
      45          12 : U_CFUNC PHP_FUNCTION(datefmt_get_timezone_id)
      46             : {
      47          12 :         DATE_FORMAT_METHOD_INIT_VARS;
      48             : 
      49          12 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
      50             :                         &object, IntlDateFormatter_ce_ptr ) == FAILURE) {
      51             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,  "datefmt_get_timezone_"
      52           1 :                                 "id: unable to parse input params", 0 TSRMLS_CC);
      53           1 :                 RETURN_FALSE;
      54             :         }
      55             : 
      56          11 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
      57             : 
      58          11 :         UnicodeString res = UnicodeString();
      59          11 :         fetch_datefmt(dfo)->getTimeZone().getID(res);
      60             :         intl_charFromString(res, &Z_STRVAL_P(return_value),
      61          11 :                         &Z_STRLEN_P(return_value), &INTL_DATA_ERROR_CODE(dfo));
      62          11 :         INTL_METHOD_CHECK_STATUS(dfo, "Could not convert time zone id to UTF-8");
      63             : 
      64          11 :         Z_TYPE_P(return_value) = IS_STRING;
      65             : }
      66             : 
      67             : /* {{{ proto IntlTimeZone IntlDateFormatter::getTimeZone()
      68             :  * Get formatter timezone. }}} */
      69             : /* {{{ proto IntlTimeZone datefmt_get_timezone(IntlDateFormatter $mf)
      70             :  * Get formatter timezone.
      71             :  */
      72          10 : U_CFUNC PHP_FUNCTION(datefmt_get_timezone)
      73             : {
      74          10 :         DATE_FORMAT_METHOD_INIT_VARS;
      75             : 
      76          10 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
      77             :                         &object, IntlDateFormatter_ce_ptr ) == FAILURE) {
      78             :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
      79           5 :                         "datefmt_get_timezone: unable to parse input params", 0 TSRMLS_CC );
      80           5 :                 RETURN_FALSE;
      81             :         }
      82             : 
      83           5 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
      84             : 
      85           5 :         const TimeZone& tz = fetch_datefmt(dfo)->getTimeZone();
      86           5 :         TimeZone *tz_clone = tz.clone();
      87           5 :         if (tz_clone == NULL) {
      88             :                 intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
      89             :                                 "datefmt_get_timezone: Out of memory when cloning time zone",
      90           0 :                                 0 TSRMLS_CC);
      91           0 :                 RETURN_FALSE;
      92             :         }
      93             : 
      94           5 :         object_init_ex(return_value, TimeZone_ce_ptr);
      95           5 :         timezone_object_construct(tz_clone, return_value, 1 TSRMLS_CC);
      96             : }
      97             : 
      98           2 : U_CFUNC PHP_FUNCTION(datefmt_set_timezone_id)
      99             : {
     100             :         php_error_docref0(NULL TSRMLS_CC, E_DEPRECATED,
     101             :                         "Use datefmt_set_timezone() instead, which also accepts a plain "
     102             :                         "time zone identifier and for which this function is now an "
     103           2 :                         "alias");
     104           2 :         PHP_FN(datefmt_set_timezone)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     105           2 : }
     106             : 
     107             : /* {{{ proto boolean IntlDateFormatter::setTimeZone(mixed $timezone)
     108             :  * Set formatter's timezone. }}} */
     109             : /* {{{ proto boolean datefmt_set_timezone_id(IntlDateFormatter $mf, $timezone_id)
     110             :  * Set formatter timezone_id.
     111             :  */
     112          13 : U_CFUNC PHP_FUNCTION(datefmt_set_timezone)
     113             : {
     114             :         zval            **timezone_zv;
     115             :         TimeZone        *timezone;
     116             : 
     117          13 :         DATE_FORMAT_METHOD_INIT_VARS;
     118             : 
     119          13 :         if ( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(),
     120             :                         "OZ", &object, IntlDateFormatter_ce_ptr, &timezone_zv) == FAILURE) {
     121             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_set_timezone: "
     122           6 :                                 "unable to parse input params", 0 TSRMLS_CC);
     123           6 :                 RETURN_FALSE;
     124             :         }
     125             : 
     126           7 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
     127             : 
     128             :         timezone = timezone_process_timezone_argument(timezone_zv,
     129           7 :                         INTL_DATA_ERROR_P(dfo), "datefmt_set_timezone" TSRMLS_CC);
     130           7 :         if (timezone == NULL) {
     131           2 :                 RETURN_FALSE;
     132             :         }
     133             : 
     134           5 :         fetch_datefmt(dfo)->adoptTimeZone(timezone);
     135             : }
     136             : 
     137             : /* {{{ proto int IntlDateFormatter::getCalendar( )
     138             :  * Get formatter calendar type. }}} */
     139             : /* {{{ proto int datefmt_get_calendar(IntlDateFormatter $mf)
     140             :  * Get formatter calendar type.
     141             :  */
     142           4 : U_CFUNC PHP_FUNCTION(datefmt_get_calendar)
     143             : {
     144           4 :         DATE_FORMAT_METHOD_INIT_VARS;
     145             : 
     146           4 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
     147             :                         &object, IntlDateFormatter_ce_ptr ) == FAILURE) {
     148             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     149           1 :                         "datefmt_get_calendar: unable to parse input params", 0 TSRMLS_CC);
     150           1 :                 RETURN_FALSE;
     151             :         }
     152             : 
     153           3 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
     154             : 
     155           3 :         if (dfo->calendar == -1) {
     156             :                 /* an IntlCalendar was provided to the constructor */
     157           1 :                 RETURN_FALSE;
     158             :         }
     159             : 
     160           2 :         RETURN_LONG(dfo->calendar);
     161             : }
     162             : /* }}} */
     163             : 
     164             : /* {{{ proto IntlCalendar IntlDateFormatter::getCalendarObject()
     165             :  * Get formatter calendar. }}} */
     166             : /* {{{ proto IntlCalendar datefmt_get_calendar_object(IntlDateFormatter $mf)
     167             :  * Get formatter calendar.
     168             :  */
     169          11 : U_CFUNC PHP_FUNCTION(datefmt_get_calendar_object)
     170             : {
     171          11 :         DATE_FORMAT_METHOD_INIT_VARS;
     172             : 
     173          11 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
     174             :                         &object, IntlDateFormatter_ce_ptr ) == FAILURE) {
     175             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     176             :                                 "datefmt_get_calendar_object: unable to parse input params",
     177           5 :                                 0 TSRMLS_CC);
     178           5 :                 RETURN_FALSE;
     179             :         }
     180             : 
     181           6 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
     182             : 
     183           6 :         const Calendar *cal = fetch_datefmt(dfo)->getCalendar();
     184           6 :         if (cal == NULL) {
     185           0 :                 RETURN_NULL();
     186             :         }
     187             : 
     188           6 :         Calendar *cal_clone = cal->clone();
     189           6 :         if (cal_clone == NULL) {
     190             :                 intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
     191             :                                 "datefmt_get_calendar_object: Out of memory when cloning "
     192           0 :                                 "calendar", 0 TSRMLS_CC);
     193           0 :                 RETURN_FALSE;
     194             :         }
     195             : 
     196           6 :         calendar_object_create(return_value, cal_clone TSRMLS_CC);
     197             : }
     198             : /* }}} */
     199             : 
     200             : /* {{{ proto bool IntlDateFormatter::setCalendar(mixed $calendar)
     201             :  * Set formatter's calendar. }}} */
     202             : /* {{{ proto bool datefmt_set_calendar(IntlDateFormatter $mf, mixed $calendar)
     203             :  * Set formatter's calendar.
     204             :  */
     205           3 : U_CFUNC PHP_FUNCTION(datefmt_set_calendar)
     206             : {
     207             :         zval    *calendar_zv;
     208           3 :         DATE_FORMAT_METHOD_INIT_VARS;
     209             : 
     210           3 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz",
     211             :                         &object, IntlDateFormatter_ce_ptr, &calendar_zv) == FAILURE) {
     212             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     213           1 :                         "datefmt_set_calendar: unable to parse input params", 0 TSRMLS_CC);
     214           1 :                 RETURN_FALSE;
     215             :         }
     216             : 
     217           2 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
     218             : 
     219             :         Calendar        *cal;
     220             :         long            cal_type;
     221             :         bool            cal_owned;
     222           2 :         Locale          locale = Locale::createFromName(dfo->requested_locale);
     223             :         // getting the actual locale from the DateFormat is not enough
     224             :         // because we would have lost modifiers such as @calendar. We
     225             :         // must store the requested locale on object creation
     226             : 
     227           2 :         if (datefmt_process_calendar_arg(calendar_zv, locale,
     228             :                         "datefmt_set_calendar",       INTL_DATA_ERROR_P(dfo), cal, cal_type,
     229             :                         cal_owned TSRMLS_CC) == FAILURE) {
     230           0 :                 RETURN_FALSE;
     231             :         }
     232             : 
     233           2 :         if (cal_owned) {
     234             :                 /* a non IntlCalendar was specified, we want to keep the timezone */
     235           1 :                 TimeZone *old_timezone = fetch_datefmt(dfo)->getTimeZone().clone();
     236           1 :                 if (old_timezone == NULL) {
     237             :                         intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
     238             :                                         "datefmt_set_calendar: Out of memory when cloning calendar",
     239           0 :                                         0 TSRMLS_CC);
     240           0 :                         delete cal;
     241           0 :                         RETURN_FALSE;
     242             :                 }
     243           1 :                 cal->adoptTimeZone(old_timezone);
     244             :         } else {
     245           1 :                 cal = cal->clone();
     246           1 :                 if (cal == NULL) {
     247             :                         intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
     248             :                                         "datefmt_set_calendar: Out of memory when cloning calendar",
     249           0 :                                         0 TSRMLS_CC);
     250           0 :                         RETURN_FALSE;
     251             :                 }
     252             :         }
     253             : 
     254           2 :         fetch_datefmt(dfo)->adoptCalendar(cal);
     255             : 
     256           2 :         dfo->calendar = cal_type;
     257             : 
     258           2 :         RETURN_TRUE;
     259             : }
     260             : /* }}} */
     261             : 

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:05 +0000 (29 days ago)

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