Test Failed
Push — master ( 09a5f1...c566a2 )
by Alexander
12:51
created

EditPostRequest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 23
c 2
b 0
f 0
dl 0
loc 45
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getText() 0 3 1
A getId() 0 3 1
A getRules() 0 23 2
A getStatus() 0 3 1
A getTitle() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog;
6
7
use Yiisoft\RequestModel\RequestModel;
8
use Yiisoft\RequestModel\ValidatableModelInterface;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\Rule\HasLength;
11
use Yiisoft\Validator\Rule\Required;
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 ValidatableModelInterface
23
{
24
    public function getId(): int
25
    {
26
        return (int)$this->getAttributeValue('attributes.id');
27
    }
28
29
    public function getTitle(): string
30
    {
31
        return (string)$this->getAttributeValue('body.title');
32
    }
33
34
    public function getText(): string
35
    {
36
        return (string)$this->getAttributeValue('body.text');
37
    }
38
39
    public function getStatus(): PostStatus
40
    {
41
        return new PostStatus($this->getAttributeValue('body.status'));
42
    }
43
44
    public function getRules(): array
45
    {
46
        return [
47
            'body.title' => [
48
                new Required(),
49
                (new HasLength())
50
                    ->min(5)
51
                    ->max(255),
52
            ],
53
            'body.text' => [
54
                new Required(),
55
                (new HasLength())
56
                    ->min(5)
57
                    ->max(1000),
58
            ],
59
            'body.status' => [
60
                new Required(),
61
                static function ($value): Result {
62
                    $result = new Result();
63
                    if (!PostStatus::isValid($value)) {
64
                        $result->addError('Incorrect status');
65
                    }
66
                    return $result;
67
                },
68
            ],
69
        ];
70
    }
71
}
72