1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Data\Persistence\Array_\Action;
6:
7: use Atk4\Data\Exception;
8:
9: /**
10: * @internal
11: *
12: * @phpstan-extends \IteratorIterator<int, array<string, mixed>, \Traversable<array<string, mixed>>>
13: */
14: final class RenameColumnIterator extends \IteratorIterator
15: {
16: /** @var string */
17: protected $origName;
18: /** @var string */
19: protected $newName;
20:
21: /**
22: * @param \Traversable<array<string, mixed>> $iterator
23: */
24: public function __construct(\Traversable $iterator, string $origName, string $newName)
25: {
26: parent::__construct($iterator);
27:
28: $this->origName = $origName;
29: $this->newName = $newName;
30: }
31:
32: #[\Override]
33: public function current(): array
34: {
35: $row = parent::current();
36:
37: $keys = array_keys($row);
38: $index = array_search($this->origName, $keys, true);
39: if ($index === false) {
40: throw (new Exception('Column not found'))
41: ->addMoreInfo('orig_name', $this->origName);
42: }
43: $keys[$index] = $this->newName;
44:
45: return array_combine($keys, $row);
46: }
47: }
48: