File tree Expand file tree Collapse file tree 3 files changed +61
-46
lines changed
Expand file tree Collapse file tree 3 files changed +61
-46
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1111
1212use Attribute ;
1313use 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 ;
1816use 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}
Original file line number Diff line number Diff line change 1111
1212use Attribute ;
1313use 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 ;
1816use 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}
You can’t perform that action at this time.
0 commit comments