Skip to content

Commit 9199a33

Browse files
committed
Feat: setType in reader
1 parent fcca5ed commit 9199a33

14 files changed

+191
-15
lines changed

include/excel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define V_XLS_FIL "fileName"
1818
#define V_XLS_COF "config"
1919
#define V_XLS_PAT "path"
20+
#define V_XLS_TYPE "read_row_type"
2021

2122
#define V_XLS_CONST_READ_TYPE_INT "TYPE_INT"
2223
#define V_XLS_CONST_READ_TYPE_DOUBLE "TYPE_DOUBLE"

include/read.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
#define READ_ROW 0x01
1818

1919
int sheet_read_row(xlsxioreadersheet sheet_t);
20+
int is_number(char *value);
2021
xlsxioreader file_open(const char *directory, const char *file_name);
21-
void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_result_t);
22+
void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_type_t, zval *zv_result_t);
2223
xlsxioreadersheet sheet_open(xlsxioreader file_t, const zend_string *zs_sheet_name_t, const zend_long zl_flag);
2324
unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, zval *zv_type, unsigned int flag);
2425
unsigned int load_sheet_current_row_data_callback(zend_string *zs_sheet_name_t, xlsxioreader file_t, void *callback_data);

kernel/excel.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ ZEND_BEGIN_ARG_INFO_EX(xls_open_sheet_arginfo, 0, 0, 1)
163163
ZEND_ARG_INFO(0, zs_sheet_name)
164164
ZEND_END_ARG_INFO()
165165

166+
ZEND_BEGIN_ARG_INFO_EX(xls_set_type_arginfo, 0, 0, 1)
167+
ZEND_ARG_INFO(0, zv_type_t)
168+
ZEND_END_ARG_INFO()
169+
166170
ZEND_BEGIN_ARG_INFO_EX(xls_next_cell_callback_arginfo, 0, 0, 2)
167171
ZEND_ARG_INFO(0, fci)
168172
ZEND_ARG_INFO(0, sheet_name)
@@ -754,6 +758,22 @@ PHP_METHOD(vtiful_xls, openSheet)
754758
}
755759
/* }}} */
756760

761+
/** {{{ \Vtiful\Kernel\xls::setType(array $rowType)
762+
*/
763+
PHP_METHOD(vtiful_xls, setType)
764+
{
765+
zval *zv_type_t = NULL;
766+
767+
ZEND_PARSE_PARAMETERS_START(1, 1)
768+
Z_PARAM_ARRAY(zv_type_t)
769+
ZEND_PARSE_PARAMETERS_END();
770+
771+
ZVAL_COPY(return_value, getThis());
772+
773+
add_property_zval_ex(getThis(), ZEND_STRL(V_XLS_TYPE), zv_type_t);
774+
}
775+
/* }}} */
776+
757777
/** {{{ \Vtiful\Kernel\xls::getSheetData()
758778
*/
759779
PHP_METHOD(vtiful_xls, getSheetData)
@@ -764,7 +784,15 @@ PHP_METHOD(vtiful_xls, getSheetData)
764784
RETURN_FALSE;
765785
}
766786

767-
load_sheet_all_data(obj->read_ptr.sheet_t, return_value);
787+
zval *zv_type = zend_read_property(vtiful_xls_ce, getThis(), ZEND_STRL(V_XLS_TYPE), 0, NULL);
788+
789+
if (zv_type != NULL && Z_TYPE_P(zv_type) == IS_ARRAY) {
790+
load_sheet_all_data(obj->read_ptr.sheet_t, zv_type, return_value);
791+
792+
return;
793+
}
794+
795+
load_sheet_all_data(obj->read_ptr.sheet_t, NULL, return_value);
768796
}
769797
/* }}} */
770798

@@ -785,6 +813,10 @@ PHP_METHOD(vtiful_xls, nextRow)
785813
RETURN_FALSE;
786814
}
787815

816+
if (zv_type == NULL) {
817+
zv_type = zend_read_property(vtiful_xls_ce, getThis(), ZEND_STRL(V_XLS_TYPE), 0, NULL);
818+
}
819+
788820
load_sheet_current_row_data(obj->read_ptr.sheet_t, return_value, zv_type, READ_ROW);
789821
}
790822
/* }}} */
@@ -846,6 +878,7 @@ zend_function_entry xls_methods[] = {
846878
#ifdef ENABLE_READER
847879
PHP_ME(vtiful_xls, openFile, xls_open_file_arginfo, ZEND_ACC_PUBLIC)
848880
PHP_ME(vtiful_xls, openSheet, xls_open_sheet_arginfo, ZEND_ACC_PUBLIC)
881+
PHP_ME(vtiful_xls, setType, xls_set_type_arginfo, ZEND_ACC_PUBLIC)
849882
PHP_ME(vtiful_xls, getSheetData, NULL, ZEND_ACC_PUBLIC)
850883
PHP_ME(vtiful_xls, nextRow, NULL, ZEND_ACC_PUBLIC)
851884
PHP_ME(vtiful_xls, nextCellCallback, xls_next_cell_callback_arginfo, ZEND_ACC_PUBLIC)
@@ -868,8 +901,9 @@ VTIFUL_STARTUP_FUNCTION(excel) {
868901
vtiful_xls_handlers.offset = XtOffsetOf(xls_object, zo);
869902
vtiful_xls_handlers.free_obj = vtiful_xls_objects_free;
870903

871-
REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_COF, ZEND_ACC_PRIVATE);
872-
REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_FIL, ZEND_ACC_PRIVATE);
904+
REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_COF, ZEND_ACC_PRIVATE);
905+
REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_FIL, ZEND_ACC_PRIVATE);
906+
REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_TYPE, ZEND_ACC_PRIVATE);
873907

874908
#ifdef ENABLE_READER
875909
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_SKIP_NONE, XLSXIOREAD_SKIP_NONE);

kernel/read.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ xlsxioreadersheet sheet_open(xlsxioreader file_t, const zend_string *zs_sheet_na
4141
}
4242
/* }}} */
4343

44+
/* {{{ */
45+
int is_number(char *value)
46+
{
47+
if (strspn(value, ".0123456789") == strlen(value)) {
48+
return XLSWRITER_TRUE;
49+
}
50+
51+
return XLSWRITER_FALSE;
52+
}
53+
/* }}} */
54+
4455
/* {{{ */
4556
int sheet_read_row(xlsxioreadersheet sheet_t)
4657
{
@@ -83,29 +94,43 @@ unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_res
8394
}
8495

8596
if (_type & READ_TYPE_DATETIME) {
97+
if (!is_number(_string_value)) {
98+
goto STRING;
99+
}
100+
86101
double value = strtod(_string_value, NULL);
87102

88103
if (value != 0) {
89104
value = (value - 25569) * 86400;
90105
}
91106

92-
add_next_index_double(zv_result_t, value);
107+
add_next_index_long(zv_result_t, (zend_long)(value + 0.5));
93108
continue;
94109
}
95110

96111
if (_type & READ_TYPE_DOUBLE) {
112+
if (!is_number(_string_value)) {
113+
goto STRING;
114+
}
115+
97116
add_next_index_double(zv_result_t, strtod(_string_value, NULL));
98117
continue;
99118
}
100119

101120
if (_type & READ_TYPE_INT) {
121+
if (!is_number(_string_value)) {
122+
goto STRING;
123+
}
124+
102125
zend_long _long_value;
103126

104127
sscanf(_string_value, "%" PRIi64, &_long_value);
105128
add_next_index_long(zv_result_t, _long_value);
106129
continue;
107130
}
108131

132+
STRING:
133+
109134
add_next_index_stringl(zv_result_t, _string_value, strlen(_string_value));
110135
}
111136

@@ -185,7 +210,7 @@ unsigned int load_sheet_current_row_data_callback(zend_string *zs_sheet_name_t,
185210
/* }}} */
186211

187212
/* {{{ */
188-
void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_result_t)
213+
void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_type_t, zval *zv_result_t)
189214
{
190215
if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
191216
array_init(zv_result_t);
@@ -196,7 +221,7 @@ void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_result_t)
196221
zval _zv_tmp_row;
197222
ZVAL_NULL(&_zv_tmp_row);
198223

199-
load_sheet_current_row_data(sheet_t, &_zv_tmp_row, NULL, READ_SKIP_ROW);
224+
load_sheet_current_row_data(sheet_t, &_zv_tmp_row, zv_type_t, READ_SKIP_ROW);
200225
add_next_index_zval(zv_result_t, &_zv_tmp_row);
201226
}
202227
}

tests/002.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ $excel = new \Vtiful\Kernel\Excel($config);
99
var_dump($excel);
1010
?>
1111
--EXPECT--
12-
object(Vtiful\Kernel\Excel)#1 (2) {
12+
object(Vtiful\Kernel\Excel)#1 (3) {
1313
["config":"Vtiful\Kernel\Excel":private]=>
1414
array(1) {
1515
["path"]=>
1616
string(7) "./tests"
1717
}
1818
["fileName":"Vtiful\Kernel\Excel":private]=>
1919
NULL
20+
["read_row_type":"Vtiful\Kernel\Excel":private]=>
21+
NULL
2022
}
2123

tests/003.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ $fileFd = $excel->fileName('tutorial01.xlsx');
1010
var_dump($fileFd);
1111
?>
1212
--EXPECT--
13-
object(Vtiful\Kernel\Excel)#1 (2) {
13+
object(Vtiful\Kernel\Excel)#1 (3) {
1414
["config":"Vtiful\Kernel\Excel":private]=>
1515
array(1) {
1616
["path"]=>
1717
string(7) "./tests"
1818
}
1919
["fileName":"Vtiful\Kernel\Excel":private]=>
2020
string(23) "./tests/tutorial01.xlsx"
21+
["read_row_type":"Vtiful\Kernel\Excel":private]=>
22+
NULL
2123
}

tests/004.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ $setHeader = $fileFd->header(['Item', 'Cost']);
1111
var_dump($setHeader);
1212
?>
1313
--EXPECT--
14-
object(Vtiful\Kernel\Excel)#1 (2) {
14+
object(Vtiful\Kernel\Excel)#1 (3) {
1515
["config":"Vtiful\Kernel\Excel":private]=>
1616
array(1) {
1717
["path"]=>
1818
string(7) "./tests"
1919
}
2020
["fileName":"Vtiful\Kernel\Excel":private]=>
2121
string(23) "./tests/tutorial01.xlsx"
22+
["read_row_type":"Vtiful\Kernel\Excel":private]=>
23+
NULL
2224
}

tests/open_xlsx_file.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ var_dump($data);
2222
@unlink(__DIR__ . '/tutorial.xlsx');
2323
?>
2424
--EXPECT--
25-
object(Vtiful\Kernel\Excel)#1 (2) {
25+
object(Vtiful\Kernel\Excel)#1 (3) {
2626
["config":"Vtiful\Kernel\Excel":private]=>
2727
array(1) {
2828
["path"]=>
2929
string(7) "./tests"
3030
}
3131
["fileName":"Vtiful\Kernel\Excel":private]=>
3232
string(21) "./tests/tutorial.xlsx"
33+
["read_row_type":"Vtiful\Kernel\Excel":private]=>
34+
NULL
3335
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/include/skipif.inc';
6+
skip_disable_reader();
7+
?>
8+
--FILE--
9+
<?php
10+
$config = ['path' => './tests'];
11+
$excel = new \Vtiful\Kernel\Excel($config);
12+
$filePath = $excel->fileName('tutorial.xlsx')
13+
->header(['Name', 'Age', 'Date'])
14+
->data([
15+
['Viest', 24]
16+
])
17+
->insertDate(1, 2, 1568877706)
18+
->output();
19+
20+
$data = $excel->openFile('tutorial.xlsx')
21+
->openSheet()
22+
->setType([
23+
\Vtiful\Kernel\Excel::TYPE_STRING,
24+
\Vtiful\Kernel\Excel::TYPE_STRING,
25+
\Vtiful\Kernel\Excel::TYPE_TIMESTAMP,
26+
])
27+
->getSheetData();
28+
29+
var_dump($data);
30+
?>
31+
--CLEAN--
32+
<?php
33+
@unlink(__DIR__ . '/tutorial.xlsx');
34+
?>
35+
--EXPECT--
36+
array(2) {
37+
[0]=>
38+
array(3) {
39+
[0]=>
40+
string(4) "Name"
41+
[1]=>
42+
string(3) "Age"
43+
[2]=>
44+
string(4) "Date"
45+
}
46+
[1]=>
47+
array(3) {
48+
[0]=>
49+
string(5) "Viest"
50+
[1]=>
51+
string(2) "24"
52+
[2]=>
53+
int(1568877706)
54+
}
55+
}

tests/open_xlsx_next_row_with_data_type_date.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ array(1) {
4040
}
4141
array(1) {
4242
[0]=>
43-
float(1568389354)
43+
int(1568389354)
4444
}
4545
NULL
4646
NULL

0 commit comments

Comments
 (0)