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