1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Data\Persistence\Array_\Db;
6:
7: class Row
8: {
9: /** @var int */
10: private static $nextRowIndex = -1;
11:
12: /** @var Table Immutable */
13: private $owner;
14: /** @var int Immutable */
15: private $rowIndex;
16: /** @var array<string, mixed> */
17: private $data = [];
18:
19: public function __construct(Table $owner)
20: {
21: $this->owner = $owner;
22: $this->rowIndex = self::getNextRowIndex();
23: }
24:
25: public static function getNextRowIndex(): int
26: {
27: return ++self::$nextRowIndex;
28: }
29:
30: /**
31: * @return array<string, mixed>
32: */
33: public function __debugInfo(): array
34: {
35: return [
36: 'row_index' => $this->getRowIndex(),
37: 'data' => $this->getData(),
38: ];
39: }
40:
41: public function getOwner(): Table
42: {
43: return $this->owner;
44: }
45:
46: public function getRowIndex(): int
47: {
48: return $this->rowIndex;
49: }
50:
51: /**
52: * @return mixed
53: */
54: public function getValue(string $columnName)
55: {
56: return $this->data[$columnName];
57: }
58:
59: /**
60: * @return array<string, mixed>
61: */
62: public function getData(): array
63: {
64: return $this->data;
65: }
66:
67: protected function initValue(string $columnName): void
68: {
69: $this->data[$columnName] = null;
70: }
71:
72: /**
73: * @param array<string, mixed> $data
74: */
75: public function updateValues(array $data): void
76: {
77: $owner = $this->getOwner();
78:
79: $newData = [];
80: foreach ($data as $columnName => $newValue) {
81: $owner->assertHasColumnName($columnName);
82: if ($newValue !== $this->data[$columnName]) {
83: $newData[$columnName] = $newValue;
84: }
85: }
86:
87: $that = $this;
88: \Closure::bind(static function () use ($owner, $that, $newData) {
89: $owner->beforeValuesSet($that, $newData);
90: }, null, $owner)();
91:
92: foreach ($newData as $columnName => $newValue) {
93: $this->data[$columnName] = $newValue;
94: }
95: }
96:
97: protected function beforeDelete(): void
98: {
99: $this->updateValues(array_map(static function () {
100: return null;
101: }, $this->data));
102:
103: $this->owner = null; // @phpstan-ignore-line
104: }
105: }
106: