Skip to content

Commit 1a4c151

Browse files
author
riccardodallavia
committed
FIX custom rules minor issues
1 parent 633279e commit 1a4c151

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/Rules/ExistsEncrypted.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,36 @@
55
use Illuminate\Contracts\Validation\Rule;
66
use Illuminate\Support\Facades\Validator;
77
use Illuminate\Support\Str;
8+
use Illuminate\Support\Traits\ForwardsCalls;
89
use Illuminate\Validation\Rules\Exists;
910
use Maize\Encryptable\Encryption;
1011

11-
class ExistsEncrypted extends Exists implements Rule
12+
class ExistsEncrypted implements Rule
1213
{
14+
use ForwardsCalls;
15+
16+
private Exists $rule;
17+
18+
public function __construct(string $table, string $column = 'NULL')
19+
{
20+
$this->rule = new Exists($table, $column);
21+
}
22+
23+
public function __call(string $name, array $arguments)
24+
{
25+
$this->forwardCallTo($this->rule, $name, $arguments);
26+
27+
return $this;
28+
}
29+
1330
public function passes($attribute, $value): bool
1431
{
1532
$attribute = Str::before($attribute, '.');
1633

1734
return ! Validator::make([
1835
$attribute => Encryption::php()->encrypt($value),
1936
], [
20-
$attribute => (string) $this,
37+
$attribute => $this->rule,
2138
])->fails();
2239
}
2340

src/Rules/UniqueEncrypted.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,36 @@
55
use Illuminate\Contracts\Validation\Rule;
66
use Illuminate\Support\Facades\Validator;
77
use Illuminate\Support\Str;
8+
use Illuminate\Support\Traits\ForwardsCalls;
89
use Illuminate\Validation\Rules\Unique;
910
use Maize\Encryptable\Encryption;
1011

11-
class UniqueEncrypted extends Unique implements Rule
12+
class UniqueEncrypted implements Rule
1213
{
14+
use ForwardsCalls;
15+
16+
private Unique $rule;
17+
18+
public function __construct(string $table, string $column = 'NULL')
19+
{
20+
$this->rule = new Unique($table, $column);
21+
}
22+
23+
public function __call(string $name, array $arguments)
24+
{
25+
$this->forwardCallTo($this->rule, $name, $arguments);
26+
27+
return $this;
28+
}
29+
1330
public function passes($attribute, $value): bool
1431
{
1532
$attribute = Str::before($attribute, '.');
1633

1734
return ! Validator::make([
1835
$attribute => Encryption::php()->encrypt($value),
1936
], [
20-
$attribute => (string) $this,
37+
$attribute => $this->rule,
2138
])->fails();
2239
}
2340

0 commit comments

Comments
 (0)