Passed
Pull Request — master (#22)
by Aleksei
02:32
created

Age::setValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Http\Header\Value\Cache;
6
7
use Yiisoft\Http\Header\Parser\ParsingException;
8
use Yiisoft\Http\Header\Internal\BaseHeaderValue;
9
10
/**
11
 * @link https://tools.ietf.org/html/rfc7234#section-5.1
12
 */
13
final class Age extends BaseHeaderValue
14
{
15
    public const NAME = 'Age';
16
17
    /**
18
     * @param int|string $value
19
     * @return BaseHeaderValue
20
     */
21 18
    public function withValue($value): BaseHeaderValue
22
    {
23 18
        if (is_int($value)) {
24 3
            $value = (string)$value;
25
        }
26 18
        return parent::withValue($value);
27
    }
28
29 18
    protected function setValue(string $value): void
30
    {
31 18
        if (preg_match('/^\\d+$/', $value) !== 1) {
32 18
            $this->error = new ParsingException($value, 0, 'The value must consist of digits only.');
33
        } else {
34 6
            $this->error = null;
35
        }
36 18
        parent::setValue($value);
37 18
    }
38
}
39