Passed
Push — master ( 1269ee...6a85bf )
by Rustam
02:55
created

EditPostRequest::getRules()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2.0011

Importance

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