summaryrefslogtreecommitdiff
path: root/src/Http/Validation/ValidatesRequest.php
blob: 142bd1292f9f484e6de5adde6f235c960a3f10fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php

namespace Engelsystem\Http\Validation;

use Engelsystem\Http\Exceptions\ValidationException;
use Engelsystem\Http\Request;

trait ValidatesRequest
{
    /** @var Validator */
    protected $validator;

    /**
     * @param Request $request
     * @param array   $rules
     * @return array
     */
    protected function validate(Request $request, array $rules)
    {
        $isValid = $this->validator->validate(
            (array)$request->getParsedBody(),
            $rules
        );

        if (!$isValid) {
            throw new ValidationException($this->validator);
        }

        return $this->validator->getData();
    }

    /**
     * @param Validator $validator
     */
    public function setValidator(Validator $validator)
    {
        $this->validator = $validator;
    }
}