Completed
Pull Request — master (#42)
by Frederik
03:39
created

NameSectionItem   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 56
c 0
b 0
f 0
ccs 0
cts 28
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A __toString() 0 11 1
A fromString() 0 9 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\MessageData;
5
6
final class NameSectionItem implements ItemInterface
7
{
8
9
    /**
10
     * @var string
11
     */
12
    private $name;
13
    /**
14
     * @var array
15
     */
16
    private $sections;
17
18
    /**
19
     * NameSectionItem constructor.
20
     * @param string $name
21
     * @param array $sections
22
     */
23
    public function __construct(string $name, array $sections = [])
24
    {
25
        $this->name = $name;
26
        $this->sections = $sections;
27
    }
28
29
    /**
30
     * @return string
31
     */
32
    public function getName(): string
33
    {
34
        return $this->name;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function __toString(): string
41
    {
42
        return sprintf(
43
            '%s[%s]',
44
            $this->name,
45
            implode(
46
                ' ',
47
                $this->sections
48
            )
49
        );
50
    }
51
52
    public static function fromString(string $nameSectionItem)
53
    {
54
        $result = preg_match('/^([A-Z]+)\[(.*?)\]$/', $nameSectionItem, $matches);
55
        if ($result !== 1) {
56
            throw new \InvalidArgumentException('Not a name section item');
57
        }
58
59
        return new self($matches[1], explode(' ', $matches[2]));
60
    }
61
}