Completed
Pull Request — master (#76)
by Frederik
01:38
created

FlagsItem::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\MessageData\Item;
5
6
use Genkgo\Mail\Protocol\Imap\FlagParenthesizedList;
7
use Genkgo\Mail\Protocol\Imap\MessageData\ItemInterface;
8
9
final class FlagsItem implements ItemInterface
10
{
11
    public const OPERATOR_REPLACE = '';
12
    
13
    public const OPERATOR_ADD = '+';
14
    
15
    public const OPERATOR_REMOVE = '-';
16
    
17
    private const OPERATORS = [
18
        self::OPERATOR_REPLACE => true,
19
        self::OPERATOR_ADD => true,
20
        self::OPERATOR_REMOVE => true,
21
    ];
22
23
    /**
24
     * @var FlagParenthesizedList
25
     */
26
    private $flagList;
27
28
    /**
29
     * @var bool
30
     */
31
    private $silent = false;
32
33
    /**
34
     * @var string
35
     */
36
    private $operator = '';
37
38
    /**
39
     * @param FlagParenthesizedList $flagList
40
     * @param string $operator
41
     */
42 9
    public function __construct(FlagParenthesizedList $flagList, string $operator = '')
43
    {
44 9
        if (!isset(self::OPERATORS[$operator])) {
45 1
            throw new \InvalidArgumentException('Invalid operator');
46
        }
47
48 8
        $this->flagList = $flagList;
49 8
        $this->operator = $operator;
50 8
    }
51
52
    /**
53
     * @return string
54
     */
55 8
    public function getName(): string
56
    {
57 8
        return $this->operator . ($this->silent ? 'FLAGS.SILENT' : 'FLAGS');
58
    }
59
60
    /**
61
     * @return string
62
     */
63 7
    public function __toString(): string
64
    {
65 7
        return \sprintf(
66 7
            '%s %s',
67 7
            $this->getName(),
68 7
            (string)$this->flagList
69
        );
70
    }
71
72
    /**
73
     * @param FlagParenthesizedList $flagParenthesizedList
74
     * @param string $operator
75
     * @return FlagsItem
76
     */
77 3
    public static function silent(FlagParenthesizedList $flagParenthesizedList, string $operator = ''): self
78
    {
79 3
        $bodySection = new self($flagParenthesizedList, $operator);
80 3
        $bodySection->silent = true;
81 3
        return $bodySection;
82
    }
83
84
    /**
85
     * @param string $list
86
     * @return FlagsItem
87
     */
88 6
    public static function fromString(string $list): self
89
    {
90 6
        if ($list === '') {
91 1
            throw new \InvalidArgumentException('Cannot parse empty string');
92
        }
93
94 5
        $silent = false;
95 5
        $operator = self::OPERATOR_REPLACE;
96 5
        $flagStringList = [];
97
98 5
        if (isset(self::OPERATORS[$list[0]])) {
99 1
            $operator = $list[0];
100 1
            $list = \substr($list, 1);
101
        }
102
103 5
        $index = 0;
104 5
        $sequence = '';
105
106 5
        while (isset($list[$index])) {
107 5
            $char = $list[$index];
108 5
            $sequence .= $char;
109
110 5
            switch ($char) {
111 5
                case ' ':
112 5
                    $sequence = \trim($sequence);
113
114 5
                    if ($sequence === 'FLAGS.SILENT') {
115 2
                        $silent = true;
116 3
                    } elseif ($sequence !== 'FLAGS') {
117 1
                        throw new \UnexpectedValueException('Only expecting FLAGS or FLAGS.SILENT');
118
                    }
119
120 4
                    $sequence = '';
121 4
                    break;
122 5
                case '(':
123 4
                    $flagStringList = \explode(' ', \substr($list, $index + 1, -1));
124 4
                    break 2;
125
            }
126
127 5
            $index++;
128
        }
129
130 4
        if ($silent) {
131 2
            return self::silent(new FlagParenthesizedList($flagStringList), $operator);
132
        }
133
134 2
        return new self(new FlagParenthesizedList($flagStringList), $operator);
135
    }
136
}
137