Completed
Pull Request — master (#42)
by Frederik
02:26
created

MatchContentCriterion::body()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\Request\SearchCriteria;
5
6
final class MatchContentCriterion implements CriterionInterface
7
{
8
9
    /**
10
     * @var string
11
     */
12
    private $query;
13
    /**
14
     * @var string
15
     */
16
    private $name;
17
18
    /**
19
     * BodyCriterium constructor.
20
     * @param string $name
21
     * @param string $query
22
     */
23 17
    private function __construct(string $name, string $query)
24
    {
25 17
        if ($query === '' || strlen($query) !== strcspn($query, "\r\n")) {
26 3
            throw new \InvalidArgumentException('CR and LF are not allowed in quoted strings');
27
        }
28
29 14
        $this->name = $name;
30 14
        $this->query = $query;
31 14
    }
32
33
    /**
34
     * @return string
35
     */
36 10
    public function __toString(): string
37
    {
38 10
        return sprintf(
39 10
            '%s "%s"',
40 10
            $this->name,
41 10
            addslashes($this->query)
42
        );
43
    }
44
45
    /**
46
     * @param string $query
47
     * @return MatchContentCriterion
48
     */
49 6
    public static function body(string $query): self
50
    {
51 6
        return new self('BODY', $query);
52
    }
53
54
    /**
55
     * @param string $query
56
     * @return MatchContentCriterion
57
     */
58 10
    public static function text(string $query): self
59
    {
60 10
        return new self('TEXT', $query);
61
    }
62
63
    /**
64
     * @param string $query
65
     * @return MatchContentCriterion
66
     */
67 1
    public static function subject(string $query): self
68
    {
69 1
        return new self('SUBJECT', $query);
70
    }
71
}