Test Failed
Pull Request — master (#87)
by Dmitriy
02:38
created

Request::getRules()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 1
nop 0
dl 0
loc 23
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Infrastructure\IO\Http\Blog\PutUpdate\Request;
6
7
use App\Application\Blog\Entity\Post\PostStatus;
8
use OpenApi\Annotations as OA;
9
use Yiisoft\RequestModel\RequestModel;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\Rule\HasLength;
12
use Yiisoft\Validator\Rule\Required;
13
use Yiisoft\Validator\RulesProviderInterface;
14
15
/**
16
 * @OA\Schema(
17
 *      schema="BlogUpdateRequest",
18
 *      @OA\Property(example="Title post", property="title", format="string"),
19
 *      @OA\Property(example="Text post", property="text", format="string"),
20
 *      @OA\Property(example=1, property="status", format="int"),
21
 * )
22
 */
23
final class Request extends RequestModel implements RulesProviderInterface
24
{
25
    public function getId(): int
26
    {
27
        return (int)$this->getAttributeValue('router.id');
28
    }
29
30
    public function getTitle(): string
31
    {
32
        return (string)$this->getAttributeValue('body.title');
33
    }
34
35
    public function getText(): string
36
    {
37
        return (string)$this->getAttributeValue('body.text');
38
    }
39
40
    public function getStatus(): PostStatus
41
    {
42
        return PostStatus::from($this->getAttributeValue('body.status'));
43
    }
44
45
    public function getRules(): array
46
    {
47
        return [
48
            'body.title' => [
49
                Required::rule(),
50
                HasLength::rule()
51
                    ->min(5)
52
                    ->max(255),
53
            ],
54
            'body.text' => [
55
                Required::rule(),
56
                HasLength::rule()
57
                    ->min(5)
58
                    ->max(1000),
59
            ],
60
            'body.status' => [
61
                Required::rule(),
62
                static function ($value): Result {
63
                    $result = new Result();
64
                    if (!PostStatus::isValid($value)) {
65
                        $result->addError('Incorrect status');
66
                    }
67
                    return $result;
68
                },
69
            ],
70
        ];
71
    }
72
}
73