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

Generated by: LCOV version 1.10

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

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