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

BaseHeaderValue::inject()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Http\Header\Internal;
6
7
use Exception;
8
use Psr\Http\Message\MessageInterface;
9
use Yiisoft\Http\Header\Header;
10
use Yiisoft\Http\Header\Parser\HeaderParsingParams;
11
12
abstract class BaseHeaderValue
13
{
14
    public const NAME = null;
15
16
    protected string $value;
17
    protected ?Exception $error = null;
18
19
    protected const PARSING_LIST = false;
20
    protected const PARSING_Q_PARAM = false;
21
    protected const PARSING_PARAMS = false;
22
23 223
    public function __construct(string $value = '')
24
    {
25 223
        $this->setValue($value);
26 223
    }
27
28 12
    public function __toString(): string
29
    {
30 12
        return $this->value;
31
    }
32
33
    final public function inject(MessageInterface $message, bool $replace = true): MessageInterface
34
    {
35
        if (static::NAME === null) {
0 ignored issues
show
introduced by
The condition static::NAME === null is always true.
Loading history...
36
            throw new \RuntimeException('Can not inject unnamed header value');
37
        }
38
        if ($replace) {
39
            $message = $message->withoutHeader(static::NAME);
40
        }
41
        return $message->withAddedHeader(static::NAME, $this->__toString());
42
    }
43
44 1
    public static function createHeader(): Header
45
    {
46 1
        return new Header(static::class);
47
    }
48
49
    public static function extract(MessageInterface $message): Header
50
    {
51
        return (new Header(static::class))->extract($message);
52
    }
53
54 66
    public function withValue(string $value): self
55
    {
56 66
        $clone = clone $this;
57 66
        $clone->setValue($value);
58 66
        return $clone;
59
    }
60
61 84
    public function getValue(): string
62
    {
63 84
        return $this->value;
64
    }
65
66
    /**
67
     * @param Exception|null $error
68
     */
69 20
    final public function withError(?Exception $error): self
70
    {
71 20
        $clone = clone $this;
72 20
        $clone->error = $error;
73 20
        return $clone;
74
    }
75
76 167
    final public function hasError(): bool
77
    {
78 167
        return $this->error !== null;
79
    }
80
81
    final public function getError(): ?Exception
82
    {
83
        return $this->error;
84
    }
85
86 87
    final public static function getParsingParams(): HeaderParsingParams
87
    {
88 87
        $params = new HeaderParsingParams();
89 87
        $params->directives = is_subclass_of(static::class, DirectivesHeaderValue::class, true);
90 87
        $params->valuesList = $params->directives || static::PARSING_LIST;
91 87
        $params->withParams = $params->directives || static::PARSING_PARAMS;
92 87
        $params->q = static::PARSING_Q_PARAM;
93 87
        return $params;
94
    }
95
96 166
    protected function setValue(string $value): void
97
    {
98 166
        $this->value = $value;
99 166
    }
100
101 59
    final protected function encodeQuotedString(string $string): string
102
    {
103 59
        return preg_replace('/([\\\\"])/', '\\\\$1', $string);
104
    }
105
106 22
    final protected function validateDateTime(string $value): bool
107
    {
108 22
        return preg_match(
109
            '/^\\w{3,}, [0-3]?\\d[ \\-]\\w{3}[ \\-]\\d+ [0-2]\\d:[0-5]\\d:[0-5]\\d \\w+|'
110 22
                . '\\w{3} \\w{3} [0-3]?\\d [0-2]\\d:[0-5]\\d:[0-5]\\d \\d+$/i',
111 22
            trim($value)
112 22
        ) === 1;
113
    }
114
}
115