1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Image; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use IteratorAggregate; |
7
|
|
|
|
8
|
|
|
class ManipulationSequence implements IteratorAggregate |
9
|
|
|
{ |
10
|
|
|
/** @var array */ |
11
|
|
|
protected $groups = []; |
12
|
|
|
|
13
|
|
|
public function __construct(array $sequenceArray = []) |
14
|
|
|
{ |
15
|
|
|
$this->startNewGroup(); |
16
|
|
|
$this->mergeArray($sequenceArray); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $operation |
21
|
|
|
* @param string $argument |
22
|
|
|
* |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
|
|
public function addManipulation(string $operation, string $argument) |
26
|
|
|
{ |
27
|
|
|
$lastIndex = count($this->groups) - 1; |
28
|
|
|
|
29
|
|
|
$this->groups[$lastIndex][$operation] = $argument; |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param \Spatie\Image\ManipulationSequence $sequence |
36
|
|
|
* |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function merge(ManipulationSequence $sequence) |
40
|
|
|
{ |
41
|
|
|
$sequenceArray = $sequence->toArray(); |
42
|
|
|
|
43
|
|
|
$this->mergeArray($sequenceArray); |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function mergeArray(array $sequenceArray) |
49
|
|
|
{ |
50
|
|
|
foreach ($sequenceArray as $group) { |
51
|
|
|
foreach ($group as $name => $argument) { |
52
|
|
|
$this->addManipulation($name, $argument); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (next($sequenceArray)) { |
56
|
|
|
$this->startNewGroup(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function startNewGroup() |
65
|
|
|
{ |
66
|
|
|
$this->groups[] = []; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function toArray(): array |
72
|
|
|
{ |
73
|
|
|
return $this->getGroups(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getGroups(): array |
77
|
|
|
{ |
78
|
|
|
return $this->sanitizeManipulationSets($this->groups); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getIterator(): ArrayIterator |
82
|
|
|
{ |
83
|
|
|
return new ArrayIterator($this->toArray()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $manipulationName |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function removeManipulation(string $manipulationName) |
92
|
|
|
{ |
93
|
|
|
foreach ($this->groups as &$group) { |
94
|
|
|
if (array_key_exists($manipulationName, $group)) { |
95
|
|
|
unset($group[$manipulationName]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function isEmpty(): bool |
103
|
|
|
{ |
104
|
|
|
if (count($this->groups) > 1) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (count($this->groups[0]) > 0) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function sanitizeManipulationSets(array $groups): array |
116
|
|
|
{ |
117
|
|
|
return array_values(array_filter($groups, function (array $manipulationSet) { |
118
|
|
|
return count($manipulationSet); |
119
|
|
|
})); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/* |
123
|
|
|
* Determine if the sequences contain a manipulation with the given name. |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function getFirstManipulationArgument($searchManipulationName) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
foreach ($this->groups as $group) { |
128
|
|
|
foreach ($group as $name => $argument) { |
129
|
|
|
if ($name === $searchManipulationName) { |
130
|
|
|
return $argument; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/* |
138
|
|
|
* Determine if the sequences contain a manipulation with the given name. |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function contains($searchManipulationName) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
foreach ($this->groups as $group) { |
143
|
|
|
foreach ($group as $name => $argument) { |
144
|
|
|
if ($name === $searchManipulationName) { |
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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.