Completed
Pull Request — master (#42)
by Frederik
01:50
created

HeaderCriterion   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 39
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A __toString() 0 8 1
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
    public function __construct(HeaderName $headerName, string $query)
26
    {
27
        if ($query === '' || strlen($query) !== strcspn($query, "\r\n")) {
28
            throw new \InvalidArgumentException('CR and LF are not allowed in quoted strings');
29
        }
30
31
        $this->headerName = $headerName;
32
        $this->query = $query;
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function __toString(): string
39
    {
40
        return sprintf(
41
            'HEADER %s %s',
42
            (string)$this->headerName,
43
            addslashes($this->query)
44
        );
45
    }
46
}