Skip to content

Commit 0c78f5b

Browse files
committed
decode v2
1 parent 74e9df8 commit 0c78f5b

File tree

1 file changed

+107
-24
lines changed

1 file changed

+107
-24
lines changed

src/Database/Database.php

Lines changed: 107 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8254,8 +8254,6 @@ public function decode(QueryContext $context, Document $document, array $selects
82548254
}
82558255
}
82568256

8257-
$new = $this->createDocumentInstance($context->getMainCollection()->getId(), []);
8258-
82598257
foreach ($document as $key => $value) {
82608258
$alias = Query::DEFAULT_ALIAS;
82618259
$attributeKey = '';
@@ -8265,13 +8263,12 @@ public function decode(QueryContext $context, Document $document, array $selects
82658263
$attributeKey = $key;
82668264
$key = $select->getAttribute();
82678265
$alias = $select->getAlias();
8268-
82698266
break;
82708267
}
82718268

8272-
if ($select->getAttribute() == $key || $this->adapter->filter($select->getAttribute()) == $key) {
8269+
if ($select->getAttribute() == $key ||
8270+
$this->adapter->filter($select->getAttribute()) == $key) {
82738271
$alias = $select->getAlias();
8274-
82758272
break;
82768273
}
82778274
}
@@ -8281,51 +8278,137 @@ public function decode(QueryContext $context, Document $document, array $selects
82818278
throw new \Exception('Invalid query: Unknown Alias context');
82828279
}
82838280

8284-
$attribute = $internals[$key] ?? null;
8281+
$attribute = $internals[$key]
8282+
?? $schema[$collection->getId()][$this->adapter->filter($key)]
8283+
?? null;
82858284

8286-
if (is_null($attribute)) {
8287-
$attribute = $schema[$collection->getId()][$this->adapter->filter($key)] ?? null;
8288-
}
8289-
8290-
if (is_null($attribute)) {
8285+
if ($attribute === null) {
82918286
if (!$this->adapter->getSupportForAttributes()) {
8292-
$new->setAttribute($key, $value); /** Schemaless */
8287+
$document->setAttribute($key, $value); // schemaless
82938288
}
8294-
82958289
continue;
82968290
}
82978291

82988292
if (empty($attributeKey)) {
82998293
$attributeKey = $attribute['$id'];
83008294
}
83018295

8302-
$array = $attribute['array'] ?? false;
8296+
$array = $attribute['array'] ?? false;
83038297
$filters = $attribute['filters'] ?? [];
83048298

8305-
// Skip decoding for Operator objects (shouldn't happen, but safety check)
8299+
// Skip decoding for Operator objects
83068300
if ($value instanceof Operator) {
83078301
continue;
83088302
}
83098303

8310-
$value = ($array) ? $value : [$value];
8311-
$value = (is_null($value)) ? [] : $value;
8304+
$value = $array ? $value : [$value];
8305+
$value = is_null($value) ? [] : $value;
83128306

83138307
foreach ($value as $index => $node) {
8314-
foreach (\array_reverse($filters) as $filter) {
8315-
$node = $this->decodeAttribute($filter, $node, $new, $key);
8308+
foreach (array_reverse($filters) as $filter) {
8309+
$node = $this->decodeAttribute($filter, $node, $document, $key);
83168310
}
8317-
83188311
$value[$index] = $node;
83198312
}
83208313

8321-
$value = ($array) ? $value : $value[0];
8322-
8323-
$new->setAttribute($attributeKey, $value);
8314+
$document->setAttribute(
8315+
$attributeKey,
8316+
$array ? $value : ($value[0] ?? null)
8317+
);
83248318
}
83258319

8326-
return $new;
8320+
return $document;
83278321
}
83288322

8323+
// public function decode(QueryContext $context, Document $document, array $selects = []): Document
8324+
// {
8325+
// $internals = [];
8326+
// $schema = [];
8327+
//
8328+
// foreach (Database::INTERNAL_ATTRIBUTES as $attribute) {
8329+
// $internals[$attribute['$id']] = $attribute;
8330+
// }
8331+
//
8332+
// foreach ($context->getCollections() as $collection) {
8333+
// foreach ($collection->getAttribute('attributes', []) as $attribute) {
8334+
// $key = $attribute->getAttribute('key', $attribute->getAttribute('$id'));
8335+
// $key = $this->adapter->filter($key);
8336+
// $schema[$collection->getId()][$key] = $attribute->getArrayCopy();
8337+
// }
8338+
// }
8339+
//
8340+
// $new = $this->createDocumentInstance($context->getMainCollection()->getId(), []);
8341+
//
8342+
// foreach ($document as $key => $value) {
8343+
// $alias = Query::DEFAULT_ALIAS;
8344+
// $attributeKey = '';
8345+
//
8346+
// foreach ($selects as $select) {
8347+
// if ($select->getAs() === $key) {
8348+
// $attributeKey = $key;
8349+
// $key = $select->getAttribute();
8350+
// $alias = $select->getAlias();
8351+
//
8352+
// break;
8353+
// }
8354+
//
8355+
// if ($select->getAttribute() == $key || $this->adapter->filter($select->getAttribute()) == $key) {
8356+
// $alias = $select->getAlias();
8357+
//
8358+
// break;
8359+
// }
8360+
// }
8361+
//
8362+
// $collection = $context->getCollectionByAlias($alias);
8363+
// if ($collection->isEmpty()) {
8364+
// throw new \Exception('Invalid query: Unknown Alias context');
8365+
// }
8366+
//
8367+
// $attribute = $internals[$key] ?? null;
8368+
//
8369+
// if (is_null($attribute)) {
8370+
// $attribute = $schema[$collection->getId()][$this->adapter->filter($key)] ?? null;
8371+
// }
8372+
//
8373+
// if (is_null($attribute)) {
8374+
// if (!$this->adapter->getSupportForAttributes()) {
8375+
// $new->setAttribute($key, $value); /** Schemaless */
8376+
// }
8377+
//
8378+
// continue;
8379+
// }
8380+
//
8381+
// if (empty($attributeKey)) {
8382+
// $attributeKey = $attribute['$id'];
8383+
// }
8384+
//
8385+
// $array = $attribute['array'] ?? false;
8386+
// $filters = $attribute['filters'] ?? [];
8387+
//
8388+
// // Skip decoding for Operator objects (shouldn't happen, but safety check)
8389+
// if ($value instanceof Operator) {
8390+
// continue;
8391+
// }
8392+
//
8393+
// $value = ($array) ? $value : [$value];
8394+
// $value = (is_null($value)) ? [] : $value;
8395+
//
8396+
// foreach ($value as $index => $node) {
8397+
// foreach (\array_reverse($filters) as $filter) {
8398+
// $node = $this->decodeAttribute($filter, $node, $new, $key);
8399+
// }
8400+
//
8401+
// $value[$index] = $node;
8402+
// }
8403+
//
8404+
// $value = ($array) ? $value : $value[0];
8405+
//
8406+
// $new->setAttribute($attributeKey, $value);
8407+
// }
8408+
//
8409+
// return $new;
8410+
// }
8411+
83298412
/**
83308413
* Casting
83318414
*

0 commit comments

Comments
 (0)