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 #include "../intl_cppshims.h"
18 
19 #include <unicode/calendar.h>
20 #include <unicode/gregocal.h>
21 
22 #include "dateformat_helpers.h"
23 
24 extern "C" {
25 #include "../php_intl.h"
26 #include <Zend/zend_operators.h>
27 #define USE_CALENDAR_POINTER 1
28 #include "../calendar/calendar_class.h"
29 }
30 
datefmt_process_calendar_arg(zval * calendar_zv,Locale const & locale,const char * func_name,intl_error * err,Calendar * & cal,long & cal_int_type,bool & calendar_owned TSRMLS_DC)31 int datefmt_process_calendar_arg(zval* calendar_zv,
32 								 Locale const& locale,
33 								 const char *func_name,
34 								 intl_error *err,
35 								 Calendar*& cal,
36 								 long& cal_int_type,
37 								 bool& calendar_owned TSRMLS_DC)
38 {
39 	char *msg;
40 	UErrorCode status = UErrorCode();
41 
42 	if (calendar_zv == NULL || Z_TYPE_P(calendar_zv) == IS_NULL) {
43 
44 		// default requested
45 		cal = new GregorianCalendar(locale, status);
46 		calendar_owned = true;
47 
48 		cal_int_type = UCAL_GREGORIAN;
49 
50 	} else if (Z_TYPE_P(calendar_zv) == IS_LONG) {
51 
52 		long v = Z_LVAL_P(calendar_zv);
53 		if (v != (long)UCAL_TRADITIONAL && v != (long)UCAL_GREGORIAN) {
54 			spprintf(&msg, 0, "%s: invalid value for calendar type; it must be "
55 					"one of IntlDateFormatter::TRADITIONAL (locale's default "
56 					"calendar) or IntlDateFormatter::GREGORIAN. "
57 					"Alternatively, it can be an IntlCalendar object",
58 					func_name);
59 			intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
60 			efree(msg);
61 			return FAILURE;
62 		} else if (v == (long)UCAL_TRADITIONAL) {
63 			cal = Calendar::createInstance(locale, status);
64 		} else { //UCAL_GREGORIAN
65 			cal = new GregorianCalendar(locale, status);
66 		}
67 		calendar_owned = true;
68 
69 		cal_int_type = Z_LVAL_P(calendar_zv);
70 
71 	} else if (Z_TYPE_P(calendar_zv) == IS_OBJECT &&
72 			instanceof_function_ex(Z_OBJCE_P(calendar_zv),
73 			Calendar_ce_ptr, 0 TSRMLS_CC)) {
74 
75 		cal = calendar_fetch_native_calendar(calendar_zv TSRMLS_CC);
76 		if (cal == NULL) {
77 			spprintf(&msg, 0, "%s: Found unconstructed IntlCalendar object",
78 					func_name);
79 			intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
80 			efree(msg);
81 			return FAILURE;
82 		}
83 		calendar_owned = false;
84 
85 		cal_int_type = -1;
86 
87 	} else {
88 		spprintf(&msg, 0, "%s: Invalid calendar argument; should be an integer "
89 				"or an IntlCalendar instance", func_name);
90 		intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
91 		efree(msg);
92 		return FAILURE;
93 	}
94 
95 	if (cal == NULL && !U_FAILURE(status)) {
96 		status = U_MEMORY_ALLOCATION_ERROR;
97 	}
98 	if (U_FAILURE(status)) {
99 		spprintf(&msg, 0, "%s: Failure instantiating calendar", func_name);
100 		intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
101 		efree(msg);
102 		return FAILURE;
103 	}
104 
105 	return SUCCESS;
106 }
107