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

Generated by: LCOV version 1.10

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

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