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/calendar - gregoriancalendar_methods.cpp (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 90 123 73.2 %
Date: 2015-07-31 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             : #ifdef HAVE_CONFIG_H
      18             : #include "config.h"
      19             : #endif
      20             : 
      21             : #include "../intl_cppshims.h"
      22             : 
      23             : #include <unicode/locid.h>
      24             : #include <unicode/calendar.h>
      25             : #include <unicode/gregocal.h>
      26             : extern "C" {
      27             : #include "../php_intl.h"
      28             : #include "../intl_common.h"
      29             : #define USE_TIMEZONE_POINTER 1
      30             : #include "../timezone/timezone_class.h"
      31             : #define USE_CALENDAR_POINTER 1
      32             : #include "calendar_class.h"
      33             : #include <ext/date/php_date.h>
      34             : #include "zend_exceptions.h"
      35             : }
      36             : 
      37           9 : static inline GregorianCalendar *fetch_greg(Calendar_object *co) {
      38           9 :         return (GregorianCalendar*)co->ucal;
      39             : }
      40             : 
      41          55 : static void _php_intlgregcal_constructor_body(
      42             :     INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor)
      43             : {
      44          55 :         zval            *tz_object      = NULL;
      45          55 :         zval            args_a[6] = {0},
      46          55 :                                 *args           = &args_a[0];
      47          55 :         char            *locale         = NULL;
      48             :         size_t                  locale_len;
      49             :         zend_long               largs[6];
      50          55 :         UErrorCode      status          = U_ZERO_ERROR;
      51             :         int                     variant;
      52          55 :   int zpp_flags = is_constructor ? ZEND_PARSE_PARAMS_THROW : 0;
      53          55 :         intl_error_reset(NULL);
      54             : 
      55             :         // parameter number validation / variant determination
      56          55 :         if (ZEND_NUM_ARGS() > 6 ||
      57             :                         zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
      58             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
      59           2 :                         "intlgregcal_create_instance: too many arguments", 0);
      60           2 :                 if (!is_constructor) {          
      61             :                         zval_dtor(return_value);
      62           2 :                         RETVAL_NULL();
      63             :                 }
      64           2 :                 return;
      65             :         }
      66         103 :         for (variant = ZEND_NUM_ARGS();
      67             :                 variant > 0 && Z_TYPE(args[variant - 1]) == IS_NULL;
      68             :                 variant--) {}
      69          53 :         if (variant == 4) {
      70             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
      71             :                         "intlgregcal_create_instance: no variant with 4 arguments "
      72           2 :                         "(excluding trailing NULLs)", 0);
      73           2 :                 if (!is_constructor) {          
      74             :                         zval_dtor(return_value);
      75           1 :                         RETVAL_NULL();
      76             :                 }
      77           2 :                 return;
      78             :         }
      79             : 
      80             :         // argument parsing
      81          51 :         if (variant <= 2) {
      82          40 :                 if (zend_parse_parameters_ex(zpp_flags, MIN(ZEND_NUM_ARGS(), 2),
      83             :                                 "|z!s!", &tz_object, &locale, &locale_len) == FAILURE) {
      84             :                         intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
      85           0 :                                 "intlgregcal_create_instance: bad arguments", 0);
      86           0 :                         if (!is_constructor) {          
      87             :                                 zval_dtor(return_value);
      88           0 :                                 RETVAL_NULL();
      89             :                         }
      90           0 :                         return;
      91             :                 }
      92             :         }
      93          51 :         if (variant > 2 && zend_parse_parameters_ex(zpp_flags, ZEND_NUM_ARGS(),
      94             :                         "lll|lll", &largs[0], &largs[1], &largs[2], &largs[3], &largs[4],
      95             :                         &largs[5]) == FAILURE) {
      96             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
      97           1 :                         "intlgregcal_create_instance: bad arguments", 0);
      98           1 :                 if (!is_constructor) {          
      99             :                         zval_dtor(return_value);
     100           0 :                         RETVAL_NULL();
     101             :                 }
     102           1 :                 return;
     103             :         }
     104             : 
     105             :         // instantion of ICU object
     106          50 :         GregorianCalendar *gcal = NULL;
     107             : 
     108          50 :         if (variant <= 2) {
     109             :                 // From timezone and locale (0 to 2 arguments)
     110             :                 TimeZone *tz = timezone_process_timezone_argument(tz_object, NULL,
     111          40 :                         "intlgregcal_create_instance");
     112          40 :                 if (tz == NULL) {
     113           0 :                         if (!EG(exception)) {
     114           0 :                                 zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
     115             :                         }
     116           0 :                         if (!is_constructor) {          
     117             :                                 zval_dtor(return_value);
     118           0 :                                 RETVAL_NULL();
     119             :                         }
     120           0 :                         return;
     121             :                 }
     122          40 :                 if (!locale) {
     123          11 :                         locale = const_cast<char*>(intl_locale_get_default());
     124             :                 }
     125             : 
     126             :                 gcal = new GregorianCalendar(tz, Locale::createFromName(locale),
     127          40 :                         status);
     128          40 :                 if (U_FAILURE(status)) {
     129             :                         intl_error_set(NULL, status, "intlgregcal_create_instance: error "
     130           0 :                                 "creating ICU GregorianCalendar from time zone and locale", 0);
     131           0 :                         if (gcal) {
     132           0 :                                 delete gcal;
     133             :                         }
     134           0 :                         delete tz;
     135           0 :                         if (!is_constructor) {          
     136             :                                 zval_dtor(return_value);
     137           0 :                                 RETVAL_NULL();
     138             :                         }
     139           0 :                         return;
     140             :                 }
     141             :         } else {
     142             :                 // From date/time (3, 5 or 6 arguments)
     143          57 :                 for (int i = 0; i < variant; i++) {
     144          47 :                         if (largs[i] < INT32_MIN || largs[i] > INT32_MAX) {
     145             :                                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     146             :                                         "intlgregcal_create_instance: at least one of the arguments"
     147           0 :                                         " has an absolute value that is too large", 0);
     148           0 :                                 if (!is_constructor) {          
     149             :                                         zval_dtor(return_value);
     150           0 :                                         RETVAL_NULL();
     151             :                                 }
     152           0 :                                 return;
     153             :                         }
     154             :                 }
     155             : 
     156          10 :                 if (variant == 3) {
     157           8 :                         gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
     158          12 :                                 (int32_t)largs[2], status);
     159           6 :                 } else if (variant == 5) {
     160           2 :                         gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
     161           3 :                                 (int32_t)largs[2], (int32_t)largs[3], (int32_t)largs[4], status);
     162           5 :                 } else if (variant == 6) {
     163          10 :                         gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
     164          20 :                                 (int32_t)largs[2], (int32_t)largs[3], (int32_t)largs[4], (int32_t)largs[5],
     165          35 :                                 status);
     166             :                 }
     167          10 :                 if (U_FAILURE(status)) {
     168             :                         intl_error_set(NULL, status, "intlgregcal_create_instance: error "
     169           0 :                                 "creating ICU GregorianCalendar from date", 0);
     170           0 :                         if (gcal) {
     171           0 :                                 delete gcal;
     172             :                         }
     173           0 :                         if (!is_constructor) {          
     174             :                                 zval_dtor(return_value);
     175           0 :                                 RETVAL_NULL();
     176             :                         }
     177           0 :                         return;
     178             :                 }
     179             : 
     180          10 :                 timelib_tzinfo *tzinfo = get_timezone_info();
     181             : #if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 42
     182          10 :                 UnicodeString tzstr = UnicodeString::fromUTF8(StringPiece(tzinfo->name));
     183             : #else
     184             :                 UnicodeString tzstr = UnicodeString(tzinfo->name,
     185             :                         strlen(tzinfo->name), US_INV);
     186             : #endif
     187          10 :                 if (tzstr.isBogus()) {
     188             :                         intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     189             :                                 "intlgregcal_create_instance: could not create UTF-8 string "
     190             :                                 "from PHP's default timezone name (see date_default_timezone_get())",
     191           0 :                                 0);
     192           0 :                         delete gcal;
     193           0 :                         if (!is_constructor) {          
     194             :                                 zval_dtor(return_value);
     195           0 :                                 RETVAL_NULL();
     196             :                         }
     197             :                         return;
     198             :                 }
     199             : 
     200          10 :                 TimeZone *tz = TimeZone::createTimeZone(tzstr);
     201          10 :                 gcal->adoptTimeZone(tz);
     202             :         }
     203             : 
     204          50 :     Calendar_object *co = Z_INTL_CALENDAR_P(return_value);
     205          50 :     co->ucal = gcal;
     206             : }
     207             : 
     208           4 : U_CFUNC PHP_FUNCTION(intlgregcal_create_instance)
     209             : {
     210           4 :         intl_error_reset(NULL);
     211             : 
     212           4 :         object_init_ex(return_value, GregorianCalendar_ce_ptr);
     213           4 :         _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
     214           4 : }
     215             : 
     216          51 : U_CFUNC PHP_METHOD(IntlGregorianCalendar, __construct)
     217             : {
     218             :         zend_error_handling error_handling;
     219             : 
     220          51 :         zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
     221          51 :         return_value = getThis();
     222          51 :         _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
     223          51 :         zend_restore_error_handling(&error_handling);
     224          51 : }
     225             : 
     226           6 : U_CFUNC PHP_FUNCTION(intlgregcal_set_gregorian_change)
     227             : {
     228             :         double date;
     229           6 :         CALENDAR_METHOD_INIT_VARS;
     230             : 
     231           6 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
     232             :                         "Od", &object, GregorianCalendar_ce_ptr, &date) == FAILURE) {
     233             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     234           4 :                         "intlgregcal_set_gregorian_change: bad arguments", 0);
     235           4 :                 RETURN_FALSE;
     236             :         }
     237             : 
     238           2 :         CALENDAR_METHOD_FETCH_OBJECT;
     239             : 
     240           2 :         fetch_greg(co)->setGregorianChange(date, CALENDAR_ERROR_CODE(co));
     241           2 :         INTL_METHOD_CHECK_STATUS(co, "intlgregcal_set_gregorian_change: error "
     242             :                 "calling ICU method");
     243             : 
     244           2 :         RETURN_TRUE;
     245             : }
     246             : 
     247           5 : U_CFUNC PHP_FUNCTION(intlgregcal_get_gregorian_change)
     248             : {
     249           5 :         CALENDAR_METHOD_INIT_VARS;
     250             : 
     251           5 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
     252             :                         "O", &object, GregorianCalendar_ce_ptr) == FAILURE) {
     253             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     254           2 :                         "intlgregcal_get_gregorian_change: bad arguments", 0);
     255           2 :                 RETURN_FALSE;
     256             :         }
     257             : 
     258           3 :         CALENDAR_METHOD_FETCH_OBJECT;
     259             : 
     260           3 :         RETURN_DOUBLE((double)fetch_greg(co)->getGregorianChange());
     261             : }
     262             : 
     263           9 : U_CFUNC PHP_FUNCTION(intlgregcal_is_leap_year)
     264             : {
     265             :         zend_long year;
     266           9 :         CALENDAR_METHOD_INIT_VARS;
     267             : 
     268           9 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
     269             :                         "Ol", &object, GregorianCalendar_ce_ptr, &year) == FAILURE) {
     270             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     271           5 :                         "intlgregcal_is_leap_year: bad arguments", 0);
     272           5 :                 RETURN_FALSE;
     273             :         }
     274             : 
     275           4 :         if (year < INT32_MIN || year > INT32_MAX) {
     276             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     277           0 :                         "intlgregcal_is_leap_year: year out of bounds", 0);
     278           0 :                 RETURN_FALSE;
     279             :         }
     280             : 
     281           4 :         CALENDAR_METHOD_FETCH_OBJECT;
     282             : 
     283           4 :         RETURN_BOOL((int)fetch_greg(co)->isLeapYear((int32_t)year));
     284             : }

Generated by: LCOV version 1.10

Generated at Fri, 31 Jul 2015 08:58:49 +0000 (22 minutes ago)

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