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 109 82.6 %
Date: 2014-08-04 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:05 +0000 (41 days ago)

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