EditPostRequest::getRules()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 14
c 4
b 0
f 0
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
rs 9.7998
cc 2
nc 1
nop 0
crap 2.003
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 Yiisoft\Validator\RulesProviderInterface;
12
use OpenApi\Annotations as OA;
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
                new Required(),
49 3
                new HasLength(min: 5, max: 255),
50
            ],
51
            'body.text' => [
52 3
                new Required(),
53 3
                new HasLength(min: 5, max: 1000),
54
            ],
55
            'body.status' => [
56 3
                new Required(),
57 3
                static function ($value): Result {
58 3
                    $result = new Result();
59 3
                    if (!PostStatus::isValid($value)) {
60
                        $result->addError('Incorrect status');
61
                    }
62 3
                    return $result;
63
                },
64
            ],
65
        ];
66
    }
67
}
68