GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 51efb0...4d5462 )
by Marco
01:21
created

Any::validationsWithTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Marcosh\PhpValidationDSL\Combinator;
6
7
use Marcosh\PhpValidationDSL\Result\ValidationResult;
8
use Marcosh\PhpValidationDSL\Translator\Translator;
9
use Marcosh\PhpValidationDSL\Validation;
10
use Webmozart\Assert\Assert;
11
use function is_callable;
12
13
final class Any implements Validation
14
{
15
    public const NOT_EVEN_ONE = 'any.not-even-one';
16
17
    /**
18
     * @var Validation[]
19
     */
20
    private $validations;
21
22
    /**
23
     * @var callable $messages -> array
24
     */
25
    private $errorFormatter;
26
27
    /**
28
     * @param Validation[] $validations
29
     * @param callable|null $errorFormatter
30
     */
31
    public function __construct(array $validations, ?callable $errorFormatter = null)
32
    {
33
        Assert::allIsInstanceOf($validations, Validation::class);
34
35
        $this->validations = $validations;
36
        $this->errorFormatter = is_callable($errorFormatter) ?
37
            $errorFormatter :
38
            function (array $messages) {
39
                return [
40
                    self::NOT_EVEN_ONE => $messages
41
                ];
42
            };
43
    }
44
45
    /**
46
     * @param Validation[] $validations
47
     * @return self
48
     */
49
    public static function validations(array $validations): self
50
    {
51
        return new self($validations);
52
    }
53
54
    /**
55
     * @param Validation[] $validations
56
     * @param callable $errorFormatter
57
     * @return self
58
     */
59
    public static function validationsWithFormatter(array $validations, callable $errorFormatter): self
60
    {
61
        return new self($validations, $errorFormatter);
62
    }
63
64
    public static function validationsWithTranslator(array $validations, Translator $translator): self
65
    {
66
        return new self(
67
            $validations,
68
            function (array $messages) use ($translator) {
69
                return [
70
                    $translator->translate(self::NOT_EVEN_ONE) => $messages
71
                ];
72
            }
73
        );
74
    }
75
76
    public function validate($data, array $context = []): ValidationResult
77
    {
78
        $result = ValidationResult::errors([]);
79
80
        foreach ($this->validations as $validation) {
81
            $result = $result->meet($validation->validate($data, $context), 'array_merge');
82
        }
83
84
        $result = $result->mapErrors($this->errorFormatter);
85
        return $result;
86
    }
87
}
88