Skip to content
Snippets Groups Projects
Commit 98a3209f authored by mindaugasd's avatar mindaugasd
Browse files

Issue #3441127 by mindaugasd: Use NestedArray utility instead of custom solution

parent 29a8cf02
No related branches found
No related tags found
No related merge requests found
Pipeline #148988 passed
......@@ -150,7 +150,7 @@ class ExampleBackend2 extends AIChatBackendBase {
* {@inheritdoc}
*/
public function loadMessages(): void {
$messages_array = $this->aichat->getDataValue('messages');
$messages_array = $this->aichat->getDataNestedValue('messages');
if (empty($messages_array)) return;
......@@ -173,7 +173,7 @@ class ExampleBackend2 extends AIChatBackendBase {
$message->setId($uuid);
}
$this->aichat->setDataSubValue('messages', $uuid, $message->toArray());
$this->aichat->setDataNestedValue(['messages', $uuid], $message->toArray());
$this->aichat->save();
}
......
......@@ -10,60 +10,35 @@ use Drupal\Core\Entity\ContentEntityInterface;
* @ingroup aichat
*/
interface AIChatInterface extends ContentEntityInterface {
/**
* Gets a value from conversation data.
*
* @param string $key
* The key of the data to retrieve.
*
* @return mixed
* The data value for the requested key. NULL if the key does not exist.
*/
public function getDataValue(string $key): mixed;
/**
* Gets a value from nested conversation data.
*
* @param string $key
* The key of the data to retrieve.
* @param string $subkey
* The subkey of the data to retrieve.
*
*
* @param mixed $key
* If $key is a string, it will return configuration value by this key.
* If $key is an array, each element of the array will be used as a nested key starting with the outermost key.
* For example, $fieldname = ['foo', 'bar'] will return $configuration['foo']['bar'].
*
* @return mixed
* The data subvalue for the requested key and subkey. NULL if the key or subkey does not exist.
*/
public function getDataSubValue(string $key, string $subkey): mixed;
/**
* Sets a value into conversation data.
*
* @param string $key
* The key of the data to set.
* @param mixed $value
* The value to be set.
* @param bool $save
* If the setting of the value should run saveData() method
* The data value for the requested key. NULL if the key does not exist.
*/
public function setDataValue(string $key, $value, $save = TRUE): void;
public function getDataNestedValue(mixed $key): mixed;
/**
* Sets a value into nested conversation data.
*
* @param string $key
* The key of the data to set.
* @param string $subkey
* The subkey of the data to set.
*
* @param mixed $key
* An array of parent keys, starting with the outermost key. Or single key string.
* @param mixed $value
* The value to be set.
* The data value to set. If NULL is provided, key will be unset.
* @param bool $save
* If the setting of the value should run saveData() method
* If should run saveData() method
*/
public function setDataSubValue(string $key, string $subkey, $value, bool $save = TRUE): void;
public function setDataNestedValue(mixed $key, mixed $value, bool $save): void;
/**
* Set data field to entity.
*/
public function saveData(): void;
}
\ No newline at end of file
}
......@@ -2,17 +2,18 @@
namespace Drupal\aichat\Entity;
use Drupal\aichat\AIChatInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\user\EntityOwnerInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\EntityOwnerTrait;
use Drupal\aichat\AIChatInterface;
/**
* AI chat conversation entity.
......@@ -165,25 +166,18 @@ class AIChat extends ContentEntityBase implements EntityChangedInterface, Entity
/**
* {@inheritdoc}
*/
public function getDataValue(string $key): mixed {
return $this->data_cache[$key] ?? NULL;
}
/**
* {@inheritdoc}
*/
public function getDataSubValue(string $key, string $subkey): mixed {
return $this->data_cache[$key][$subkey] ?? NULL;
public function getDataNestedValue(mixed $key): mixed {
return NestedArray::getValue($this->data_cache, (array) $key);
}
/**
* {@inheritdoc}
*/
public function setDataValue(string $key, $val, $save = TRUE): void {
$this->data_cache[$key] = $val;
public function setDataNestedValue(mixed $key, mixed $value, bool $save = TRUE): void {
NestedArray::setValue($this->data_cache, (array) $key, $value, TRUE);
if ($val === NULL) {
unset($this->data_cache[$key]);
if ($value === NULL) {
NestedArray::unsetValue($this->data_cache, $key);
}
if ($save) {
......@@ -192,28 +186,38 @@ class AIChat extends ContentEntityBase implements EntityChangedInterface, Entity
}
/**
* {@inheritdoc}
* @deprecated, use new getDataNestedValue()
*/
public function setDataSubValue(string $key, string $subkey, $val, bool $save = TRUE): void {
public function getDataValue(string $key): mixed {
return $this->getDataNestedValue($key);
}
if ($val === NULL) {
if (isset($this->data_cache[$key][$subkey])) {
unset($this->data_cache[$key][$subkey]);
}
} else {
$this->data_cache[$key][$subkey] = $val;
}
/**
* @deprecated, use new getDataNestedValue()
*/
public function getDataSubValue(string $key, string $subkey): mixed {
return $this->getDataNestedValue([$key, $subkey]);
}
if ($save) {
$this->saveData();
}
/**
* @deprecated, use new setDataNestedValue()
*/
public function setDataValue(string $key, $val, $save = TRUE): void {
$this->setDataNestedValue($key, $val, $save);
}
/**
* @deprecated, use new setDataNestedValue()
*/
public function setDataSubValue(string $key, string $subkey, $val, bool $save = TRUE): void {
$this->setDataNestedValue([$key, $subkey], $val, $save);
}
/**
* {@inheritdoc}
*/
public function saveData(): void {
$this->set('data', json_encode($this->data_cache));
$this->set('data', json_encode($this->data_cache ?? [], JSON_PRETTY_PRINT));
}
}
......@@ -354,7 +354,7 @@ class DefaultBackend extends AIChatBackendBase {
* {@inheritdoc}
*/
public function loadMessages(): void {
$messages_array = $this->aichat->getDataValue('messages');
$messages_array = $this->aichat->getDataNestedValue('messages');
if (empty($messages_array)) return;
......@@ -377,7 +377,7 @@ class DefaultBackend extends AIChatBackendBase {
$message->setId($uuid);
}
$this->aichat->setDataSubValue('messages', $uuid, $message->toArray());
$this->aichat->setDataNestedValue(['messages', $uuid], $message->toArray());
$this->aichat->save();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment