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: 2015-03-28 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);
      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);
      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);
      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),
      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);
      76           0 :                         Z_OBJ_P(return_value) = NULL;
      77           0 :                         return;
      78             :                 }
      79             :         }
      80          51 :         if (variant > 2 && zend_parse_parameters(ZEND_NUM_ARGS(),
      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);
      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");
      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());
     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);
     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);
     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);
     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();
     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);
     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);
     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);
     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));
     201           2 :                 ZEND_CTOR_MAKE_NULL();
     202             :         }
     203          51 : }
     204             : 
     205           7 : U_CFUNC PHP_FUNCTION(intlgregcal_set_gregorian_change)
     206             : {
     207             :         double date;
     208           7 :         CALENDAR_METHOD_INIT_VARS;
     209             : 
     210           7 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
     211             :                         "Od", &object, GregorianCalendar_ce_ptr, &date) == FAILURE) {
     212             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     213           5 :                         "intlgregcal_set_gregorian_change: bad arguments", 0);
     214           5 :                 RETURN_FALSE;
     215             :         }
     216             : 
     217           2 :         CALENDAR_METHOD_FETCH_OBJECT;
     218             : 
     219           2 :         fetch_greg(co)->setGregorianChange(date, CALENDAR_ERROR_CODE(co));
     220           2 :         INTL_METHOD_CHECK_STATUS(co, "intlgregcal_set_gregorian_change: error "
     221             :                 "calling ICU method");
     222             : 
     223           2 :         RETURN_TRUE;
     224             : }
     225             : 
     226           6 : U_CFUNC PHP_FUNCTION(intlgregcal_get_gregorian_change)
     227             : {
     228           6 :         CALENDAR_METHOD_INIT_VARS;
     229             : 
     230           6 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
     231             :                         "O", &object, GregorianCalendar_ce_ptr) == FAILURE) {
     232             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     233           3 :                         "intlgregcal_get_gregorian_change: bad arguments", 0);
     234           3 :                 RETURN_FALSE;
     235             :         }
     236             : 
     237           3 :         CALENDAR_METHOD_FETCH_OBJECT;
     238             : 
     239           3 :         RETURN_DOUBLE((double)fetch_greg(co)->getGregorianChange());
     240             : }
     241             : 
     242          10 : U_CFUNC PHP_FUNCTION(intlgregcal_is_leap_year)
     243             : {
     244             :         zend_long year;
     245          10 :         CALENDAR_METHOD_INIT_VARS;
     246             : 
     247          10 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
     248             :                         "Ol", &object, GregorianCalendar_ce_ptr, &year) == FAILURE) {
     249             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     250           6 :                         "intlgregcal_is_leap_year: bad arguments", 0);
     251           6 :                 RETURN_FALSE;
     252             :         }
     253             : 
     254           4 :         if (year < INT32_MIN || year > INT32_MAX) {
     255             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     256           0 :                         "intlgregcal_is_leap_year: year out of bounds", 0);
     257           0 :                 RETURN_FALSE;
     258             :         }
     259             : 
     260           4 :         CALENDAR_METHOD_FETCH_OBJECT;
     261             : 
     262           4 :         RETURN_BOOL((int)fetch_greg(co)->isLeapYear((int32_t)year));
     263             : }

Generated by: LCOV version 1.10

Generated at Sun, 29 Mar 2015 03:45:50 +0000 (43 hours ago)

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