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

Flag::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap;
5
6
/**
7
 * Class Flag
8
 * @package Genkgo\Mail\Protocol\Imap
9
 */
10
final class Flag
11
{
12
    /**
13
     *
14
     */
15
    private CONST RFC_3501_FIXED = [
16
        '\\Answered' => true,
17
        '\\Flagged' => true,
18
        '\\Deleted' => true,
19
        '\\Seen' => true,
20
        '\\Draft' => true,
21
    ];
22
23
    /**
24
     *
25
     */
26
    private CONST RFC_3501_ATOM = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x20\x1A\x1B\x1C\x1D\x1E\x1F\x22\x25\x28\x29\x2A\x5C\x5D\x7B\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF";
27
28
    /**
29
     * @var string
30
     */
31
    private $flag;
32
33
    /**
34
     * Flag constructor.
35
     * @param string $flag
36
     */
37 22
    public function __construct(string $flag)
38
    {
39 22
        $this->validate($flag);
40
41 17
        $this->flag = $flag;
42 17
    }
43
44
    /**
45
     * @param Flag $other
46
     * @return bool
47
     */
48 1
    public function equals(self $other): bool
49
    {
50 1
        return $this->flag === $other->flag;
51
    }
52
53
    /**
54
     * @return bool
55
     */
56 5
    public function isKeyword(): bool
57
    {
58 5
        return $this->flag[0] !== '\\';
59
    }
60
61
    /**
62
     * @return string
63
     */
64 12
    public function __toString():string
65
    {
66 12
        return $this->flag;
67
    }
68
69
    /**
70
     * @param string $flag
71
     */
72 22
    private function validate(string $flag): void
73
    {
74 22
        if ($flag === '') {
75 1
            throw new \InvalidArgumentException('Flag string is empty');
76
        }
77
78 21
        if (isset(self::RFC_3501_FIXED[$flag])) {
79 12
            return;
80
        }
81
82 10
        if ($flag[0] === '\\') {
83 3
            $this->validateAtom(substr($flag, 1));
84
        } else {
85 7
            $this->validateAtom($flag);
86
        }
87 6
    }
88
89
    /**
90
     * @param string $flag
91
     */
92 10
    private function validateAtom(string $flag): void
93
    {
94 10
        if (strcspn($flag, self::RFC_3501_ATOM) !== strlen($flag)) {
95 4
            throw new \InvalidArgumentException(
96 4
                'You are not allowed to use any atom-specials within a flag'
97
            );
98
        }
99
    }
100
}