Completed
Pull Request — master (#77)
by Frederik
01:44
created

SequenceSet::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 2
cts 2
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\Request;
5
6
final class SequenceSet
7
{
8
    /**
9
     * @var array<int, mixed>
10
     */
11
    private $set = [];
12
    
13 15
    private function __construct()
14
    {
15
        // this constructor is protected in order to force
16
        // a set to have at least one item
17 15
    }
18
19
    /**
20
     * @param int $number
21
     * @return SequenceSet
22
     */
23 1
    public function withSingle(int $number): self
24
    {
25 1
        $set = clone $this;
26 1
        $set->set[] = $number;
27 1
        return $set;
28
    }
29
30
    /**
31
     * @param int $first
32
     * @param int $last
33
     * @return SequenceSet
34
     */
35 1
    public function withRange(int $first, int $last): self
36
    {
37 1
        $set = clone $this;
38 1
        $set->set[] = (string)$first.':'.(string)$last;
39 1
        return $set;
40
    }
41
42
    /**
43
     * @param int $first
44
     * @return SequenceSet
45
     */
46 1
    public function withInfiniteRange(int $first): self
47
    {
48 1
        $set = clone $this;
49 1
        $set->set[] = (string)$first.':*';
50 1
        return $set;
51
    }
52
53
    /**
54
     * @return string
55
     */
56 15
    public function __toString(): string
57
    {
58 15
        return \implode(',', $this->set);
59
    }
60
61
    /**
62
     * @return SequenceSet
63
     */
64 1
    public static function all(): self
65
    {
66 1
        $set = new self();
67 1
        $set->set = ['*'];
68 1
        return $set;
69
    }
70
71
    /**
72
     * @param int $number
73
     * @return SequenceSet
74
     */
75 10
    public static function single(int $number): self
76
    {
77 10
        $set = new self();
78 10
        $set->set = [(string)$number];
79 10
        return $set;
80
    }
81
82
    /**
83
     * @param array<int, int> $numbers
84
     * @return SequenceSet
85
     */
86
    public static function multiple(array $numbers): self
87
    {
88
        $set = new self();
89
        $set->set = \array_map('strval', $numbers);
90
        return $set;
91
    }
92
93
    /**
94
     * @param int $first
95
     * @param int $last
96
     * @return SequenceSet
97
     */
98 3
    public static function range(int $first, int $last): self
99
    {
100 3
        $set = new self();
101 3
        $set->set = [(string)$first.':'.(string)$last];
102 3
        return $set;
103
    }
104
105
    /**
106
     * @param int $first
107
     * @return SequenceSet
108
     */
109 1
    public static function infiniteRange(int $first): self
110
    {
111 1
        $set = new self();
112 1
        $set->set = [(string)$first.':*'];
113 1
        return $set;
114
    }
115
}
116