Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ValidatesRequest
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 validate
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 setValidator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Http\Validation;
6
7use Engelsystem\Http\Exceptions\ValidationException;
8use Engelsystem\Http\Request;
9
10trait ValidatesRequest
11{
12    protected ?Validator $validator;
13
14    protected function validate(Request $request, array $rules): array
15    {
16        $isValid = $this->validator->validate(
17            (array) $request->getParsedBody(),
18            $rules
19        );
20
21        if (!$isValid) {
22            throw new ValidationException($this->validator);
23        }
24
25        return $this->validator->getData();
26    }
27
28    public function setValidator(Validator $validator): void
29    {
30        $this->validator = $validator;
31    }
32}