diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 868d37e..fe01ad1 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,15 +9,9 @@ jobs: strategy: fail-fast: false matrix: - php: [7.4, '8.0', 8.1, 8.2] + php: [8.1, 8.2] laravel: [8, 9, 10] exclude: - - php: 7.4 - laravel: 9 - - php: 7.4 - laravel: 10 - - php: 8.0 - laravel: 10 - php: 8.2 laravel: 8 diff --git a/CHANGELOG.md b/CHANGELOG.md index 8098561..2a0a9c4 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ All notable changes to `laravel-config` will be documented in this file. ## [Unreleased] +## [5.0.0] - 2024-03-05 + +- Dropped support for PHP versions below 8.1. +- Added dynamic casting support to the LaravelConfig class, enabling it to cast configuration values based on their types. +- Added new enum `ConfigValueCast.php` + ## [4.7.0] - 2023-05-11 - PHP 8.2 support added. diff --git a/README.md b/README.md index 34927de..c689166 100755 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Create new config parameter: ``` php $factory = new ConfigFactory(); $configItem = $factory->setName('key') - ->setType('boolean') + ->setType(ConfigDataType::BOOLEAN) ->setValue('1') ->setTags(['system'])//optional ->setDescription('Lorem ipsum dolor sit amet') @@ -57,6 +57,7 @@ Get value with config name: ``` php LaravelConfig::get('key'); ``` + Set value with config name and value: ``` php @@ -86,7 +87,7 @@ Update config with new values: ``` php $factory = new ConfigFactory($configId); $configItem = $factory->setName('updated-key') - ->setType('boolean') + ->setType(ConfigDataType::BOOLEAN) ->setValue('0') ->setTags(['system'])//optional ->setDescription('updated description') @@ -144,7 +145,7 @@ You can also use helper functions: // Creating config item $factory = new ConfigFactory(); $configItem = $factory->setName('key') - ->setType('boolean') + ->setType(ConfigDataType::BOOLEAN) ->setValue('1') ->setTags(['system'])//optional ->setDescription('Lorem ipsum dolor sit amet') @@ -164,7 +165,7 @@ set_config_value('key', 'value'); // Updating config item $factory = new ConfigFactory($configId); $configItem = $factory->setName('updated-key') - ->setType('boolean') + ->setType(ConfigDataType::BOOLEAN) ->setTags(['system'])//optional ->setValue('0') ->setDescription('updated description') diff --git a/composer.json b/composer.json index aca6b16..ba6f4a1 100755 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ } ], "require": { - "php": "^7.4|^8.0|^8.1|^8.2", + "php": "^8.1|^8.2", + "ext-json": "*", "illuminate/support": "^8.49|^9.0|^10.0", "laravel/legacy-factories": "^1.0" }, diff --git a/database/migrations/2024_02_29_111020_update_type_column_in_config_table.php b/database/migrations/2024_02_29_111020_update_type_column_in_config_table.php new file mode 100644 index 0000000..a7f645d --- /dev/null +++ b/database/migrations/2024_02_29_111020_update_type_column_in_config_table.php @@ -0,0 +1,32 @@ +string('type')->default('boolean')->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table(config('laravel-config.table'), function (Blueprint $table) { + $table->enum('type', ['boolean', 'text'])->default('boolean')->change(); + }); + } +} diff --git a/src/Casts/ConfigValueCast.php b/src/Casts/ConfigValueCast.php new file mode 100644 index 0000000..c446cb2 --- /dev/null +++ b/src/Casts/ConfigValueCast.php @@ -0,0 +1,28 @@ +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, + }; + } + + public function set(Model $model, string $key, mixed $value, array $attributes) + { + return $value; + } +} diff --git a/src/Config/Config.php b/src/Config/Config.php index 96e5534..6eb374d 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -3,6 +3,7 @@ namespace TarfinLabs\LaravelConfig\Config; use Illuminate\Database\Eloquent\Model; +use TarfinLabs\LaravelConfig\Casts\ConfigValueCast; class Config extends Model { @@ -14,6 +15,7 @@ class Config extends Model protected $casts = [ 'tags' => 'array', + 'val' => ConfigValueCast::class, ]; public function __construct($attributes = []) diff --git a/src/Enums/ConfigDataType.php b/src/Enums/ConfigDataType.php new file mode 100644 index 0000000..56b8d20 --- /dev/null +++ b/src/Enums/ConfigDataType.php @@ -0,0 +1,12 @@ +name = rtrim($name, '.'); + $config->push($param); } @@ -54,7 +55,7 @@ public function getNested(string $namespace): Collection } /** - * @param $tags + * @param $tags * @return Collection */ public function getByTag($tags): ?Collection diff --git a/tests/LaravelConfigTest.php b/tests/LaravelConfigTest.php index bfe020e..714a139 100644 --- a/tests/LaravelConfigTest.php +++ b/tests/LaravelConfigTest.php @@ -2,9 +2,11 @@ namespace TarfinLabs\LaravelConfig\Tests; +use Carbon\Carbon; use Illuminate\Support\Str; use TarfinLabs\LaravelConfig\Config\Config; use TarfinLabs\LaravelConfig\Config\ConfigFactory; +use TarfinLabs\LaravelConfig\Enums\ConfigDataType; use TarfinLabs\LaravelConfig\LaravelConfig; class LaravelConfigTest extends TestCase @@ -24,7 +26,7 @@ public function it_create_a_new_config_parameter(): void { $factory = new ConfigFactory(); $configItem = $factory->setName(Str::random(5)) - ->setType('boolean') + ->setType(ConfigDataType::BOOLEAN) ->setValue('1') ->setDescription(Str::random(50)) ->get(); @@ -32,9 +34,9 @@ public function it_create_a_new_config_parameter(): void $this->laravelConfig->create($configItem); $this->assertDatabaseHas(config('laravel-config.table'), [ - 'name' => $configItem->name, - 'val' => $configItem->val, - 'type' => $configItem->type, + 'name' => $configItem->name, + 'val' => $configItem->val, + 'type' => $configItem->type, 'description' => $configItem->description, ]); } @@ -44,7 +46,7 @@ public function it_create_a_new_config_parameter_with_tag(): void { $factory = new ConfigFactory(); $configItem = $factory->setName(Str::random(5)) - ->setType('boolean') + ->setType(ConfigDataType::BOOLEAN) ->setValue('1') ->setTags(['system']) ->setDescription(Str::random(50)) @@ -53,9 +55,9 @@ public function it_create_a_new_config_parameter_with_tag(): void $this->laravelConfig->create($configItem); $this->assertDatabaseHas(config('laravel-config.table'), [ - 'name' => $configItem->name, - 'val' => $configItem->val, - 'type' => $configItem->type, + 'name' => $configItem->name, + 'val' => $configItem->val, + 'type' => $configItem->type, 'description' => $configItem->description, ]); @@ -70,7 +72,7 @@ public function it_does_not_create_a_config_parameter_with_the_same_name(): void $factory = new ConfigFactory(); $configItem = $factory->setName($config->name) - ->setType('boolean') + ->setType(ConfigDataType::BOOLEAN) ->setValue('1') ->setDescription(Str::random(50)) ->get(); @@ -87,7 +89,7 @@ public function it_updates_existing_config_parameter(): void $this->assertDatabaseHas(config('laravel-config.table'), ['name' => $config->name, 'val' => $config->val]); $factory = new ConfigFactory($config); - $configItem = $factory->setType('boolean') + $configItem = $factory->setType(ConfigDataType::BOOLEAN) ->setValue('0') ->setDescription('updated-description') ->get(); @@ -95,9 +97,9 @@ public function it_updates_existing_config_parameter(): void $this->laravelConfig->update($config, $configItem); $this->assertDatabaseHas(config('laravel-config.table'), [ - 'name' => $config->name, - 'val' => $configItem->val, - 'type' => $configItem->type, + 'name' => $config->name, + 'val' => $configItem->val, + 'type' => $configItem->type, 'description' => $configItem->description, ]); } @@ -191,13 +193,13 @@ public function it_returns_all_config_parameters(): void public function it_returns_nested_config_parameters(): void { factory(Config::class)->create([ - 'name' => 'foo.bar', - 'val' => true, + 'name' => 'foo.bar', + 'val' => true, ]); factory(Config::class)->create([ - 'name' => 'foo.baz', - 'val' => false, + 'name' => 'foo.baz', + 'val' => false, ]); $response = $this->laravelConfig->getNested('foo'); @@ -206,4 +208,78 @@ public function it_returns_nested_config_parameters(): void $this->assertEquals('bar', $response->first()->name); $this->assertEquals('baz', $response->last()->name); } + + /** @test */ + public function it_returns_boolean_value_for_boolean_type_config_parameter_if_exists(): void + { + $config = factory(Config::class)->create([ + 'name' => 'yunus.was.here', + 'val' => '1', + 'type' => ConfigDataType::BOOLEAN, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertTrue($response); + } + + /** @test */ + public function it_returns_integer_value_for_integer_type_config_parameter_if_exists(): void + { + $config = factory(Config::class)->create([ + 'name' => 'yunus.was.here', + 'val' => '123456', + 'type' => ConfigDataType::INTEGER, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertIsInt($response); + } + + /** @test */ + public function it_returns_datetime_value_for_datetime_type_config_parameter_if_exists(): void + { + $config = factory(Config::class)->create([ + 'name' => 'yunus.was.here', + 'val' => '2024-02-29 12:00', + 'type' => ConfigDataType::DATE_TIME, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertInstanceOf(Carbon::class, $response); + } + + /** @test */ + public function it_returns_date_value_for_date_type_config_parameter_if_exists(): void + { + $config = factory(Config::class)->create([ + 'name' => 'yunus.was.here', + 'val' => '2024-02-29', + 'type' => ConfigDataType::DATE, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertInstanceOf(Carbon::class, $response); + } + + /** @test */ + public function it_returns_json_value_for_json_type_config_parameter_if_exists(): void + { + $config = factory(Config::class)->create([ + 'name' => 'yunus.was.here', + 'val' => '{"9":[7,8,9],"2":[7,8,9],"31":[10,11,12]}', + 'type' => ConfigDataType::JSON, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertIsArray($response); + + $this->assertArrayHasKey(9, $response); + $this->assertArrayHasKey(2, $response); + $this->assertArrayHasKey(31, $response); + } }