Skip to content

Commit

Permalink
change forget() to filter()
Browse files Browse the repository at this point in the history
  • Loading branch information
OrdinaryJellyfish committed Dec 16, 2023
1 parent c55fc75 commit d049f53
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
12 changes: 6 additions & 6 deletions framework/core/src/Api/Controller/SetSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

class SetSettingsController implements RequestHandlerInterface
{
public static array $forget = [];
public static array $filter = [];

public function __construct(
protected SettingsRepositoryInterface $settings,
Expand All @@ -39,14 +39,14 @@ public function handle(ServerRequestInterface $request): ResponseInterface

foreach ($settings as $k => $v) {
$this->dispatcher->dispatch(new Event\Serializing($k, $v));
$forgetCallback = Arr::get(static::$forget, $k);
$shouldForget = false;
$filterCallback = Arr::get(static::$filter, $k);
$shouldFilter = false;

if (! is_null($forgetCallback)) {
$shouldForget = $forgetCallback($v);
if (! is_null($filterCallback)) {
$shouldFilter = $filterCallback($v);
}

if ($shouldForget) {
if ($shouldFilter) {
$this->settings->delete($k);
} else {
$this->settings->set($k, $v);
Expand Down
12 changes: 6 additions & 6 deletions framework/core/src/Extend/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Settings implements ExtenderInterface
private array $settings = [];
private array $defaults = [];
private array $lessConfigs = [];
private array $forget = [];
private array $filter = [];

/**
* Serialize a setting value to the ForumSerializer attributes.
Expand Down Expand Up @@ -68,9 +68,9 @@ public function default(string $key, mixed $value): self
* @param string $key: The key of the setting.
* @param (callable(mixed $value): bool)|bool $callback: Boolean to determine whether the setting needs deleted.
*/
public function forget(string $key, callable|bool $callback): self
public function filter(string $key, callable|bool $callback): self
{
$this->forget[$key] = $callback;
$this->filter[$key] = $callback;

return $this;
}
Expand Down Expand Up @@ -113,10 +113,10 @@ public function extend(Container $container, Extension $extension = null): void
});
}

if (! empty($this->forget)) {
foreach ($this->forget as $key => $callback) {
if (! empty($this->filter)) {
foreach ($this->filter as $key => $callback) {
Arr::set(
SetSettingsController::$forget,
SetSettingsController::$filter,
$key,
ContainerUtil::wrapCallback($callback, $container)
);
Expand Down
10 changes: 5 additions & 5 deletions framework/core/tests/integration/extenders/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ public function null_custom_setting_returns_null()
/**
* @test
*/
public function forgetting_setting_returns_default_value()
public function filtering_setting_returns_default_value()
{
$this->extend(
(new Extend\Settings())
->default('custom-prefix.forget_this_setting', 'extenderDefault')
->forget('custom-prefix.forget_this_setting', function (mixed $value): bool {
->default('custom-prefix.filter_this_setting', 'extenderDefault')
->filter('custom-prefix.filter_this_setting', function (mixed $value): bool {
return $value === '';
})
);
Expand All @@ -194,15 +194,15 @@ public function forgetting_setting_returns_default_value()
$this->request('POST', '/api/settings', [
'authenticatedAs' => 1,
'json' => [
'custom-prefix.forget_this_setting' => ''
'custom-prefix.filter_this_setting' => ''
]
])
);

$value = $this->app()
->getContainer()
->make('flarum.settings')
->get('custom-prefix.forget_this_setting');
->get('custom-prefix.filter_this_setting');

$this->assertEquals('extenderDefault', $value);
}
Expand Down

0 comments on commit d049f53

Please sign in to comment.