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
|
|
|
|