summaryrefslogtreecommitdiff
path: root/src/Http/Validation/ValidatesRequest.php
blob: 33ff76af6e69c226619f15c8448174b31faa244d (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
<?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)
    {
        if (!$this->validator->validate(
            (array)$request->getParsedBody(),
            $rules
        )) {
            throw new ValidationException($this->validator);
        }

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

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