Skip to content

Commit e757085

Browse files
abnegateclaude
andcommitted
refactor(document): make the storage constructors final and internal
fromRow() and fromStorage() always build a plain Document with new self, so a subclass override could only diverge from that; Collection, whose constructor is now private, hydrates through Collection::fromArray() from the document-type registry instead. fromRow() skips nested-document detection because raw rows hold only scalars and encoded strings; callers building documents from resolved relationship trees or user input keep using the constructor. The internal key set reads Attribute::$key, the property the value object exposes once the getters are removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ccc18c8 commit e757085

1 file changed

Lines changed: 6 additions & 17 deletions

File tree

‎src/Database/Document.php‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ private static function getInternalKeySet(): array
5353
{
5454
if (self::$internalKeySet === null) {
5555
self::$internalKeySet = [];
56-
foreach (Database::internalAttributes() as $attr) {
57-
self::$internalKeySet[$attr->getKey()] = true;
56+
foreach (Database::internalAttributes() as $attribute) {
57+
self::$internalKeySet[$attribute->key] = true;
5858
}
5959
}
6060
return self::$internalKeySet;
@@ -120,21 +120,13 @@ public static function fromArray(array $data): self
120120
}
121121

122122
/**
123-
* Construct from a raw PDO row.
124-
*
125-
* Fast path that skips nested-Document detection. Raw PDO rows from
126-
* `$stmt->fetch()` only contain scalars or JSON-encoded strings — there
127-
* are never nested arrays carrying `$id`/`$collection`, so the
128-
* nested-detection foreach in the constructor is pure waste per row.
129-
*
130-
* Callers that build documents from relationship-resolved trees or
131-
* arbitrary user input must continue to use the regular constructor.
123+
* @internal
132124
*
133125
* @param array<int|string, mixed> $row
134126
*
135127
* @throws DatabaseException
136128
*/
137-
public static function fromRow(array $row): self
129+
final public static function fromRow(array $row): self
138130
{
139131
foreach (\array_keys($row) as $key) {
140132
if (\is_int($key)) {
@@ -170,16 +162,13 @@ public static function fromRow(array $row): self
170162
}
171163

172164
/**
173-
* Construct from a document read back from storage.
174-
*
175-
* Builds nested documents like the constructor, but drops non-string permissions instead of
176-
* rejecting them, as fromRow() does.
165+
* @internal
177166
*
178167
* @param array<string, mixed> $data
179168
*
180169
* @throws StructureException When $id is not a string or $permissions is not an array
181170
*/
182-
public static function fromStorage(array $data): self
171+
final public static function fromStorage(array $data): self
183172
{
184173
if (array_key_exists(self::ID, $data) && ! \is_string($data[self::ID])) {
185174
throw new StructureException(self::ID.' must be of type string');

0 commit comments

Comments
 (0)