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

MatchStringCriterion::subject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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