Skip to content

Commit

Permalink
Remove modifier method
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 3, 2020
1 parent 400b102 commit b94a2c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
32 changes: 9 additions & 23 deletions src/Extend/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ class Settings implements ExtenderInterface
{
private $settings = [];
private $defaults = [];
private $modifiers = [];

/**
* Serialize a setting value to the ForumSerializer attributes.
*
* @param string $attributeName: The attribute name to be used in the ForumSerializer attributes array.
* @param string $key: The key of the setting.
* @param string|callable|null $callback: Optional callback to modify the value before serialization.
* @return $this
*/
public function serializeToForum(string $attributeName, string $key)
public function serializeToForum(string $attributeName, string $key, $callback = null)
{
$this->settings[$key] = $attributeName;
$this->settings[$key] = compact('attributeName', 'callback');

return $this;
}
Expand All @@ -50,20 +50,6 @@ public function default(string $key, $value)
return $this;
}

/**
* Modify a setting's value.
*
* @param string $key
* @param $callback
* @return $this
*/
public function modifier(string $key, $callback)
{
$this->modifiers[$key] = $callback;

return $this;
}

public function extend(Container $container, Extension $extension = null)
{
if (! empty($this->settings)) {
Expand All @@ -73,15 +59,15 @@ function () use ($container) {
$settings = $container->make(SettingsRepositoryInterface::class);
$attributes = [];

foreach ($this->settings as $key => $attributeName) {
$value = $settings->get($key, $this->defaults[$key] ?? null);
foreach ($this->settings as $key => $setting) {
$value = $settings->get($key, null);

if (isset($this->modifiers[$key])) {
$callback = ContainerUtil::wrapCallback($this->modifiers[$key], $container);
$value = $callback($value);
if (isset($setting['callback'])) {
$setting['callback'] = ContainerUtil::wrapCallback($setting['callback'], $container);
$value = $setting['callback']($value);
}

$attributes[$attributeName] = $value;
$attributes[$setting['attributeName']] = $value;
}

return $attributes;
Expand Down
17 changes: 7 additions & 10 deletions tests/integration/extenders/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ public function custom_setting_falls_back_to_default_value()
/**
* @test
*/
public function custom_setting_modifier_works_if_added()
public function custom_setting_callback_works_if_added()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting')
->modifier('custom-prefix.custom_setting', function ($value) {
->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting', function ($value) {
return $value.'Modified';
})
);
Expand All @@ -128,12 +127,11 @@ public function custom_setting_modifier_works_if_added()
/**
* @test
*/
public function custom_setting_modifier_works_with_invokable_class()
public function custom_setting_callback_works_with_invokable_class()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.customSetting2', 'custom-prefix.custom_setting2')
->modifier('custom-prefix.custom_setting2', CustomInvokableClass::class)
->serializeToForum('customPrefix.customSetting2', 'custom-prefix.custom_setting2', CustomInvokableClass::class)
);

$this->prepDb();
Expand All @@ -153,15 +151,14 @@ public function custom_setting_modifier_works_with_invokable_class()
/**
* @test
*/
public function custom_setting_modifier_works_on_default_value()
public function custom_setting_callback_works_on_default_value()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.unavailableCustomSetting2', 'custom-prefix.unavailable_custom_setting2')
->default('custom-prefix.unavailable_custom_setting2', 'default')
->modifier('custom-prefix.unavailable_custom_setting2', function ($value) {
->serializeToForum('customPrefix.unavailableCustomSetting2', 'custom-prefix.unavailable_custom_setting2', function ($value) {
return $value.'Modified';
})
->default('custom-prefix.unavailable_custom_setting2', 'default')
);

$this->prepDb();
Expand Down

0 comments on commit b94a2c0

Please sign in to comment.