Skip to content

Commit a712508

Browse files
Upgrade to Laravel 10.
1 parent a1fb016 commit a712508

24 files changed

+315
-144
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
# PHPUnit
66
/.phpunit.result.cache
7+
/.phpunit.cache
78

89
# Other
9-
.idea/
10+
.idea/

composer.json

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sebastiansulinski/laravel-validation",
3-
"description": "Validation wrapper for Laravel 5.8+",
3+
"description": "Validation wrapper for Laravel",
44
"license": "MIT",
55
"authors": [
66
{
@@ -16,19 +16,40 @@
1616
},
1717
"autoload-dev": {
1818
"psr-4": {
19-
"SSDTest\\": "tests/"
19+
"SSDTest\\": "tests/",
20+
"Workbench\\App\\": "workbench/app/",
21+
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
22+
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
2023
}
2124
},
2225
"require-dev": {
23-
"orchestra/testbench": "^7.1"
26+
"laravel/pint": "dev-main",
27+
"orchestra/testbench": "^8.0"
2428
},
2529
"require": {
26-
"php": "^8.0",
27-
"laravel/framework": "^9.0"
30+
"php": "^8.1",
31+
"laravel/framework": "^10.0"
2832
},
2933
"config": {
3034
"optimize-autoloader": true,
3135
"preferred-install": "dist",
3236
"sort-packages": true
37+
},
38+
"scripts": {
39+
"post-autoload-dump": [
40+
"@clear",
41+
"@prepare"
42+
],
43+
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
44+
"prepare": "@php vendor/bin/testbench package:discover --ansi",
45+
"build": "@php vendor/bin/testbench workbench:build --ansi",
46+
"serve": [
47+
"Composer\\Config::disableProcessTimeout",
48+
"@build",
49+
"@php vendor/bin/testbench serve"
50+
],
51+
"test": [
52+
"@php vendor/bin/phpunit"
53+
]
3354
}
3455
}

phpunit.xml

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage processUncoveredFiles="true">
4-
<include>
5-
<directory suffix=".php">./src</directory>
6-
</include>
7-
</coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<coverage/>
84
<testsuites>
95
<testsuite name="Feature">
106
<directory suffix="Test.php">./tests</directory>
@@ -13,4 +9,9 @@
139
<php>
1410
<env name="DB_CONNECTION" value="testing"/>
1511
</php>
12+
<source>
13+
<include>
14+
<directory suffix=".php">./src</directory>
15+
</include>
16+
</source>
1617
</phpunit>

src/Factory.php

+83-49
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace SSD\LaravelValidation;
44

55
use Closure;
6-
use Illuminate\Support\Str;
76
use Illuminate\Contracts\Container\Container;
87
use Illuminate\Contracts\Translation\Translator;
9-
use Illuminate\Validation\PresenceVerifierInterface;
108
use Illuminate\Contracts\Validation\Factory as FactoryContract;
9+
use Illuminate\Support\Str;
10+
use Illuminate\Validation\PresenceVerifierInterface;
1111

1212
class Factory implements FactoryContract
1313
{
@@ -28,45 +28,52 @@ class Factory implements FactoryContract
2828
/**
2929
* The IoC container instance.
3030
*
31-
* @var \Illuminate\Contracts\Container\Container
31+
* @var \Illuminate\Contracts\Container\Container|null
3232
*/
3333
protected $container;
3434

3535
/**
3636
* All of the custom validator extensions.
3737
*
38-
* @var array
38+
* @var array<string, \Closure|string>
3939
*/
4040
protected $extensions = [];
4141

4242
/**
4343
* All of the custom implicit validator extensions.
4444
*
45-
* @var array
45+
* @var array<string, \Closure|string>
4646
*/
4747
protected $implicitExtensions = [];
4848

4949
/**
5050
* All of the custom dependent validator extensions.
5151
*
52-
* @var array
52+
* @var array<string, \Closure|string>
5353
*/
5454
protected $dependentExtensions = [];
5555

5656
/**
5757
* All of the custom validator message replacers.
5858
*
59-
* @var array
59+
* @var array<string, \Closure|string>
6060
*/
6161
protected $replacers = [];
6262

6363
/**
6464
* All of the fallback messages for custom rules.
6565
*
66-
* @var array
66+
* @var array<string, string>
6767
*/
6868
protected $fallbackMessages = [];
6969

70+
/**
71+
* Indicates that unvalidated array keys should be excluded, even if the parent array was validated.
72+
*
73+
* @var bool
74+
*/
75+
protected $excludeUnvalidatedArrayKeys = true;
76+
7077
/**
7178
* The Validator resolver instance.
7279
*
@@ -77,11 +84,9 @@ class Factory implements FactoryContract
7784
/**
7885
* Create a new Validator factory instance.
7986
*
80-
* @param \Illuminate\Contracts\Translation\Translator $translator
81-
* @param \Illuminate\Contracts\Container\Container $container
8287
* @return void
8388
*/
84-
public function __construct(Translator $translator, Container $container = null)
89+
public function __construct(Translator $translator, ?Container $container = null)
8590
{
8691
$this->container = $container;
8792
$this->translator = $translator;
@@ -90,32 +95,30 @@ public function __construct(Translator $translator, Container $container = null)
9095
/**
9196
* Create a new Validator instance.
9297
*
93-
* @param array $data
94-
* @param array $rules
95-
* @param array $messages
96-
* @param array $customAttributes
97-
* @return \Illuminate\Validation\Validator
98+
* @return \Illuminate\Contracts\Validation\Validator
9899
*/
99-
public function make(array $data, array $rules, array $messages = [], array $customAttributes = [])
100+
public function make(array $data, array $rules, array $messages = [], array $attributes = [])
100101
{
101102
$validator = $this->resolve(
102-
$data, $rules, $messages, $customAttributes
103+
$data, $rules, $messages, $attributes
103104
);
104105

105106
// The presence verifier is responsible for checking the unique and exists data
106107
// for the validator. It is behind an interface so that multiple versions of
107108
// it may be written besides database. We'll inject it into the validator.
108-
if (!is_null($this->verifier)) {
109+
if (! is_null($this->verifier)) {
109110
$validator->setPresenceVerifier($this->verifier);
110111
}
111112

112113
// Next we'll set the IoC container instance of the validator, which is used to
113114
// resolve out class based validator extensions. If it is not set then these
114115
// types of extensions will not be possible on these validation instances.
115-
if (!is_null($this->container)) {
116+
if (! is_null($this->container)) {
116117
$validator->setContainer($this->container);
117118
}
118119

120+
$validator->excludeUnvalidatedArrayKeys = $this->excludeUnvalidatedArrayKeys;
121+
119122
$this->addExtensions($validator);
120123

121124
return $validator;
@@ -124,41 +127,32 @@ public function make(array $data, array $rules, array $messages = [], array $cus
124127
/**
125128
* Validate the given data against the provided rules.
126129
*
127-
* @param array $data
128-
* @param array $rules
129-
* @param array $messages
130-
* @param array $customAttributes
131130
* @return void
132131
*
133132
* @throws \Illuminate\Validation\ValidationException
134133
*/
135-
public function validate(array $data, array $rules, array $messages = [], array $customAttributes = [])
134+
public function validate(array $data, array $rules, array $messages = [], array $attributes = [])
136135
{
137-
$this->make($data, $rules, $messages, $customAttributes)->validate();
136+
$this->make($data, $rules, $messages, $attributes)->validate();
138137
}
139138

140139
/**
141140
* Resolve a new Validator instance.
142141
*
143-
* @param array $data
144-
* @param array $rules
145-
* @param array $messages
146-
* @param array $customAttributes
147-
* @return \Illuminate\Validation\Validator
142+
* @return \Illuminate\Contracts\Validation\Validator
148143
*/
149-
protected function resolve(array $data, array $rules, array $messages, array $customAttributes)
144+
protected function resolve(array $data, array $rules, array $messages, array $attributes)
150145
{
151146
if (is_null($this->resolver)) {
152-
return new Validator($this->translator, $data, $rules, $messages, $customAttributes);
147+
return new Validator($this->translator, $data, $rules, $messages, $attributes);
153148
}
154149

155-
return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $customAttributes);
150+
return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $attributes);
156151
}
157152

158153
/**
159154
* Add the extensions to a validator instance.
160155
*
161-
* @param \SSD\LaravelValidation\Validator $validator
162156
* @return void
163157
*/
164158
protected function addExtensions(Validator $validator)
@@ -180,9 +174,9 @@ protected function addExtensions(Validator $validator)
180174
/**
181175
* Register a custom validator extension.
182176
*
183-
* @param string $rule
184-
* @param \Closure|string $extension
185-
* @param string $message
177+
* @param string $rule
178+
* @param \Closure|string $extension
179+
* @param string $message
186180
* @return void
187181
*/
188182
public function extend($rule, $extension, $message = null)
@@ -197,9 +191,9 @@ public function extend($rule, $extension, $message = null)
197191
/**
198192
* Register a custom implicit validator extension.
199193
*
200-
* @param string $rule
201-
* @param \Closure|string $extension
202-
* @param string $message
194+
* @param string $rule
195+
* @param \Closure|string $extension
196+
* @param string $message
203197
* @return void
204198
*/
205199
public function extendImplicit($rule, $extension, $message = null)
@@ -214,9 +208,9 @@ public function extendImplicit($rule, $extension, $message = null)
214208
/**
215209
* Register a custom dependent validator extension.
216210
*
217-
* @param string $rule
218-
* @param \Closure|string $extension
219-
* @param string $message
211+
* @param string $rule
212+
* @param \Closure|string $extension
213+
* @param string $message
220214
* @return void
221215
*/
222216
public function extendDependent($rule, $extension, $message = null)
@@ -231,19 +225,38 @@ public function extendDependent($rule, $extension, $message = null)
231225
/**
232226
* Register a custom validator message replacer.
233227
*
234-
* @param string $rule
235-
* @param \Closure|string $replacer
228+
* @param string $rule
229+
* @param \Closure|string $replacer
236230
* @return void
237231
*/
238232
public function replacer($rule, $replacer)
239233
{
240234
$this->replacers[$rule] = $replacer;
241235
}
242236

237+
/**
238+
* Indicate that unvalidated array keys should be included in validated data when the parent array is validated.
239+
*
240+
* @return void
241+
*/
242+
public function includeUnvalidatedArrayKeys()
243+
{
244+
$this->excludeUnvalidatedArrayKeys = false;
245+
}
246+
247+
/**
248+
* Indicate that unvalidated array keys should be excluded from the validated data, even if the parent array was validated.
249+
*
250+
* @return void
251+
*/
252+
public function excludeUnvalidatedArrayKeys()
253+
{
254+
$this->excludeUnvalidatedArrayKeys = true;
255+
}
256+
243257
/**
244258
* Set the Validator instance resolver.
245259
*
246-
* @param \Closure $resolver
247260
* @return void
248261
*/
249262
public function resolver(Closure $resolver)
@@ -274,11 +287,32 @@ public function getPresenceVerifier()
274287
/**
275288
* Set the Presence Verifier implementation.
276289
*
277-
* @param \Illuminate\Validation\PresenceVerifierInterface $presenceVerifier
278290
* @return void
279291
*/
280292
public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
281293
{
282294
$this->verifier = $presenceVerifier;
283295
}
284-
}
296+
297+
/**
298+
* Get the container instance used by the validation factory.
299+
*
300+
* @return \Illuminate\Contracts\Container\Container|null
301+
*/
302+
public function getContainer()
303+
{
304+
return $this->container;
305+
}
306+
307+
/**
308+
* Set the container instance used by the validation factory.
309+
*
310+
* @return $this
311+
*/
312+
public function setContainer(Container $container)
313+
{
314+
$this->container = $container;
315+
316+
return $this;
317+
}
318+
}

src/RuleContract.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ interface RuleContract extends Rule
88
{
99
/**
1010
* Get the validation rule.
11-
*
12-
* @return string
1311
*/
1412
public function rule(): string;
15-
}
13+
}

0 commit comments

Comments
 (0)