forked from Automattic/vip-go-compatibility-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv-data.php
126 lines (103 loc) · 1.72 KB
/
csv-data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/*
* Get CSV data. First line of file
* should be columns.
*/
function vipgocs_csv_parse_data(
$zendesk_csv_file_path
) {
vipgoci_log(
'Parsing CSV data file',
array(
'zendesk_csv_file_path' => $zendesk_csv_file_path,
)
);
ini_set( 'auto_detect_line_endings', false );
$res = array();
$f = fopen(
$zendesk_csv_file_path,
'r'
);
if ( ! $f ) {
vipgoci_sysexit(
'Unable to parse CSV file',
array(
'zendesk_csv_file_path' => $zendesk_csv_file_path,
)
);
}
$first = fgetcsv(
$f,
0,
','
);
if ( empty( $first ) ) {
vipgoci_sysexit(
'CSV file seems empty? First line should be columns.'
);
}
while ( $item = fgetcsv(
$f,
0,
','
) ) {
if ( count( $item ) <= 0 ) {
continue;
}
$i = 0;
$item_arr = array();
foreach( array_values( $first ) as $key ) {
if ( ! isset( $item[ $i ] ) ) {
continue;
}
if ( empty( $item[ $i ] ) ) {
continue;
}
/*
* Assign item, trim any whitespace characters.
*/
$item_arr[ $key ] = trim( $item[ $i++ ] );
}
if ( ! empty( $item_arr ) ) {
$res[] = $item_arr;
}
}
fclose(
$f
);
ini_set( 'auto_detect_line_endings', false );
return $res;
}
/*
* Try to match email address with repository.
*/
function vipgocs_csv_get_email_for_repo(
$csv_data_arr,
$repo_owner,
$repo_name
) {
$repo_owner = strtolower(
$repo_owner
);
$repo_name = strtolower(
$repo_name
);
foreach(
$csv_data_arr as $csv_item
) {
if (
( ! isset( $csv_item['source_repo'] ) ) ||
( ! isset( $csv_item['client_email'] ) )
) {
continue;
}
if (
$csv_item['source_repo'] !==
$repo_owner .'/' . $repo_name
) {
continue;
}
return $csv_item['client_email'];
}
return null;
}