RegularParameter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\Http\Header\Value\ContentDisposition;
6
7
use InvalidArgumentException;
8
use Stadly\Http\Utilities\Rfc2616;
9
use Stadly\Http\Utilities\Rfc6266;
10
11
/**
12
 * Class for handling regular content disposition parameters.
13
 *
14
 * Specification: https://tools.ietf.org/html/rfc6266#section-4.1
15
 */
16
final class RegularParameter extends Parameter
17
{
18
    /**
19
     * Constructor.
20
     *
21
     * @param string $name Name. Usually `filename`.
22
     * @param string $value Value.
23
     */
24 6
    public function __construct(string $name, string $value)
25
    {
26 6
        $this->setName($name);
27 4
        $this->setValue($value);
28
    }
29
30
    /**
31
     * Construct parameter from string.
32
     *
33
     * @param string $parameter Parameter string.
34
     * @return self Parameter generated based on the string.
35
     */
36 10
    public static function fromString(string $parameter): self
37
    {
38 10
        $regEx = '{^' . Rfc6266::DISPOSITION_PARM_REGULAR_CAPTURE . '$}';
39 10
        $plainParameter = mb_convert_encoding($parameter, 'ISO-8859-1', 'UTF-8');
40 10
        if ($plainParameter !== $parameter || preg_match($regEx, $parameter, $matches) !== 1) {
41 6
            throw new InvalidArgumentException('Invalid parameter: ' . $parameter);
42
        }
43
44 4
        $value = $matches['VALUE'];
45
        // Strip slashes if value is quoted.
46 4
        if (mb_strlen($value) >= 2 && mb_substr($value, 0, 1) === '"' && mb_substr($value, -1) === '"') {
47 3
            $value = stripslashes(mb_substr($value, 1, -1));
48
        }
49
50 4
        return new self($matches['NAME'], $value);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56 2
    public function __toString(): string
57
    {
58 2
        return $this->name . '=' . self::encodeValue($this->value);
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64 3
    public function setName(string $name): void
65
    {
66 3
        $plainName = mb_convert_encoding($name, 'ISO-8859-1', 'UTF-8');
67 3
        if ($plainName !== $name || preg_match('{^' . Rfc2616::TOKEN . '$}', $name) !== 1) {
68 2
            throw new InvalidArgumentException('Invalid name: ' . $name);
69
        }
70
71 3
        $this->name = $name;
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77 3
    public function setValue(string $value): void
78
    {
79 3
        $encodedValue = self::encodeValue($value);
80
81 3
        $regEx = '{^' . Rfc2616::VALUE . '$}';
82 3
        $plainValue = mb_convert_encoding($encodedValue, 'ISO-8859-1', 'UTF-8');
83 3
        if ($plainValue !== $encodedValue || preg_match($regEx, $encodedValue) !== 1) {
84 1
            throw new InvalidArgumentException('Invalid value: ' . $value);
85
        }
86
87 3
        $this->value = $value;
88
    }
89
90
    /**
91
     * @param string $value Value.
92
     * @return string Encoded value.
93
     */
94 18
    private static function encodeValue(string $value): string
95
    {
96 18
        if (preg_match('{^' . Rfc2616::TOKEN . '$}', $value) === 1) {
97 11
            return $value;
98
        }
99
100
        // If value is not a valid token, turn it into a quoted token.
101 9
        return '"' . addslashes($value) . '"';
102
    }
103
}
104