Skip to content

Commit d962d33

Browse files
authored
Added some validation rules such as ExcludeIf and ProhibitedIf. (#6094)
1 parent a35fe9b commit d962d33

18 files changed

+1523
-2
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"php": ">=8.0",
2222
"egulias/email-validator": "^3.0",
2323
"hyperf/collection": "~3.0.0",
24+
"hyperf/conditionable": "~3.0.0",
2425
"hyperf/contract": "~3.0.0",
2526
"hyperf/database": "~3.0.0",
2627
"hyperf/di": "~3.0.0",
@@ -29,6 +30,7 @@
2930
"hyperf/macroable": "~3.0.0",
3031
"hyperf/tappable": "~3.0.0",
3132
"hyperf/translation": "~3.0.0",
33+
"hyperf/stringable": "~3.0.0",
3234
"hyperf/support": "~3.0.0",
3335
"hyperf/utils": "~3.0.0",
3436
"nesbot/carbon": "^2.21",

src/Concerns/ValidatesAttributes.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,25 @@ public function validateDigitsBetween(string $attribute, $value, array $paramete
342342
*/
343343
public function validateDimensions(string $attribute, $value, array $parameters): bool
344344
{
345-
if (! $this->isValidFileInstance($value) || ! $sizeDetails = @getimagesize($value->getRealPath())) {
345+
if ($this->isValidFileInstance($value) && in_array($value->getMimeType(), ['image/svg+xml', 'image/svg'])) {
346+
return true;
347+
}
348+
349+
if (! $this->isValidFileInstance($value)) {
350+
return false;
351+
}
352+
353+
$dimensions = method_exists($value, 'dimensions')
354+
? $value->dimensions()
355+
: @getimagesize($value->getRealPath());
356+
357+
if (! $dimensions) {
346358
return false;
347359
}
348360

349361
$this->requireParameterCount(1, $parameters, 'dimensions');
350362

351-
[$width, $height] = $sizeDetails;
363+
[$width, $height] = $dimensions;
352364

353365
$parameters = $this->parseNamedParameters($parameters);
354366

src/Contract/DataAwareRule.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Validation\Contract;
13+
14+
interface DataAwareRule
15+
{
16+
/**
17+
* Set the data under validation.
18+
*
19+
* @return $this
20+
*/
21+
public function setData(array $data): static;
22+
}

src/Contract/ValidatorAwareRule.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Validation\Contract;
13+
14+
use Hyperf\Validation\Validator;
15+
16+
interface ValidatorAwareRule
17+
{
18+
/**
19+
* Set the current validator.
20+
*
21+
* @return $this
22+
*/
23+
public function setValidator(Validator $validator): static;
24+
}

src/Rules/ExcludeIf.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Validation\Rules;
13+
14+
use Closure;
15+
use InvalidArgumentException;
16+
use Stringable;
17+
18+
class ExcludeIf implements Stringable
19+
{
20+
public Closure|bool $condition;
21+
22+
/**
23+
* Create a new exclude validation rule based on a condition.
24+
* @param bool|Closure $condition the condition that validates the attribute
25+
* @throws InvalidArgumentException
26+
*/
27+
public function __construct($condition)
28+
{
29+
if ($condition instanceof Closure || is_bool($condition)) {
30+
$this->condition = $condition;
31+
} else {
32+
throw new InvalidArgumentException('The provided condition must be a callable or boolean.');
33+
}
34+
}
35+
36+
/**
37+
* Convert the rule to a validation string.
38+
*
39+
* @return string
40+
*/
41+
public function __toString()
42+
{
43+
if (is_callable($this->condition)) {
44+
return call_user_func($this->condition) ? 'exclude' : '';
45+
}
46+
47+
return $this->condition ? 'exclude' : '';
48+
}
49+
}

0 commit comments

Comments
 (0)