Completed
Pull Request — master (#42)
by Frederik
04:15 queued 01:07
created

HeaderCriterion::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\Request\SearchCriteria;
5
6
use Genkgo\Mail\Header\HeaderName;
7
8
final class HeaderCriterion implements CriterionInterface
9
{
10
11
    /**
12
     * @var HeaderName
13
     */
14
    private $headerName;
15
    /**
16
     * @var string
17
     */
18
    private $query;
19
20
    /**
21
     * HeaderCriterium constructor.
22
     * @param HeaderName $headerName
23
     * @param string $query
24
     */
25 5
    public function __construct(HeaderName $headerName, string $query)
26
    {
27 5
        if ($query === '' || strlen($query) !== strcspn($query, "\r\n")) {
28 3
            throw new \InvalidArgumentException('CR and LF are not allowed in quoted strings');
29
        }
30
31 2
        $this->headerName = $headerName;
32 2
        $this->query = $query;
33 2
    }
34
35
    /**
36
     * @return string
37
     */
38 2
    public function __toString(): string
39
    {
40 2
        return sprintf(
41 2
            'HEADER %s "%s"',
42 2
            (string)$this->headerName,
43 2
            addslashes($this->query)
44
        );
45
    }
46
}