Skip to content

Commit f5c37eb

Browse files
alexdawnAlex Dawn
and
Alex Dawn
authored
fix: Make constructor argument ordering valid (#25)
Co-authored-by: Alex Dawn <[email protected]>
1 parent 34afb02 commit f5c37eb

File tree

4 files changed

+93
-74
lines changed

4 files changed

+93
-74
lines changed

README.md

+57-43
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ Set a valid csv file path, pass the CSV delimiter and pass in your validation ru
4242
```php
4343
use Oshomo\CsvUtils\Validator\Validator;
4444

45-
$validator = new Validator("some/valid/file_path", ",", [
46-
"name" => ["ascii_only"],
47-
"uri" => ["url"],
48-
"stars" => ["between:0,5"]
49-
]);
45+
$validator = new Validator(
46+
"some/valid/file_path",
47+
[
48+
"name" => ["ascii_only"],
49+
"uri" => ["url"],
50+
"stars" => ["between:0,5"]
51+
]
52+
);
5053
```
5154

5255
##### Validating the CSV
@@ -58,9 +61,12 @@ A better implementation:
5861
```php
5962
use Oshomo\CsvUtils\Validator\Validator;
6063

61-
$validator = new Validator("some/valid/file_path", ",", [
62-
'title' => ["ascii_only", "url"]
63-
]);
64+
$validator = new Validator(
65+
"some/valid/file_path",
66+
[
67+
'title' => ["ascii_only", "url"]
68+
]
69+
);
6470

6571
if ($validator->fails()) {
6672
// Do something when validation fails
@@ -77,20 +83,22 @@ You can also customize the error messages for different validation rules and dif
7783
```php
7884
use Oshomo\CsvUtils\Validator\Validator;
7985

80-
$validator = new Validator("some/valid/file_path", ",", [
81-
'title' => ["ascii_only", "url"]
82-
], [
83-
'ascii_only' => 'The :value supplied for :attribute attribute is invalid on line :line of the CSV.',
84-
// This specifies a custom message for a given attribute.
85-
'hotel_link:url' => 'The :attribute must be a valid link. This error occured on line :line of the CSV.',
86-
]);
86+
$validator = new Validator(
87+
"some/valid/file_path",
88+
['title' => ["ascii_only", "url"]],
89+
[
90+
'ascii_only' => 'The :value supplied for :attribute attribute is invalid on line :line of the CSV.',
91+
// This specifies a custom message for a given attribute.
92+
'hotel_link:url' => 'The :attribute must be a valid link. This error occured on line :line of the CSV.',
93+
]
94+
);
8795
```
8896

89-
In this above example:
97+
In this above example:
9098

91-
The `:attribute` place-holder will be replaced by the actual name of the field under validation.
92-
The `:value` place-holder will be replaced with value being validated.
93-
The `:line` place-holder will also be replaced with the row/line number in the CSV in which the error happened.
99+
The `:attribute` place-holder will be replaced by the actual name of the field under validation.
100+
The `:value` place-holder will be replaced with value being validated.
101+
The `:line` place-holder will also be replaced with the row/line number in the CSV in which the error happened.
94102

95103
You may also utilize other place-holders in validation messages. For example the `between` rule exposes two other placeholder `min` and `max`. Find more about this in the available rules section
96104

@@ -100,18 +108,18 @@ You may also utilize other place-holders in validation messages. For example the
100108
```
101109
Validates that a cell value is between a :min and :max. The rule exposes the :min and :max placeholder for inline messages
102110
```
103-
`ascii_only`:
111+
`ascii_only`:
104112
```
105113
Validates that a cell value does not contain a non-ascii character
106114
```
107-
`url`:
115+
`url`:
108116
```
109-
Validates that a cell value is a valid URL. By valid URL we mean
117+
Validates that a cell value is a valid URL. By valid URL we mean
110118
111-
(#protocol)
112-
(#basic auth)
113-
(#a domain name or #an IP address or #an IPv6 address)
114-
(#a port(optional)) then
119+
(#protocol)
120+
(#basic auth)
121+
(#a domain name or #an IP address or #an IPv6 address)
122+
(#a port(optional)) then
115123
(#a /, nothing, a / with something, a query or a fragment)
116124
117125
```
@@ -125,11 +133,14 @@ use Oshomo\CsvUtils\Validator\Validator;
125133
use Oshomo\CsvUtils\Converter\JsonConverter;
126134
use Oshomo\CsvUtils\Converter\XmlConverter;
127135

128-
$validator = new Validator('some/valid/file_path', ',', [
129-
"stars" => ["between:0,5"],
130-
"name" => ["ascii_only"],
131-
"uri" => ["url"],
132-
]);
136+
$validator = new Validator(
137+
'some/valid/file_path',
138+
[
139+
"stars" => ["between:0,5"],
140+
"name" => ["ascii_only"],
141+
"uri" => ["url"],
142+
]
143+
);
133144

134145
if(!$validator->fails()) {
135146
$validator->write(new JsonConverter());
@@ -166,9 +177,10 @@ Passing a custom rule to the validator is easy. Create a CustomRule class the im
166177
```php
167178
use Oshomo\CsvUtils\Validator\Validator;
168179

169-
$validator = new Validator('some/valid/file_path', ',', [
170-
"name" => ["ascii_only", new UppercaseRule]
171-
]);
180+
$validator = new Validator(
181+
'some/valid/file_path',
182+
["name" => ["ascii_only", new UppercaseRule]]
183+
);
172184
```
173185

174186
The class definition for `UppercaseRule`. Follow the same approach if you want to create your own rule.
@@ -193,7 +205,7 @@ class UppercaseRule implements ValidationRuleInterface
193205

194206
/**
195207
* Get the validation error message. Specify the message that should
196-
* be returned if the validation fails. You can make use of the
208+
* be returned if the validation fails. You can make use of the
197209
* :attribute and :value placeholders in the message string
198210
*
199211
* @return string
@@ -215,13 +227,15 @@ If you only need the functionality of a custom rule once throughout your applica
215227
```php
216228
use Oshomo\CsvUtils\Validator\Validator;
217229

218-
$validator = new Validator("some/valid/file_path", ",", [
219-
"uri" => ["url", function($value, $fail) {
220-
if (strpos($value, "https://") !== 0) {
221-
return $fail('The URL passed must be https i.e it must start with https://');
222-
}
223-
}]
224-
]);
230+
$validator = new Validator(
231+
"some/valid/file_path",
232+
[
233+
"uri" => ["url", function($value, $fail) {
234+
if (strpos($value, "https://") !== 0) {
235+
return $fail('The URL passed must be https i.e it must start with https://');
236+
}
237+
}]
238+
]);
225239
```
226240

227241
##### Writing CSV Output Data to Other Formats
@@ -291,7 +305,7 @@ Run `composer test` from the root of the Package.
291305
### Contributing to this Repo
292306

293307
Feel free to submit a pull request for a feature or bug fix. However, do note that before your pull request can be merged it must have test written or updated as the case maybe.
294-
The project run's automatic checks to make sure that the Symfony code standards are met using [php-cs-fixer](https://symfony.com/doc/current/contributing/code/standards.html).
308+
The project run's automatic checks to make sure that the Symfony code standards are met using [php-cs-fixer](https://symfony.com/doc/current/contributing/code/standards.html).
295309

296310
So, before pushing or making any pull request run the below command:
297311

index.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88

99
$file_path = realpath(dirname(__FILE__));
1010
$file = $file_path . '/sample/sample.csv';
11-
$validator = new Validator($file, ',', [
12-
'stars' => ['between:7,10'],
13-
'name' => ['ascii_only'],
14-
'uri' => ['url', function ($value, $fail) {
15-
if (0 !== strpos($value, 'https://')) {
16-
return $fail('The URL passed must be https i.e it must start with https://');
17-
}
18-
}],
19-
]);
11+
$validator = new Validator(
12+
$file,
13+
[
14+
'stars' => ['between:7,10'],
15+
'name' => ['ascii_only'],
16+
'uri' => ['url', function ($value, $fail) {
17+
if (0 !== strpos($value, 'https://')) {
18+
return $fail('The URL passed must be https i.e it must start with https://');
19+
}
20+
}],
21+
]
22+
);
2023

2124
if (!$validator->fails()) {
2225
$validator->write(new JsonConverter());

src/Validator/Validator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class Validator
114114
/**
115115
* Create a new Validator instance.
116116
*/
117-
public function __construct(string $filePath, string $delimiter = ',', array $rules, array $messages = [])
117+
public function __construct(string $filePath, array $rules, string $delimiter = ',', array $messages = [])
118118
{
119119
$this->filePath = $filePath;
120120
$this->delimiter = $delimiter;

tests/src/CsvValidatorTest.php

+23-21
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testInvalidCsvFilePath()
2626
{
2727
$file = $this->testAssets . '/tests.csv';
2828

29-
$validator = new Validator($file, ',', [
29+
$validator = new Validator($file, [
3030
'stars' => ['between:0,5'],
3131
]);
3232

@@ -40,7 +40,7 @@ public function testAsciiOnlyValidationRule()
4040
{
4141
$file = $this->testAssets . '/ascii_test.csv';
4242

43-
$validator = new Validator($file, ',', [
43+
$validator = new Validator($file, [
4444
'name' => ['ascii_only'],
4545
]);
4646

@@ -66,7 +66,7 @@ public function testBetweenValidationRule()
6666
{
6767
$file = $this->testAssets . '/between_test.csv';
6868

69-
$validator = new Validator($file, ',', [
69+
$validator = new Validator($file, [
7070
'stars' => ['between:4,10'],
7171
]);
7272

@@ -92,7 +92,7 @@ public function testUrlValidationRule()
9292
{
9393
$file = $this->testAssets . '/url_test.csv';
9494

95-
$validator = new Validator($file, ',', [
95+
$validator = new Validator($file, [
9696
'uri' => ['url'],
9797
]);
9898

@@ -132,7 +132,7 @@ public function testValidatorWithCustomRuleObject()
132132
{
133133
$file = $this->testAssets . '/ascii_test.csv';
134134

135-
$validator = new Validator($file, ',', [
135+
$validator = new Validator($file, [
136136
'name' => [new UppercaseRule()],
137137
]);
138138

@@ -158,7 +158,7 @@ public function testValidatorWithCustomRuleClosure()
158158
{
159159
$file = $this->testAssets . '/url_test.csv';
160160

161-
$validator = new Validator($file, ',', [
161+
$validator = new Validator($file, [
162162
'uri' => [function ($value, $fail) {
163163
if (0 !== strpos($value, 'https://')) {
164164
return $fail('The URL passed must be https i.e it must start with https://');
@@ -184,11 +184,12 @@ public function testValidatorWithCustomErrorMessage()
184184
$file = $this->testAssets . '/ascii_test.csv';
185185
$customErrorMessage = 'The value supplied for the name attribute must only contain ascii characters';
186186

187-
$validator = new Validator($file, ',', [
188-
'name' => ['ascii_only'],
189-
], [
190-
'ascii_only' => $customErrorMessage,
191-
]);
187+
$validator = new Validator(
188+
$file,
189+
['name' => ['ascii_only']],
190+
',',
191+
['ascii_only' => $customErrorMessage]
192+
);
192193

193194
$this->assertTrue($validator->fails());
194195

@@ -212,11 +213,12 @@ public function testValidatorWithCustomErrorMessageWithPlaceholder()
212213
{
213214
$file = $this->testAssets . '/between_test.csv';
214215

215-
$validator = new Validator($file, ',', [
216-
'stars' => ['between:4,10'],
217-
], [
218-
'between' => 'The value supplied for :attribute must be between :min and :max',
219-
]);
216+
$validator = new Validator(
217+
$file,
218+
['stars' => ['between:4,10']],
219+
',',
220+
['between' => 'The value supplied for :attribute must be between :min and :max']
221+
);
220222

221223
$this->assertTrue($validator->fails());
222224

@@ -240,7 +242,7 @@ public function testValidatorJsonWriter()
240242
{
241243
$file = $this->testAssets . '/valid_test.csv';
242244

243-
$validator = new Validator($file, ',', [
245+
$validator = new Validator($file, [
244246
'name' => ['ascii_only'],
245247
'stars' => ['between:3,10'],
246248
'uri' => ['url'],
@@ -265,7 +267,7 @@ public function testValidatorXmlWriter()
265267
{
266268
$file = $this->testAssets . '/valid_test.csv';
267269

268-
$validator = new Validator($file, ',', [
270+
$validator = new Validator($file, [
269271
'name' => ['ascii_only'],
270272
'stars' => ['between:3,10'],
271273
'uri' => ['url'],
@@ -303,7 +305,7 @@ public function testValidatorCsvOnEmptyRule()
303305
],
304306
];
305307

306-
$validator = new Validator($file, ',', [
308+
$validator = new Validator($file, [
307309
'stars' => [''],
308310
]);
309311

@@ -314,7 +316,7 @@ public function testValidatorCsvIsValid()
314316
{
315317
$file = $this->testAssets . '/valid_test.csv';
316318

317-
$validator = new Validator($file, ',', [
319+
$validator = new Validator($file, [
318320
'stars' => ['between:3,10'],
319321
]);
320322

@@ -338,7 +340,7 @@ public function testValidatorXmlWriterWithRecordElementParameter()
338340
{
339341
$file = $this->testAssets . '/valid_test.csv';
340342

341-
$validator = new Validator($file, ',', [
343+
$validator = new Validator($file, [
342344
'name' => ['ascii_only'],
343345
'stars' => ['between:3,10'],
344346
'uri' => ['url'],

0 commit comments

Comments
 (0)