|
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 8.1.1 |
|
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
|
|
|
namespace League\Csv\Config; |
|
14
|
|
|
|
|
15
|
|
|
use InvalidArgumentException; |
|
16
|
|
|
use League\Csv\AbstractCsv; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* A trait to configure and check CSV file and content |
|
20
|
|
|
* |
|
21
|
|
|
* @package League.csv |
|
22
|
|
|
* @since 6.0.0 |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
trait Validator |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Validate an integer |
|
29
|
|
|
* |
|
30
|
|
|
* @param int $int |
|
31
|
|
|
* @param int $minValue |
|
32
|
|
|
* @param string $errorMessage |
|
33
|
|
|
* |
|
34
|
|
|
* @throws InvalidArgumentException If the value is invalid |
|
35
|
|
|
* |
|
36
|
|
|
* @return int |
|
37
|
|
|
*/ |
|
38
|
|
|
protected static function validateInteger($int, $minValue, $errorMessage) |
|
39
|
|
|
{ |
|
40
|
|
|
if (false === ($int = filter_var($int, FILTER_VALIDATE_INT, ['options' => ['min_range' => $minValue]]))) { |
|
41
|
|
|
throw new InvalidArgumentException($errorMessage); |
|
42
|
|
|
} |
|
43
|
|
|
return $int; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* validate a string |
|
48
|
|
|
* |
|
49
|
|
|
* @param mixed $str the value to evaluate as a string |
|
50
|
|
|
* |
|
51
|
|
|
* @throws InvalidArgumentException if the submitted data can not be converted to string |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected static function validateString($str) |
|
56
|
|
|
{ |
|
57
|
|
|
if (is_string($str) || (is_object($str) && method_exists($str, '__toString'))) { |
|
58
|
|
|
return (string) $str; |
|
59
|
|
|
} |
|
60
|
|
|
throw new InvalidArgumentException('Expected data must be a string or stringable'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Tell whether to use Stream Filter or not to convert the CSV |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function useInternalConverter(AbstractCsv $csv) |
|
69
|
|
|
{ |
|
70
|
|
|
return !('UTF-8' === $csv->getInputEncoding() |
|
71
|
|
|
|| ($csv->isActiveStreamFilter() && STREAM_FILTER_READ === $csv->getStreamFilterMode())); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Convert a CSV record to UTF-8 |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $record |
|
78
|
|
|
* @param string $input_encoding |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function convertRecord(array $record, $input_encoding) |
|
83
|
|
|
{ |
|
84
|
|
|
return array_map(function ($value) use ($input_encoding) { |
|
85
|
|
|
return iconv($input_encoding, 'UTF-8//TRANSLIT', $value); |
|
86
|
|
|
}, $record); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Strip the BOM character from the record |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $record |
|
93
|
|
|
* @param string $bom |
|
94
|
|
|
* @param string $enclosure |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function stripBOM(array $record, $bom, $enclosure) |
|
99
|
|
|
{ |
|
100
|
|
|
$bom_length = mb_strlen($bom); |
|
101
|
|
|
if (0 == $bom_length) { |
|
102
|
|
|
return $record; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$record[0] = mb_substr($record[0], $bom_length); |
|
106
|
|
|
if ($record[0][0] === $enclosure && mb_substr($record[0], -1, 1) === $enclosure) { |
|
107
|
|
|
$record[0] = mb_substr($record[0], 1, -1); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $record; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|