Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdankharchenko committed Dec 21, 2021
2 parents 1eb6a2f + 1c71296 commit 394cf1f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"php":"8.1.0","version":"3.4.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}},"single_trait_insert_per_statement":true},"hashes":{"src\/Models\/HasSettings.php":3446277725,"src\/Models\/Setting.php":1606402483,"src\/ClassMorphMap.php":613274952,"src\/BaseSettings.php":204669101,"src\/Providers\/SettingsServiceProvider.php":3414138676,"src\/CachesSettings.php":258548273,"tests\/ClassMapTest.php":1928410964,"tests\/SettingsTest.php":2064399654,"tests\/Fixtures\/ComplexSetting.php":4179202307,"tests\/BaseTestCase.php":2037732410}}
{"php":"8.1.0","version":"3.4.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}},"single_trait_insert_per_statement":true},"hashes":{"src\/Models\/HasSettings.php":3533225731,"src\/Models\/Setting.php":1606402483,"src\/ClassMorphMap.php":613274952,"src\/BaseSettings.php":204669101,"src\/Providers\/SettingsServiceProvider.php":3414138676,"src\/CachesSettings.php":258548273,"tests\/ClassMapTest.php":1928410964,"tests\/SettingsTest.php":842726344,"tests\/Fixtures\/ComplexSetting.php":4179202307,"tests\/BaseTestCase.php":335068492,"tests\/Fixtures\/SimpleSetting.php":448900176}}
8 changes: 2 additions & 6 deletions src/Models/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@

trait HasSettings
{

use ReflectsClosures;

public function settings(): MorphMany
{
return $this->morphMany(Setting::class, 'settable');
}


public function setSettings(Closure $closure): self
{
$closureParameterTypes = $this->closureParameterTypes($closure);
Expand All @@ -43,21 +41,19 @@ public function setSettings(Closure $closure): self
return $this;
}


public function getSettings($class)
{
return new $class($this);
}


public function scopeWhereSettings(Builder $builder, $class, $name, $operator, $value = null)
{
$class = ClassMorphMap::getKeyFromClass($class);

$builder->whereHas('settings', function($builder) use ($class, $name, $operator, $value) {
$builder->whereHas('settings', function ($builder) use ($class, $name, $operator, $value) {
$builder->where('class', $class);

$builder->where(function(Builder $builder) use ($name, $operator, $value) {
$builder->where(function (Builder $builder) use ($name, $operator, $value) {
$builder->where("payload->{$name}", $operator ?? '=', $value ?? $operator);
});
});
Expand Down
1 change: 0 additions & 1 deletion tests/Fixtures/SimpleSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@

class SimpleSetting extends BaseSettings
{

public string $favoriteColor = 'red';
}
18 changes: 6 additions & 12 deletions tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

class SettingsBaseTest extends BaseTestCase
{

public function test_complex_data_types(): void
{
config([
'typed-settings.morph' => [
'typed-settings.morph' => [
'complex' => ComplexSetting::class,
],
'typed-settings.cache.enabled' => false,
Expand Down Expand Up @@ -52,12 +51,11 @@ public function test_complex_data_types(): void
//$this->assertTrue($user->exists());
}


public function test_settings_can_be_set_with_closure(): void
{
config([ 'typed-settings.morph' => [ 'complex' => ComplexSetting::class ] ]);

$this->getUser()->setSettings(function(ComplexSetting $complex) {
$this->getUser()->setSettings(function (ComplexSetting $complex) {
$complex->filling = 'cherry';
$complex->pi = 22.22;
$complex->ingredients = [ 'cherry', 'eggs', 'butter' ];
Expand All @@ -73,14 +71,13 @@ public function test_settings_can_be_set_with_closure(): void
$this->assertTrue($complex->isReady);
}


public function test_settings_are_cachable_and_cache_is_cleared_when_settings_are_updated(): void
{
config([ 'typed-settings.morph' => [ 'complex' => ComplexSetting::class ] ]);
config([
'typed-settings.cache' => [
'enabled' => true,
'driver' => 'array',
'driver' => 'array',
'seconds' => 30,
],
]);
Expand All @@ -98,27 +95,26 @@ public function test_settings_are_cachable_and_cache_is_cleared_when_settings_ar

$this->assertTrue(Cache::driver('array')->has($key));

$user->setSettings(function(ComplexSetting $complex) {
$user->setSettings(function (ComplexSetting $complex) {
$complex->filling = 'cherry';
});

$this->assertFalse(Cache::driver('array')->has($key));
}


public function test_you_may_pass_multiple_closures_when_setting(): void
{
$user = $this->getUser();

config([
'typed-settings.morph' => [
'typed-settings.morph' => [
'complex' => ComplexSetting::class,
'simplex' => SimpleSetting::class,
],
'typed-settings.cache.enabled' => false,
]);

$user->setSettings(function(SimpleSetting $simple, ComplexSetting $complex) {
$user->setSettings(function (SimpleSetting $simple, ComplexSetting $complex) {
$simple->favoriteColor = 'blue';
$complex->filling = 'cherry';
});
Expand All @@ -130,7 +126,6 @@ public function test_you_may_pass_multiple_closures_when_setting(): void
$this->assertEquals('cherry', $complex->filling);
}


protected function getUser(): User
{
$user = new User();
Expand All @@ -142,6 +137,5 @@ protected function getUser(): User

class User extends Model
{

use HasSettings;
}

0 comments on commit 394cf1f

Please sign in to comment.