Skip to content

Commit 8e9c66a

Browse files
authored
Merge pull request #352 from viest/dev
Feat: validation
2 parents 1a83b42 + 28605a6 commit 8e9c66a

14 files changed

+1048
-16
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ if test "$PHP_XLSWRITER" != "no"; then
2121
kernel/format.c \
2222
kernel/chart.c \
2323
kernel/help.c \
24+
kernel/validation.c \
2425
"
2526

2627
xls_read_sources="

config.w32

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ if (PHP_XLSWRITER != "no") {
2828
read.c \
2929
csv.c \
3030
help.c \
31+
validation.c \
3132
", "xlswriter");
3233

3334
ADD_SOURCES(configure_module_dirname + "\\library\\libxlsxwriter\\third_party\\minizip", "\

include/validation.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| XlsWriter Extension |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 2017-2018 The Viest |
6+
+----------------------------------------------------------------------+
7+
| http://www.viest.me |
8+
+----------------------------------------------------------------------+
9+
| Author: viest <[email protected]> |
10+
+----------------------------------------------------------------------+
11+
*/
12+
13+
#ifndef PHP_EXT_XLS_WRITER_VALIDATION_H
14+
#define PHP_EXT_XLS_WRITER_VALIDATION_H
15+
16+
extern zend_class_entry *vtiful_validation_ce;
17+
18+
VTIFUL_STARTUP_FUNCTION(validation);
19+
20+
#endif // PHP_EXT_XLS_WRITER_VALIDATION_H

include/xlswriter.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#endif
1919

2020
#include <php.h>
21+
#include "ext/date/php_date.h"
2122

2223
#include "zend_smart_str.h"
2324
#include "zend_exceptions.h"
@@ -31,6 +32,7 @@
3132

3233
#include "php_xlswriter.h"
3334
#include "excel.h"
35+
#include "validation.h"
3436
#include "exception.h"
3537
#include "format.h"
3638
#include "chart.h"
@@ -84,6 +86,10 @@ typedef struct {
8486
lxw_format *format;
8587
} xls_resource_format_t;
8688

89+
typedef struct {
90+
lxw_data_validation *validation;
91+
} xls_resource_validation_t;
92+
8793
typedef struct {
8894
lxw_chart *chart;
8995
lxw_chart_series *series;
@@ -107,6 +113,11 @@ typedef struct _vtiful_chart_object {
107113
zend_object zo;
108114
} chart_object;
109115

116+
typedef struct _vtiful_validation_object {
117+
xls_resource_validation_t ptr;
118+
zend_object zo;
119+
} validation_object;
120+
110121
static inline xls_object *php_vtiful_xls_fetch_object(zend_object *obj) {
111122
return (xls_object *)((char *)(obj) - XtOffsetOf(xls_object, zo));
112123
}
@@ -119,15 +130,20 @@ static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
119130
return (chart_object *)((char *)(obj) - XtOffsetOf(chart_object, zo));
120131
}
121132

133+
static inline validation_object *php_vtiful_validation_fetch_object(zend_object *obj) {
134+
return (validation_object *)((char *)(obj) - XtOffsetOf(validation_object, zo));
135+
}
136+
122137
#define REGISTER_CLASS_CONST_LONG(class_name, const_name, value) \
123138
zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (zend_long)value);
124139

125140
#define REGISTER_CLASS_PROPERTY_NULL(class_name, property_name, acc) \
126141
zend_declare_property_null(class_name, ZEND_STRL(property_name), acc);
127142

128-
#define Z_XLS_P(zv) php_vtiful_xls_fetch_object(Z_OBJ_P(zv));
129-
#define Z_CHART_P(zv) php_vtiful_chart_fetch_object(Z_OBJ_P(zv));
130-
#define Z_FORMAT_P(zv) php_vtiful_format_fetch_object(Z_OBJ_P(zv));
143+
#define Z_XLS_P(zv) php_vtiful_xls_fetch_object(Z_OBJ_P(zv));
144+
#define Z_CHART_P(zv) php_vtiful_chart_fetch_object(Z_OBJ_P(zv));
145+
#define Z_FORMAT_P(zv) php_vtiful_format_fetch_object(Z_OBJ_P(zv));
146+
#define Z_VALIDATION_P(zv) php_vtiful_validation_fetch_object(Z_OBJ_P(zv));
131147

132148
#define WORKBOOK_NOT_INITIALIZED(xls_object_t) \
133149
do { \
@@ -209,6 +225,7 @@ static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
209225

210226

211227
lxw_format * zval_get_format(zval *handle);
228+
lxw_data_validation * zval_get_validation(zval *resource);
212229
xls_resource_write_t * zval_get_resource(zval *handle);
213230
xls_resource_chart_t * zval_get_chart(zval *resource);
214231

@@ -235,6 +252,7 @@ void printed_direction(xls_resource_write_t *res, unsigned int direction);
235252
void xls_file_path(zend_string *file_name, zval *dir_path, zval *file_path);
236253
void freeze_panes(xls_resource_write_t *res, zend_long row, zend_long column);
237254
void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format);
255+
void validation(xls_resource_write_t *res, zend_string *range, lxw_data_validation *validation);
238256
void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format);
239257
void merge_cells(zend_string *range, zval *value, xls_resource_write_t *res, lxw_format *format);
240258
void comment_writer(zend_string *comment, zend_long row, zend_long columns, xls_resource_write_t *res);
@@ -249,6 +267,7 @@ void url_writer(zend_long row, zend_long columns, xls_resource_write_t *res, zen
249267

250268
lxw_error workbook_file(xls_resource_write_t *self);
251269

270+
lxw_datetime timestamp_to_datetime(zend_long timestamp);
252271
zend_string* str_pick_up(zend_string *left, const char *right, size_t len);
253272

254273
#endif

kernel/common.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,20 @@ void call_object_method(zval *object, const char *function_name, uint32_t param_
6969

7070
zval_ptr_dtor(&z_f_name);
7171
}
72-
/* }}} */
72+
/* }}} */
73+
74+
lxw_datetime timestamp_to_datetime(zend_long timestamp)
75+
{
76+
int yearLocal = php_idate('Y', timestamp, 0);
77+
int monthLocal = php_idate('m', timestamp, 0);
78+
int dayLocal = php_idate('d', timestamp, 0);
79+
int hourLocal = php_idate('H', timestamp, 0);
80+
int minuteLocal = php_idate('i', timestamp, 0);
81+
int secondLocal = php_idate('s', timestamp, 0);
82+
83+
lxw_datetime datetime = {
84+
yearLocal, monthLocal, dayLocal, hourLocal, minuteLocal, secondLocal
85+
};
86+
87+
return datetime;
88+
}

kernel/excel.c

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212

1313
#include "xlswriter.h"
14-
#include "ext/date/php_date.h"
1514

1615
zend_class_entry *vtiful_xls_ce;
1716

@@ -267,6 +266,11 @@ ZEND_BEGIN_ARG_INFO_EX(xls_protection_arginfo, 0, 0, 0)
267266
ZEND_ARG_INFO(0, password)
268267
ZEND_END_ARG_INFO()
269268

269+
ZEND_BEGIN_ARG_INFO_EX(xls_validation_arginfo, 0, 0, 2)
270+
ZEND_ARG_INFO(0, range)
271+
ZEND_ARG_INFO(0, validation_resource)
272+
ZEND_END_ARG_INFO()
273+
270274
ZEND_BEGIN_ARG_INFO_EX(xls_set_printed_portrait_arginfo, 0, 0, 0)
271275
ZEND_END_ARG_INFO()
272276

@@ -640,16 +644,7 @@ PHP_METHOD(vtiful_xls, insertDate)
640644
format = zend_string_init(ZEND_STRL("yyyy-mm-dd hh:mm:ss"), 0);
641645
}
642646

643-
int yearLocal = php_idate('Y', data->value.lval, 0);
644-
int monthLocal = php_idate('m', data->value.lval, 0);
645-
int dayLocal = php_idate('d', data->value.lval, 0);
646-
int hourLocal = php_idate('H', data->value.lval, 0);
647-
int minuteLocal = php_idate('i', data->value.lval, 0);
648-
int secondLocal = php_idate('s', data->value.lval, 0);
649-
650-
lxw_datetime datetime = {
651-
yearLocal, monthLocal, dayLocal, hourLocal, minuteLocal, secondLocal
652-
};
647+
lxw_datetime datetime = timestamp_to_datetime(data->value.lval);
653648

654649
if (format_handle) {
655650
datetime_writer(&datetime, row, column, format, &obj->write_ptr, zval_get_format(format_handle));
@@ -1172,6 +1167,28 @@ PHP_METHOD(vtiful_xls, setCurrentSheetIsFirst)
11721167
}
11731168
/* }}} */
11741169

1170+
/** {{{ \Vtiful\Kernel\Excel::validation()
1171+
*/
1172+
PHP_METHOD(vtiful_xls, validation)
1173+
{
1174+
zend_string *range = NULL;
1175+
zval *validation_handle = NULL;
1176+
1177+
ZEND_PARSE_PARAMETERS_START(2, 2)
1178+
Z_PARAM_STR(range)
1179+
Z_PARAM_RESOURCE(validation_handle)
1180+
ZEND_PARSE_PARAMETERS_END();
1181+
1182+
ZVAL_COPY(return_value, getThis());
1183+
1184+
xls_object *obj = Z_XLS_P(getThis());
1185+
1186+
WORKBOOK_NOT_INITIALIZED(obj);
1187+
1188+
validation(&obj->write_ptr, range, zval_get_validation(validation_handle));
1189+
}
1190+
/* }}} */
1191+
11751192
#ifdef ENABLE_READER
11761193

11771194
/** {{{ \Vtiful\Kernel\Excel::openFile()
@@ -1497,6 +1514,7 @@ zend_function_entry xls_methods[] = {
14971514
PHP_ME(vtiful_xls, freezePanes, xls_freeze_panes_arginfo, ZEND_ACC_PUBLIC)
14981515

14991516
PHP_ME(vtiful_xls, protection, xls_protection_arginfo, ZEND_ACC_PUBLIC)
1517+
PHP_ME(vtiful_xls, validation, xls_validation_arginfo, ZEND_ACC_PUBLIC)
15001518

15011519
PHP_ME(vtiful_xls, zoom, xls_sheet_zoom_arginfo, ZEND_ACC_PUBLIC)
15021520
PHP_ME(vtiful_xls, gridline, xls_sheet_gridline_arginfo, ZEND_ACC_PUBLIC)

kernel/resource.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ lxw_format * zval_get_format(zval *handle)
3838
}
3939
/* }}} */
4040

41+
/* {{{ */
4142
xls_resource_chart_t *zval_get_chart(zval *resource)
4243
{
4344
xls_resource_chart_t *res;
@@ -47,4 +48,18 @@ xls_resource_chart_t *zval_get_chart(zval *resource)
4748
}
4849

4950
return res;
50-
}
51+
}
52+
/* }}} */
53+
54+
/* {{{ */
55+
lxw_data_validation *zval_get_validation(zval *resource)
56+
{
57+
lxw_data_validation *res;
58+
59+
if((res = (lxw_data_validation *)zend_fetch_resource(Z_RES_P(resource), VTIFUL_RESOURCE_NAME, le_xls_writer)) == NULL) {
60+
zend_throw_exception(vtiful_exception_ce, "validation resources resolution fail", 210);
61+
}
62+
63+
return res;
64+
}
65+
/* }}} */

0 commit comments

Comments
 (0)