1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* League.Csv (https://csv.thephpleague.com) |
5
|
|
|
* |
6
|
|
|
* (c) Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace League\Csv; |
15
|
|
|
|
16
|
|
|
use Traversable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Converts tabular data into a SQL string. |
20
|
|
|
*/ |
21
|
|
|
class MySQLConverter |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $table = 'csv_data'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $database = 'csv_data'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* MySQLConverter constructor. |
35
|
|
|
* @param string $database |
36
|
|
|
* @param string $table |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string $database = '', string $table = '') |
39
|
|
|
{ |
40
|
|
|
if (! empty($table)) { |
41
|
|
|
$this->table = $table; |
42
|
|
|
} |
43
|
|
|
if (! empty($database)) { |
44
|
|
|
$this->database = $database; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Traversable $records |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function convert(Traversable $records): string |
53
|
|
|
{ |
54
|
|
|
$sql = $this->generateUseStatement(); |
55
|
|
|
$sql .= $this->generateDropStatement(); |
56
|
|
|
$sql .= $this->generateCreateStatement($records); |
57
|
|
|
$sql .= $this->generateInsertStatement($records); |
58
|
|
|
|
59
|
|
|
return $sql; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
protected function generateUseStatement(): string |
66
|
|
|
{ |
67
|
|
|
return 'USE `'.$this->database.'`;'.PHP_EOL; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
protected function generateDropStatement(): string |
74
|
|
|
{ |
75
|
|
|
return 'DROP TABLE IF EXISTS '.$this->getTableStr().';'.PHP_EOL; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Traversable $records |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function generateCreateStatement(Traversable $records): string |
83
|
|
|
{ |
84
|
|
|
$fields = $this->prepareColumns($records); |
85
|
|
|
return 'CREATE TABLE '.$this->getTableStr().' ('.PHP_EOL.$fields.PHP_EOL.') ENGINE=innodb;'.PHP_EOL; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Traversable $records |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function generateInsertStatement(Traversable $records): string |
93
|
|
|
{ |
94
|
|
|
$records = $this->prepareRecords($records); |
95
|
|
|
return $records; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function getTableStr(): string |
102
|
|
|
{ |
103
|
|
|
return '`'.$this->database.'`.`'.$this->table.'`'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Traversable $records |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function prepareRecords(Traversable $records): string |
111
|
|
|
{ |
112
|
|
|
$values = []; |
113
|
|
|
foreach ($records as $row) { |
114
|
|
|
$fields = []; |
115
|
|
|
foreach ($row as $data) { |
116
|
|
|
$data = trim($data); |
117
|
|
|
if (empty($data)) { |
118
|
|
|
$fields[] = 'NULL'; |
119
|
|
|
} else { |
120
|
|
|
$fields[] = "'".trim($data)."'"; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
$fields = 'INSERT INTO '.$this->getTableStr().' VALUES ('.implode(',', $fields).');'; |
124
|
|
|
$values[] = $fields; |
125
|
|
|
} |
126
|
|
|
$values = implode(PHP_EOL, $values); |
127
|
|
|
return $values; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Traversable $records |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function prepareColumns(Traversable $records): string |
136
|
|
|
{ |
137
|
|
|
$records->rewind(); |
|
|
|
|
138
|
|
|
$sample_data = $records->current(); |
|
|
|
|
139
|
|
|
$columns = array_keys($sample_data); |
140
|
|
|
$fields = []; |
141
|
|
|
foreach ($columns as $column) { |
142
|
|
|
$column_name = strtolower(iconv('UTF-8', 'ASCII//TRANSLIT', $column)); |
143
|
|
|
$column_name = preg_replace('/[ ]+/', ' ', $column_name); |
144
|
|
|
$column_name = str_replace(' ', '_', $column_name); |
145
|
|
|
$data = trim($sample_data[$column]); |
146
|
|
|
|
147
|
|
|
$is_int = filter_var($data, FILTER_VALIDATE_INT); |
148
|
|
|
if ($is_int !== false) { |
149
|
|
|
if ($is_int < 2147483647) { |
150
|
|
|
$fields[] = '`'.$column_name.'` INT NULL'; |
151
|
|
|
} else { |
152
|
|
|
$fields[] = '`'.$column_name.'` BIGINT NULL'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
continue; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$is_float = filter_var($data, FILTER_VALIDATE_FLOAT); |
159
|
|
|
if ($is_float !== false) { |
160
|
|
|
if ($is_float < 2147483647) { |
161
|
|
|
$fields[] = '`'.$column_name.'` FLOAT(10,24) NULL'; |
162
|
|
|
} else { |
163
|
|
|
$fields[] = '`'.$column_name.'` DOUBLE(16,53) NULL'; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
continue; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (strlen($data) < 255) { |
170
|
|
|
$fields[] = '`'.$column_name.'` VARCHAR(255) NULL'; |
171
|
|
|
} else { |
172
|
|
|
$fields[] = '`'.$column_name.'` LONGTEXT NULL'; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$fields = implode(','.PHP_EOL, $fields); |
177
|
|
|
return $fields; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: