Skip to content

Commit

Permalink
Merge pull request #31 from YunusEmreNalbant/master
Browse files Browse the repository at this point in the history
dynamic casting support in LaravelConfig
  • Loading branch information
frkcn authored Mar 5, 2024
2 parents 0e897bc + c99bf6c commit fbc84de
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 30 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -57,6 +57,7 @@ Get value with config name:
``` php
LaravelConfig::get('key');
```

Set value with config name and value:

``` php
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateTypeColumnInConfigTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table(config('laravel-config.table'), function (Blueprint $table) {
$table->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();
});
}
}
28 changes: 28 additions & 0 deletions src/Casts/ConfigValueCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace TarfinLabs\LaravelConfig\Casts;

use Carbon\Carbon;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use TarfinLabs\LaravelConfig\Enums\ConfigDataType;

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,
};
}

public function set(Model $model, string $key, mixed $value, array $attributes)
{
return $value;
}
}
2 changes: 2 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TarfinLabs\LaravelConfig\Config;

use Illuminate\Database\Eloquent\Model;
use TarfinLabs\LaravelConfig\Casts\ConfigValueCast;

class Config extends Model
{
Expand All @@ -14,6 +15,7 @@ class Config extends Model

protected $casts = [
'tags' => 'array',
'val' => ConfigValueCast::class,
];

public function __construct($attributes = [])
Expand Down
12 changes: 12 additions & 0 deletions src/Enums/ConfigDataType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace TarfinLabs\LaravelConfig\Enums;

enum ConfigDataType: string
{
case INTEGER = 'integer';
case BOOLEAN = 'boolean';
case JSON = 'json';
case DATE = 'date';
case DATE_TIME = 'datetime';
}
3 changes: 2 additions & 1 deletion src/LaravelConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ public function getNested(string $namespace): Collection
}

$param->name = rtrim($name, '.');

$config->push($param);
}

return $config;
}

/**
* @param $tags
* @param $tags
* @return Collection
*/
public function getByTag($tags): ?Collection
Expand Down
110 changes: 93 additions & 17 deletions tests/LaravelConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,17 +26,17 @@ 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();

$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,
]);
}
Expand All @@ -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))
Expand All @@ -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,
]);

Expand All @@ -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();
Expand All @@ -87,17 +89,17 @@ 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();

$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,
]);
}
Expand Down Expand Up @@ -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');
Expand All @@ -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);
}
}

0 comments on commit fbc84de

Please sign in to comment.