|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the League.csv library |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT |
|
6
|
|
|
* @link https://github.com/thephpleague/csv/ |
|
7
|
|
|
* @version 9.0.0 |
|
8
|
|
|
* @package League.csv |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace League\Csv\Config; |
|
16
|
|
|
|
|
17
|
|
|
use League\Csv\Exception; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* An abstract class to enable basic CSV manipulation |
|
21
|
|
|
* |
|
22
|
|
|
* @package League.csv |
|
23
|
|
|
* @since 9.0.0 |
|
24
|
|
|
* @internal |
|
25
|
|
|
*/ |
|
26
|
|
|
trait ValidatorTrait |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Filter Csv control character |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $char Csv control character |
|
32
|
|
|
* @param string $type Csv control character type |
|
33
|
|
|
* |
|
34
|
|
|
* @throws Exception If the Csv control character is not one character only. |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
208 |
|
protected function filterControl(string $char, string $type) |
|
39
|
|
|
{ |
|
40
|
208 |
|
if (1 == strlen($char)) { |
|
41
|
208 |
|
return $char; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
6 |
|
throw new Exception(sprintf('The %s must be a single character', $type)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Validate an integer |
|
49
|
|
|
* |
|
50
|
|
|
* @param int $value |
|
51
|
|
|
* @param int $min_value |
|
52
|
|
|
* @param string $error_message |
|
53
|
|
|
* |
|
54
|
|
|
* @throws Exception If the value is invalid |
|
55
|
|
|
* |
|
56
|
|
|
* @return int |
|
57
|
|
|
*/ |
|
58
|
78 |
|
protected function filterInteger(int $value, int $min_value, string $error_message): int |
|
59
|
|
|
{ |
|
60
|
78 |
|
if ($value < $min_value) { |
|
61
|
10 |
|
throw new Exception($error_message); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
70 |
|
return $value; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Validates the array to be used by the fetchAssoc method |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $keys |
|
71
|
|
|
* |
|
72
|
|
|
* @throws Exception If the submitted array fails the assertion |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
32 |
|
protected function filterHeader(array $keys): array |
|
77
|
|
|
{ |
|
78
|
32 |
|
if (empty($keys)) { |
|
79
|
2 |
|
return $keys; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
30 |
|
if ($keys !== array_unique(array_filter($keys, [$this, 'isValidKey']))) { |
|
83
|
2 |
|
throw new Exception('Use a flat array with unique string values'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
28 |
|
return $keys; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Returns whether the submitted value can be used as string |
|
91
|
|
|
* |
|
92
|
|
|
* @param mixed $value |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
30 |
|
protected function isValidKey(string $value) |
|
97
|
|
|
{ |
|
98
|
30 |
|
return is_scalar($value) || (is_object($value) && method_exists($value, '__toString')); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Strip the BOM sequence from a record |
|
103
|
|
|
* |
|
104
|
|
|
* @param string[] $row |
|
105
|
|
|
* @param int $bom_length |
|
106
|
|
|
* @param string $enclosure |
|
107
|
|
|
* |
|
108
|
|
|
* @return string[] |
|
109
|
|
|
*/ |
|
110
|
26 |
|
protected function removeBOM(array $row, int $bom_length, string $enclosure): array |
|
111
|
|
|
{ |
|
112
|
26 |
|
if (0 == $bom_length) { |
|
113
|
8 |
|
return $row; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
18 |
|
$row[0] = mb_substr($row[0], $bom_length); |
|
117
|
18 |
|
if ($enclosure == mb_substr($row[0], 0, 1) && $enclosure == mb_substr($row[0], -1, 1)) { |
|
118
|
8 |
|
$row[0] = mb_substr($row[0], 1, -1); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
18 |
|
return $row; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|