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

ETag   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 46
ccs 22
cts 22
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTag() 0 3 1
A withTag() 0 7 1
A setValue() 0 10 2
A __toString() 0 6 3
A isWeak() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Http\Header\Value\Condition;
6
7
use Yiisoft\Http\Header\Parser\ParsingException;
8
use Yiisoft\Http\Header\Internal\BaseHeaderValue;
9
10
/**
11
 * @link https://tools.ietf.org/html/rfc7232#section-2.3
12
 */
13
final class ETag extends BaseHeaderValue
14
{
15
    public const NAME = 'ETag';
16
17
    private string $tag = '';
18
    private bool $toStringFromTag = false;
19
    private bool $weak = true;
20
21 3
    public function __toString(): string
22
    {
23 3
        if ($this->toStringFromTag) {
24 1
            return ($this->weak ? 'W/' : '') . '"' . $this->tag . '"';
25
        } else {
26 2
            return $this->value;
27
        }
28
    }
29
30 9
    public function getTag(): string
31
    {
32 9
        return $this->tag;
33
    }
34
35 9
    public function isWeak(): bool
36
    {
37 9
        return $this->weak;
38
    }
39
40 4
    public function withTag(string $tag, bool $weak = true): self
41
    {
42 4
        $clone = clone $this;
43 4
        $clone->tag = $tag;
44 4
        $clone->weak = $weak;
45 4
        $clone->toStringFromTag = true;
46 4
        return $clone;
47
    }
48
49 12
    protected function setValue(string $value): void
50
    {
51 12
        $this->value = trim($value);
52 12
        $this->toStringFromTag = false;
53 12
        if (preg_match('/^(?<weak>W\\/)?"(?<etagc>[^"\\x00-\\x20\\x7F]+)"$/', $this->value, $matches) === 1) {
54 5
            $this->tag = $matches['etagc'];
55 5
            $this->weak = $matches['weak'] === 'W/';
56 5
            $this->error = null;
57
        } else {
58 12
            $this->error = new ParsingException($value, 0, 'Invalid ETag value format', 0, $this->error);
59
        }
60 12
    }
61
}
62