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