Passed
Push — master ( 815c87...2f9749 )
by Alexander
03:36
created

EditPostRequest::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
13
final class EditPostRequest extends RequestModel implements ValidatableModelInterface
14
{
15 1
    public function getId(): int
16
    {
17 1
        return (int)$this->getValue('attributes.id');
18
    }
19
20 2
    public function getTitle(): string
21
    {
22 2
        return (string)$this->getValue('body.title');
23
    }
24
25 2
    public function getText(): string
26
    {
27 2
        return (string)$this->getValue('body.text');
28
    }
29
30 2
    public function getStatus(): PostStatus
31
    {
32 2
        return new PostStatus($this->getValue('body.status'));
33
    }
34
35 3
    public function getRules(): array
36
    {
37
        return [
38
            'body.title' => [
39 3
                new Required(),
40 3
                (new HasLength())
41 3
                    ->min(5)
42 3
                    ->max(255),
43
            ],
44
            'body.text' => [
45 3
                new Required(),
46 3
                (new HasLength())
47 3
                    ->min(5)
48 3
                    ->max(1000),
49
            ],
50
            'body.status' => [
51 3
                new Required(),
52 3
                static function ($value): Result {
53 3
                    $result = new Result();
54 3
                    if (!PostStatus::isValid($value)) {
55 1
                        $result->addError('Incorrect status');
56
                    }
57 3
                    return $result;
58 3
                },
59
            ],
60
        ];
61
    }
62
}
63