Skip to content

Commit

Permalink
Allow dot notation when setting store values
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Nov 5, 2024
1 parent d85aee2 commit d61d710
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "yii-module",
"license": "mit",
"require": {
"php": ">=8.2",
"php": "^8.2",
"craftcms/cms": "^5.0",
"putyourlightson/datastar-php": "1.0.2"
},
Expand Down
22 changes: 19 additions & 3 deletions src/models/StoreModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,29 @@ public function getValues(): array
}

/**
* Sets a value in the store.
* Sets a value in the store, converting the name to a nested array using dot notation.
*/
public function set(string $name, mixed $value): static
{
$this->values[$name] = $value;
$parts = explode('.', $name);
$currentValue = &$this->values;
$store = [];
$currentStore = &$store;

Spark::getInstance()->events->store([$name => $value]);
foreach ($parts as $part) {
if (!isset($currentValue[$part])) {
$currentValue[$part] = [];
}
$currentValue = &$currentValue[$part];

$currentStore[$part] = [];
$currentStore = &$currentStore[$part];
}

$currentValue = $value;
$currentStore = $value;

Spark::getInstance()->events->store($store);

return $this;
}
Expand Down

0 comments on commit d61d710

Please sign in to comment.