From 7b2e5fd86f9986b23d37375f064173117d56d2c0 Mon Sep 17 00:00:00 2001 From: Fatih AYDIN Date: Wed, 13 Mar 2024 17:01:57 +0300 Subject: [PATCH 01/10] feat: Added cast type conversions for set to `ConfigValueCast` --- src/Casts/ConfigValueCast.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Casts/ConfigValueCast.php b/src/Casts/ConfigValueCast.php index c446cb2..65614d0 100644 --- a/src/Casts/ConfigValueCast.php +++ b/src/Casts/ConfigValueCast.php @@ -24,5 +24,18 @@ public function get(Model $model, string $key, mixed $value, array $attributes) public function set(Model $model, string $key, mixed $value, array $attributes) { return $value; + + $type = $attributes['type']?->value ?? $attributes['type'] ?? null; + + switch ($type) { + case ConfigDataType::DATE->value: + return Carbon::parse($value)->format('Y-m-d'); + case ConfigDataType::DATE_TIME->value: + return Carbon::parse($value)->format('Y-m-d H:i'); + case ConfigDataType::JSON->value: + return json_encode($value, true); + default: + return $value; + } } } From 2ef4fb7e4b9836c14a4656c1c8cc552a4a4bfc6c Mon Sep 17 00:00:00 2001 From: Fatih AYDIN Date: Wed, 13 Mar 2024 17:20:17 +0300 Subject: [PATCH 02/10] feat: Added custom caster feature --- src/Casts/ConfigValueCast.php | 54 +++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/Casts/ConfigValueCast.php b/src/Casts/ConfigValueCast.php index 65614d0..36f6245 100644 --- a/src/Casts/ConfigValueCast.php +++ b/src/Casts/ConfigValueCast.php @@ -3,7 +3,9 @@ namespace TarfinLabs\LaravelConfig\Casts; use Carbon\Carbon; +use Illuminate\Contracts\Database\Eloquent\Castable; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; +use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes; use Illuminate\Database\Eloquent\Model; use TarfinLabs\LaravelConfig\Enums\ConfigDataType; @@ -11,19 +13,38 @@ class ConfigValueCast implements CastsAttributes { public function get(Model $model, string $key, mixed $value, array $attributes) { - return match ($attributes['type']) { - ConfigDataType::BOOLEAN->value => (bool) $value, - ConfigDataType::INTEGER->value => (int) $value, - ConfigDataType::DATE->value => Carbon::createFromFormat('Y-m-d', $value), - ConfigDataType::DATE_TIME->value => Carbon::createFromFormat('Y-m-d H:i', $value), - ConfigDataType::JSON->value => json_decode($value, true), - default => $value, - }; + switch ($attributes['type']) { + case ConfigDataType::BOOLEAN->value: + return (bool)$value; + case ConfigDataType::INTEGER->value: + return (int)$value; + case ConfigDataType::DATE->value: + return Carbon::createFromFormat('Y-m-d', $value); + case ConfigDataType::DATE_TIME->value: + return Carbon::createFromFormat('Y-m-d H:i', $value); + case ConfigDataType::JSON->value: + return json_decode($value, true); + default: + $parts = explode(':', $attributes['type']); + $casterClass = array_shift($parts); + if (class_exists($casterClass)) { + $caster = match (true) { + is_subclass_of($casterClass, CastsAttributes::class), is_subclass_of($casterClass, CastsInboundAttributes::class) => new $casterClass(...$parts), + is_subclass_of($casterClass, Castable::class) => $casterClass::castUsing($parts), + default => null + }; + + if ($caster !== null) { + return $caster->get(model: $model, key: $key, value: $value, attributes: $attributes); + } + } + + return $value; + } } public function set(Model $model, string $key, mixed $value, array $attributes) { - return $value; $type = $attributes['type']?->value ?? $attributes['type'] ?? null; @@ -35,6 +56,21 @@ public function set(Model $model, string $key, mixed $value, array $attributes) case ConfigDataType::JSON->value: return json_encode($value, true); default: + $parts = explode(':', $type); + $casterClass = array_shift($parts); + + if (class_exists($casterClass)) { + $caster = match (true) { + is_subclass_of($casterClass, CastsAttributes::class), is_subclass_of($casterClass, CastsInboundAttributes::class) => new $casterClass(...$parts), + is_subclass_of($casterClass, Castable::class) => $casterClass::castUsing($parts), + default => null + }; + + if ($caster !== null) { + return $caster->set(model: $model, key: $key, value: $value, attributes: $parts); + } + } + return $value; } } From dffc8f43933ecb6d7216cbb18ad1248da0bbe64d Mon Sep 17 00:00:00 2001 From: Fatih AYDIN Date: Wed, 13 Mar 2024 17:20:33 +0300 Subject: [PATCH 03/10] test: Added tests --- tests/LaravelConfigTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/LaravelConfigTest.php b/tests/LaravelConfigTest.php index 714a139..6ed8c40 100644 --- a/tests/LaravelConfigTest.php +++ b/tests/LaravelConfigTest.php @@ -282,4 +282,37 @@ public function it_returns_json_value_for_json_type_config_parameter_if_exists() $this->assertArrayHasKey(2, $response); $this->assertArrayHasKey(31, $response); } + + /** @test */ + public function it_returns_in_caster_type_if_type_is_custom_caster_with_param(): void + { + $config = factory(Config::class)->create([ + 'name' => 'fatih.was.here', + 'val' => [ConfigDataType::DATE], + 'type' => AsEnumCollection::class.':'.ConfigDataType::class, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertInstanceOf(Collection::class, $response); + $this->assertCount(1, $response); + + $this->assertEquals([ConfigDataType::DATE], $response->toArray()); + } + + /** @test */ + public function it_returns_in_caster_type_if_type_is_custom_caster(): void + { + $config = factory(Config::class)->create([ + 'name' => 'fatih.was.here', + 'val' => [ConfigDataType::DATE->value], + 'type' => AsCollection::class, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertInstanceOf(Collection::class, $response); + $this->assertCount(1, $response); + $this->assertEquals([ConfigDataType::DATE->value], $response->toArray()); + } } From 734b8d3326ffc9d1dd7388a5b9a6da705ea8257f Mon Sep 17 00:00:00 2001 From: Fatih AYDIN Date: Wed, 13 Mar 2024 17:20:37 +0300 Subject: [PATCH 04/10] test: Fixed tests --- tests/LaravelConfigTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/LaravelConfigTest.php b/tests/LaravelConfigTest.php index 6ed8c40..3c7cb2f 100644 --- a/tests/LaravelConfigTest.php +++ b/tests/LaravelConfigTest.php @@ -3,6 +3,9 @@ namespace TarfinLabs\LaravelConfig\Tests; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Casts\AsCollection; +use Illuminate\Database\Eloquent\Casts\AsEnumCollection; +use Illuminate\Support\Collection; use Illuminate\Support\Str; use TarfinLabs\LaravelConfig\Config\Config; use TarfinLabs\LaravelConfig\Config\ConfigFactory; @@ -256,7 +259,7 @@ public function it_returns_date_value_for_date_type_config_parameter_if_exists() { $config = factory(Config::class)->create([ 'name' => 'yunus.was.here', - 'val' => '2024-02-29', + 'val' => '2024-02-29 14:50:00', 'type' => ConfigDataType::DATE, ]); @@ -270,7 +273,7 @@ public function it_returns_json_value_for_json_type_config_parameter_if_exists() { $config = factory(Config::class)->create([ 'name' => 'yunus.was.here', - 'val' => '{"9":[7,8,9],"2":[7,8,9],"31":[10,11,12]}', + 'val' => [9 => [7,8,9], 2 => [7,8,9], 31 => [10,11,12]], 'type' => ConfigDataType::JSON, ]); From 2f3a8148bafd91ec2657bd35ef58cba9556a1238 Mon Sep 17 00:00:00 2001 From: Fatih AYDIN Date: Thu, 14 Mar 2024 10:06:39 +0300 Subject: [PATCH 05/10] fix: Bug fix --- src/Casts/ConfigValueCast.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Casts/ConfigValueCast.php b/src/Casts/ConfigValueCast.php index 36f6245..a843bc9 100644 --- a/src/Casts/ConfigValueCast.php +++ b/src/Casts/ConfigValueCast.php @@ -54,7 +54,7 @@ public function set(Model $model, string $key, mixed $value, array $attributes) case ConfigDataType::DATE_TIME->value: return Carbon::parse($value)->format('Y-m-d H:i'); case ConfigDataType::JSON->value: - return json_encode($value, true); + return json_encode($value); default: $parts = explode(':', $type); $casterClass = array_shift($parts); From 6167a2d5bdd2b214073a3bf612af8e76b58927f2 Mon Sep 17 00:00:00 2001 From: Fatih AYDIN Date: Thu, 14 Mar 2024 10:07:41 +0300 Subject: [PATCH 06/10] refactor: The order of attributes has changed --- src/LaravelConfig.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LaravelConfig.php b/src/LaravelConfig.php index 8cd9b7b..d2457ef 100755 --- a/src/LaravelConfig.php +++ b/src/LaravelConfig.php @@ -159,8 +159,8 @@ public function delete(Config $config): int private function fillColumns(Config $config, ConfigItem $configItem): Config { $config->name = $configItem->name; - $config->val = $configItem->val; $config->type = $configItem->type; + $config->val = $configItem->val; $config->description = $configItem->description; $config->tags = $configItem->tags; From 808f0a851561c986157e6b399e77e2be3db22ae7 Mon Sep 17 00:00:00 2001 From: Fatih AYDIN Date: Thu, 14 Mar 2024 10:41:03 +0300 Subject: [PATCH 07/10] refactor: Update README.md --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index c689166..36c22df 100755 --- a/README.md +++ b/README.md @@ -180,6 +180,17 @@ delete_config('key'); read_nested('foo.bar'); ``` +### Custom Casters +You can also custom casters: + +```php +$config = factory(Config::class)->create([ + 'name' => 'custom-cast-config-name', + 'val' => [ConfigDataType::DATE], + 'type' => AsEnumCollection::class.':'.ConfigDataType::class, +]); +``` + ### Testing ``` bash From 5218b683108c8c5e93ab4501f908cf108237116a Mon Sep 17 00:00:00 2001 From: Fatih AYDIN Date: Thu, 14 Mar 2024 11:03:44 +0300 Subject: [PATCH 08/10] fix: Fixed json type issue --- src/Casts/ConfigValueCast.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Casts/ConfigValueCast.php b/src/Casts/ConfigValueCast.php index a843bc9..b3f70c3 100644 --- a/src/Casts/ConfigValueCast.php +++ b/src/Casts/ConfigValueCast.php @@ -54,7 +54,10 @@ public function set(Model $model, string $key, mixed $value, array $attributes) case ConfigDataType::DATE_TIME->value: return Carbon::parse($value)->format('Y-m-d H:i'); case ConfigDataType::JSON->value: - return json_encode($value); + return match (true) { + is_string($value) => $value, + default => json_encode($value) + }; default: $parts = explode(':', $type); $casterClass = array_shift($parts); From 11df5f62677e220ed81a824e2008ca0474912b75 Mon Sep 17 00:00:00 2001 From: Fatih AYDIN Date: Thu, 14 Mar 2024 11:05:16 +0300 Subject: [PATCH 09/10] style: Fixed styling --- src/Casts/ConfigValueCast.php | 4 ++-- tests/LaravelConfigTest.php | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Casts/ConfigValueCast.php b/src/Casts/ConfigValueCast.php index b3f70c3..3842c68 100644 --- a/src/Casts/ConfigValueCast.php +++ b/src/Casts/ConfigValueCast.php @@ -15,9 +15,9 @@ public function get(Model $model, string $key, mixed $value, array $attributes) { switch ($attributes['type']) { case ConfigDataType::BOOLEAN->value: - return (bool)$value; + return (bool) $value; case ConfigDataType::INTEGER->value: - return (int)$value; + return (int) $value; case ConfigDataType::DATE->value: return Carbon::createFromFormat('Y-m-d', $value); case ConfigDataType::DATE_TIME->value: diff --git a/tests/LaravelConfigTest.php b/tests/LaravelConfigTest.php index 3c7cb2f..2aa97c5 100644 --- a/tests/LaravelConfigTest.php +++ b/tests/LaravelConfigTest.php @@ -29,10 +29,10 @@ public function it_create_a_new_config_parameter(): void { $factory = new ConfigFactory(); $configItem = $factory->setName(Str::random(5)) - ->setType(ConfigDataType::BOOLEAN) - ->setValue('1') - ->setDescription(Str::random(50)) - ->get(); + ->setType(ConfigDataType::BOOLEAN) + ->setValue('1') + ->setDescription(Str::random(50)) + ->get(); $this->laravelConfig->create($configItem); @@ -75,10 +75,10 @@ public function it_does_not_create_a_config_parameter_with_the_same_name(): void $factory = new ConfigFactory(); $configItem = $factory->setName($config->name) - ->setType(ConfigDataType::BOOLEAN) - ->setValue('1') - ->setDescription(Str::random(50)) - ->get(); + ->setType(ConfigDataType::BOOLEAN) + ->setValue('1') + ->setDescription(Str::random(50)) + ->get(); $response = $this->laravelConfig->create($configItem); @@ -93,9 +93,9 @@ public function it_updates_existing_config_parameter(): void $factory = new ConfigFactory($config); $configItem = $factory->setType(ConfigDataType::BOOLEAN) - ->setValue('0') - ->setDescription('updated-description') - ->get(); + ->setValue('0') + ->setDescription('updated-description') + ->get(); $this->laravelConfig->update($config, $configItem); @@ -273,7 +273,7 @@ public function it_returns_json_value_for_json_type_config_parameter_if_exists() { $config = factory(Config::class)->create([ 'name' => 'yunus.was.here', - 'val' => [9 => [7,8,9], 2 => [7,8,9], 31 => [10,11,12]], + 'val' => [9 => [7, 8, 9], 2 => [7, 8, 9], 31 => [10, 11, 12]], 'type' => ConfigDataType::JSON, ]); @@ -292,7 +292,7 @@ public function it_returns_in_caster_type_if_type_is_custom_caster_with_param(): $config = factory(Config::class)->create([ 'name' => 'fatih.was.here', 'val' => [ConfigDataType::DATE], - 'type' => AsEnumCollection::class.':'.ConfigDataType::class, + 'type' => AsEnumCollection::class . ':' . ConfigDataType::class, ]); $response = $this->laravelConfig->get($config->name); From eb92eaab8c80ab0c92ee5d152fc038f8776af48b Mon Sep 17 00:00:00 2001 From: Fatih AYDIN Date: Thu, 14 Mar 2024 11:08:05 +0300 Subject: [PATCH 10/10] style: Fixed styling --- src/Casts/ConfigValueCast.php | 1 - tests/LaravelConfigTest.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Casts/ConfigValueCast.php b/src/Casts/ConfigValueCast.php index 3842c68..92b13a4 100644 --- a/src/Casts/ConfigValueCast.php +++ b/src/Casts/ConfigValueCast.php @@ -45,7 +45,6 @@ public function get(Model $model, string $key, mixed $value, array $attributes) public function set(Model $model, string $key, mixed $value, array $attributes) { - $type = $attributes['type']?->value ?? $attributes['type'] ?? null; switch ($type) { diff --git a/tests/LaravelConfigTest.php b/tests/LaravelConfigTest.php index 2aa97c5..8c0dca3 100644 --- a/tests/LaravelConfigTest.php +++ b/tests/LaravelConfigTest.php @@ -292,7 +292,7 @@ public function it_returns_in_caster_type_if_type_is_custom_caster_with_param(): $config = factory(Config::class)->create([ 'name' => 'fatih.was.here', 'val' => [ConfigDataType::DATE], - 'type' => AsEnumCollection::class . ':' . ConfigDataType::class, + 'type' => AsEnumCollection::class.':'.ConfigDataType::class, ]); $response = $this->laravelConfig->get($config->name);