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: 71 82 86.6 %
Date: 2015-05-21 Functions: 7 7 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          29 : static inline DateFormat *fetch_datefmt(IntlDateFormatter_object *dfo) {
      37          29 :         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             :         size_t str_len;
      49          12 :         DATE_FORMAT_METHOD_INIT_VARS;
      50             : 
      51          12 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), 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);
      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(), 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 );
      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);
      94           0 :                 RETURN_FALSE;
      95             :         }
      96             : 
      97           5 :         timezone_object_construct(tz_clone, return_value, 1);
      98             : }
      99             : 
     100             : /* {{{ proto boolean IntlDateFormatter::setTimeZone(mixed $timezone)
     101             :  * Set formatter's timezone. */
     102          11 : U_CFUNC PHP_FUNCTION(datefmt_set_timezone)
     103             : {
     104             :         zval            *timezone_zv;
     105             :         TimeZone        *timezone;
     106             : 
     107          11 :         DATE_FORMAT_METHOD_INIT_VARS;
     108             : 
     109          11 :         if ( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
     110             :                         "Oz", &object, IntlDateFormatter_ce_ptr, &timezone_zv) == FAILURE) {
     111             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_set_timezone: "
     112           5 :                                 "unable to parse input params", 0);
     113           5 :                 RETURN_FALSE;
     114             :         }
     115             : 
     116           6 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
     117             : 
     118             :         timezone = timezone_process_timezone_argument(timezone_zv,
     119           6 :                         INTL_DATA_ERROR_P(dfo), "datefmt_set_timezone");
     120           6 :         if (timezone == NULL) {
     121           2 :                 RETURN_FALSE;
     122             :         }
     123             : 
     124           4 :         fetch_datefmt(dfo)->adoptTimeZone(timezone);
     125             : }
     126             : 
     127             : /* {{{ proto int IntlDateFormatter::getCalendar( )
     128             :  * Get formatter calendar type. }}} */
     129             : /* {{{ proto int datefmt_get_calendar(IntlDateFormatter $mf)
     130             :  * Get formatter calendar type.
     131             :  */
     132           4 : U_CFUNC PHP_FUNCTION(datefmt_get_calendar)
     133             : {
     134           4 :         DATE_FORMAT_METHOD_INIT_VARS;
     135             : 
     136           4 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
     137             :                         &object, IntlDateFormatter_ce_ptr ) == FAILURE) {
     138             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     139           1 :                         "datefmt_get_calendar: unable to parse input params", 0);
     140           1 :                 RETURN_FALSE;
     141             :         }
     142             : 
     143           3 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
     144             : 
     145           3 :         if (dfo->calendar == -1) {
     146             :                 /* an IntlCalendar was provided to the constructor */
     147           1 :                 RETURN_FALSE;
     148             :         }
     149             : 
     150           2 :         RETURN_LONG(dfo->calendar);
     151             : }
     152             : /* }}} */
     153             : 
     154             : /* {{{ proto IntlCalendar IntlDateFormatter::getCalendarObject()
     155             :  * Get formatter calendar. }}} */
     156             : /* {{{ proto IntlCalendar datefmt_get_calendar_object(IntlDateFormatter $mf)
     157             :  * Get formatter calendar.
     158             :  */
     159          11 : U_CFUNC PHP_FUNCTION(datefmt_get_calendar_object)
     160             : {
     161          11 :         DATE_FORMAT_METHOD_INIT_VARS;
     162             : 
     163          11 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
     164             :                         &object, IntlDateFormatter_ce_ptr ) == FAILURE) {
     165             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     166             :                                 "datefmt_get_calendar_object: unable to parse input params",
     167           5 :                                 0);
     168           5 :                 RETURN_FALSE;
     169             :         }
     170             : 
     171           6 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
     172             : 
     173           6 :         const Calendar *cal = fetch_datefmt(dfo)->getCalendar();
     174           6 :         if (cal == NULL) {
     175           0 :                 RETURN_NULL();
     176             :         }
     177             : 
     178           6 :         Calendar *cal_clone = cal->clone();
     179           6 :         if (cal_clone == NULL) {
     180             :                 intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
     181             :                                 "datefmt_get_calendar_object: Out of memory when cloning "
     182           0 :                                 "calendar", 0);
     183           0 :                 RETURN_FALSE;
     184             :         }
     185             : 
     186           6 :         calendar_object_create(return_value, cal_clone);
     187             : }
     188             : /* }}} */
     189             : 
     190             : /* {{{ proto bool IntlDateFormatter::setCalendar(mixed $calendar)
     191             :  * Set formatter's calendar. }}} */
     192             : /* {{{ proto bool datefmt_set_calendar(IntlDateFormatter $mf, mixed $calendar)
     193             :  * Set formatter's calendar.
     194             :  */
     195           3 : U_CFUNC PHP_FUNCTION(datefmt_set_calendar)
     196             : {
     197             :         zval    *calendar_zv;
     198           3 :         DATE_FORMAT_METHOD_INIT_VARS;
     199             : 
     200           3 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oz",
     201             :                         &object, IntlDateFormatter_ce_ptr, &calendar_zv) == FAILURE) {
     202             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     203           1 :                         "datefmt_set_calendar: unable to parse input params", 0);
     204           1 :                 RETURN_FALSE;
     205             :         }
     206             : 
     207           2 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
     208             : 
     209             :         Calendar        *cal;
     210             :         zend_long               cal_type;
     211             :         bool            cal_owned;
     212           2 :         Locale          locale = Locale::createFromName(dfo->requested_locale);
     213             :         // getting the actual locale from the DateFormat is not enough
     214             :         // because we would have lost modifiers such as @calendar. We
     215             :         // must store the requested locale on object creation
     216             : 
     217           2 :         if (datefmt_process_calendar_arg(calendar_zv, locale,
     218             :                         "datefmt_set_calendar",       INTL_DATA_ERROR_P(dfo), cal, cal_type,
     219             :                         cal_owned) == FAILURE) {
     220           0 :                 RETURN_FALSE;
     221             :         }
     222             : 
     223           2 :         if (cal_owned) {
     224             :                 /* a non IntlCalendar was specified, we want to keep the timezone */
     225           1 :                 TimeZone *old_timezone = fetch_datefmt(dfo)->getTimeZone().clone();
     226           1 :                 if (old_timezone == NULL) {
     227             :                         intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
     228             :                                         "datefmt_set_calendar: Out of memory when cloning calendar",
     229           0 :                                         0);
     230           0 :                         delete cal;
     231           0 :                         RETURN_FALSE;
     232             :                 }
     233           1 :                 cal->adoptTimeZone(old_timezone);
     234             :         } else {
     235           1 :                 cal = cal->clone();
     236           1 :                 if (cal == 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);
     240           0 :                         RETURN_FALSE;
     241             :                 }
     242             :         }
     243             : 
     244           2 :         fetch_datefmt(dfo)->adoptCalendar(cal);
     245             : 
     246           2 :         dfo->calendar = cal_type;
     247             : 
     248           2 :         RETURN_TRUE;
     249             : }
     250             : /* }}} */
     251             : 

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:58:56 +0000 (2 days ago)

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