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

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:16 +0000 (4 days ago)

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