1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Equip\Structure; |
4
|
|
|
|
5
|
|
|
use Equip\Structure\Traits\CanStructure; |
6
|
|
|
|
7
|
|
|
class Set implements SetInterface |
8
|
|
|
{ |
9
|
|
|
use CanStructure; |
10
|
|
|
|
11
|
5 |
|
public function hasValue($value) |
12
|
|
|
{ |
13
|
5 |
|
return in_array($value, $this->values, true); |
14
|
|
|
} |
15
|
|
|
|
16
|
4 |
|
public function withValues(array $values) |
17
|
|
|
{ |
18
|
4 |
|
$this->assertValid($values); |
19
|
|
|
|
20
|
3 |
|
$copy = clone $this; |
21
|
3 |
|
$copy->values = array_unique($values, SORT_REGULAR); |
22
|
|
|
|
23
|
3 |
|
return $copy; |
24
|
|
|
} |
25
|
|
|
|
26
|
3 |
View Code Duplication |
public function withValue($value) |
|
|
|
|
27
|
|
|
{ |
28
|
3 |
|
if ($this->hasValue($value)) { |
29
|
1 |
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
3 |
|
$this->assertValid([$value]); |
33
|
|
|
|
34
|
3 |
|
$copy = clone $this; |
35
|
3 |
|
$copy->values[] = $value; |
36
|
|
|
|
37
|
3 |
|
return $copy; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
View Code Duplication |
public function withoutValue($value) |
|
|
|
|
41
|
|
|
{ |
42
|
1 |
|
$key = array_search($value, $this->values, true); |
43
|
|
|
|
44
|
1 |
|
if ($key === false) { |
45
|
1 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$copy = clone $this; |
49
|
1 |
|
unset($copy->values[$key]); |
50
|
|
|
|
51
|
1 |
|
return $copy; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
View Code Duplication |
public function withValueAfter($value, $search) |
|
|
|
|
55
|
|
|
{ |
56
|
1 |
|
if ($this->hasValue($value)) { |
57
|
1 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$this->assertValid([$value]); |
61
|
|
|
|
62
|
1 |
|
$copy = clone $this; |
63
|
|
|
|
64
|
1 |
|
$key = array_search($search, $this->values); |
65
|
1 |
|
if ($key === false) { |
66
|
1 |
|
array_push($copy->values, $value); |
67
|
1 |
|
} else { |
68
|
1 |
|
array_splice($copy->values, $key + 1, 0, $value); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $copy; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
View Code Duplication |
public function withValueBefore($value, $search) |
|
|
|
|
75
|
|
|
{ |
76
|
1 |
|
if ($this->hasValue($value)) { |
77
|
1 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
$this->assertValid([$value]); |
81
|
|
|
|
82
|
1 |
|
$copy = clone $this; |
83
|
|
|
|
84
|
1 |
|
$key = array_search($search, $this->values); |
85
|
1 |
|
if ($key === false) { |
86
|
1 |
|
array_unshift($copy->values, $value); |
87
|
1 |
|
} else { |
88
|
1 |
|
array_splice($copy->values, $key, 0, $value); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return $copy; |
92
|
|
|
} |
93
|
|
|
|
94
|
9 |
|
protected function assertValid(array $values) |
95
|
|
|
{ |
96
|
9 |
|
if (empty($values)) { |
97
|
3 |
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
9 |
|
if ($values !== array_values($values)) { |
101
|
1 |
|
throw ValidationException::invalid( |
102
|
|
|
'Set structures cannot have distinct keys' |
103
|
1 |
|
); |
104
|
|
|
} |
105
|
9 |
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.