Skip to content

Commit 6d3b518

Browse files
dmjohnsson23henriquemoody
authored andcommitted
Abstract common functionality from Min and Max
This base class could also be used for other aggregate operations on arrays, e.g. a sum. (It can't be used for `Length` though, as we would not be able to validate a length of 0.).
1 parent 243f91c commit 6d3b518

File tree

3 files changed

+61
-46
lines changed

3 files changed

+61
-46
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Respect\Validation\Rules\Core;
11+
12+
use Respect\Validation\Result;
13+
14+
use function array_map;
15+
16+
abstract class ArrayAggregateFunction extends FilteredNonEmptyArray
17+
{
18+
protected string $idPrefix;
19+
20+
/**
21+
* This function should extract the aggregate data from the input array
22+
*
23+
* @param non-empty-array<mixed> $input
24+
*/
25+
abstract protected function extractAggregate(array $input): mixed;
26+
27+
/** @param non-empty-array<mixed> $input */
28+
protected function evaluateNonEmptyArray(array $input): Result
29+
{
30+
$aggregate = $this->extractAggregate($input);
31+
32+
return $this->enrichResult($input, $this->rule->evaluate($aggregate));
33+
}
34+
35+
private function enrichResult(mixed $input, Result $result): Result
36+
{
37+
if (!$result->allowsSubsequent()) {
38+
return $result
39+
->withInput($input)
40+
->withChildren(
41+
...array_map(fn(Result $child) => $this->enrichResult($input, $child), $result->children)
42+
);
43+
}
44+
45+
return (new Result($result->isValid, $input, $this, id: $result->id))
46+
->withPrefixedId($this->idPrefix)
47+
->withSubsequent($result->withInput($input));
48+
}
49+
}

library/Rules/Max.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,19 @@
1111

1212
use Attribute;
1313
use Respect\Validation\Message\Template;
14-
use Respect\Validation\Result;
15-
use Respect\Validation\Rules\Core\FilteredNonEmptyArray;
14+
use Respect\Validation\Rules\Core\ArrayAggregateFunction;
1615

17-
use function array_map;
1816
use function max;
1917

2018
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
2119
#[Template('The maximum of', 'The maximum of')]
22-
final class Max extends FilteredNonEmptyArray
20+
final class Max extends ArrayAggregateFunction
2321
{
24-
/** @param non-empty-array<mixed> $input */
25-
protected function evaluateNonEmptyArray(array $input): Result
26-
{
27-
$max = max($input);
28-
29-
return $this->enrichResult($input, $this->rule->evaluate($max));
30-
}
22+
protected string $idPrefix = 'max';
3123

32-
private function enrichResult(mixed $input, Result $result): Result
24+
/** @param non-empty-array<mixed> $input */
25+
protected function extractAggregate(array $input): mixed
3326
{
34-
if (!$result->allowsSubsequent()) {
35-
return $result
36-
->withInput($input)
37-
->withChildren(
38-
...array_map(fn(Result $child) => $this->enrichResult($input, $child), $result->children)
39-
);
40-
}
41-
42-
return (new Result($result->isValid, $input, $this, id: $result->id))
43-
->withPrefixedId('max')
44-
->withSubsequent($result->withInput($input));
27+
return max($input);
4528
}
4629
}

library/Rules/Min.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,19 @@
1111

1212
use Attribute;
1313
use Respect\Validation\Message\Template;
14-
use Respect\Validation\Result;
15-
use Respect\Validation\Rules\Core\FilteredNonEmptyArray;
14+
use Respect\Validation\Rules\Core\ArrayAggregateFunction;
1615

17-
use function array_map;
1816
use function min;
1917

2018
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
2119
#[Template('The minimum of', 'The minimum of')]
22-
final class Min extends FilteredNonEmptyArray
20+
final class Min extends ArrayAggregateFunction
2321
{
24-
/** @param non-empty-array<mixed> $input */
25-
protected function evaluateNonEmptyArray(array $input): Result
26-
{
27-
$min = min($input);
28-
29-
return $this->enrichResult($input, $this->rule->evaluate($min));
30-
}
22+
protected string $idPrefix = 'min';
3123

32-
private function enrichResult(mixed $input, Result $result): Result
24+
/** @param non-empty-array<mixed> $input */
25+
protected function extractAggregate(array $input): mixed
3326
{
34-
if (!$result->allowsSubsequent()) {
35-
return $result
36-
->withInput($input)
37-
->withChildren(
38-
...array_map(fn(Result $child) => $this->enrichResult($input, $child), $result->children)
39-
);
40-
}
41-
42-
return (new Result($result->isValid, $input, $this, id: $result->id))
43-
->withPrefixedId('min')
44-
->withSubsequent($result->withInput($input));
27+
return min($input);
4528
}
4629
}

0 commit comments

Comments
 (0)