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; |
16
|
|
|
|
17
|
|
|
use DOMAttr; |
18
|
|
|
use DOMElement; |
19
|
|
|
use DOMException; |
20
|
|
|
use League\Csv\Exception\InvalidArgumentException; |
21
|
|
|
use League\Csv\Exception\OutOfRangeException; |
22
|
|
|
use Traversable; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* A trait to validate properties |
26
|
|
|
* |
27
|
|
|
* @package League.csv |
28
|
|
|
* @since 9.0.0 |
29
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
30
|
|
|
* @internal Use to validate incoming data |
31
|
|
|
*/ |
32
|
|
|
trait ValidatorTrait |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Filter Csv control character |
36
|
|
|
* |
37
|
|
|
* @param string $char Csv control character |
38
|
|
|
* @param string $type Csv control character type |
39
|
|
|
* @param string $method Method call |
40
|
|
|
* |
41
|
|
|
* @throws InvalidArgumentException If the Csv control character is not one character only. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
52 |
|
protected function filterControl(string $char, string $type, string $method) |
46
|
|
|
{ |
47
|
52 |
|
if (1 == strlen($char)) { |
48
|
50 |
|
return $char; |
49
|
|
|
} |
50
|
|
|
|
51
|
8 |
|
throw new InvalidArgumentException(sprintf('%s: %s must be a single character', $method, $type)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Validate an integer |
56
|
|
|
* |
57
|
|
|
* @param int $value |
58
|
|
|
* @param int $min_value |
59
|
|
|
* @param string $error_message |
60
|
|
|
* |
61
|
|
|
* @throws InvalidArgumentException If the value is invalid |
62
|
|
|
* |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
94 |
|
protected function filterInteger(int $value, int $min_value, string $error_message): int |
66
|
|
|
{ |
67
|
94 |
|
if ($value >= $min_value) { |
68
|
86 |
|
return $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
12 |
|
throw new OutOfRangeException($error_message); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Validates the array to be used by the fetchAssoc method |
76
|
|
|
* |
77
|
|
|
* @param array $keys |
78
|
|
|
* |
79
|
|
|
* @throws InvalidArgumentException If the submitted array fails the assertion |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
44 |
|
protected function filterColumnNames(array $keys): array |
84
|
|
|
{ |
85
|
44 |
|
if (empty($keys) || $keys === array_unique(array_filter($keys, 'is_string'))) { |
86
|
42 |
|
return $keys; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
throw new InvalidArgumentException('Use a flat array with unique string values'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Validate the argument given is an iterable |
94
|
|
|
* |
95
|
|
|
* @param array|Traversable $iterable |
96
|
|
|
* @param string $method |
97
|
|
|
* |
98
|
|
|
* @throws InvalidArgumentException If the submitted value is not iterable |
99
|
|
|
* |
100
|
|
|
* @return array|Traversable |
101
|
|
|
*/ |
102
|
14 |
|
protected function filterIterable($iterable, string $method) |
103
|
|
|
{ |
104
|
14 |
|
if (is_array($iterable) || $iterable instanceof Traversable) { |
105
|
12 |
|
return $iterable; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be iterable, %s given', $method, is_object($iterable) ? get_class($iterable) : gettype($iterable))); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Filter XML element name |
113
|
|
|
* |
114
|
|
|
* @param string $value Element name |
115
|
|
|
* |
116
|
|
|
* @throws DOMException If the Element name is invalid |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
6 |
|
protected function filterNodeName(string $value): string |
121
|
|
|
{ |
122
|
6 |
|
new DOMElement($value); |
123
|
|
|
|
124
|
4 |
|
return $value; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Filter XML attribute name |
129
|
|
|
* |
130
|
|
|
* @param string $value Element name |
131
|
|
|
* |
132
|
|
|
* @throws DOMException If the Element attribute name is invalid |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
4 |
|
protected function filterAttributeName(string $value): string |
137
|
|
|
{ |
138
|
4 |
|
if ('' === $value) { |
139
|
2 |
|
return $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
new DOMAttr($value); |
143
|
|
|
|
144
|
4 |
|
return $value; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|