Test Failed
Pull Request — master (#87)
by Dmitriy
03:15
created

Request::getText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
                new Required(),
50
                new HasLength(min: 5, max: 255),
51
            ],
52
            'body.text' => [
53
                new Required(),
54
                new HasLength(min: 5, max: 1000),
55
            ],
56
            'body.status' => [
57
                new Required(),
58
                static function ($value): Result {
59
                    $result = new Result();
60
                    if (!PostStatus::isValid($value)) {
61
                        $result->addError('Incorrect status');
62
                    }
63
                    return $result;
64
                },
65
            ],
66
        ];
67
    }
68
}
69