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

Age::withValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
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
     *
20
     * @return BaseHeaderValue
21
     */
22 18
    public function withValue($value): BaseHeaderValue
23
    {
24 18
        if (is_int($value)) {
25 3
            $value = (string)$value;
26
        }
27 18
        return parent::withValue($value);
28
    }
29
30 18
    protected function setValue(string $value): void
31
    {
32 18
        if (preg_match('/^\\d+$/', $value) !== 1) {
33 18
            $this->error = new ParsingException($value, 0, 'The value must consist of digits only.');
34
        } else {
35 6
            $this->error = null;
36
        }
37 18
        parent::setValue($value);
38 18
    }
39
}
40