Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic casting support in LaravelConfig #31

Merged
merged 38 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b027d53
Update composer.json
YunusEmreNalbant Feb 29, 2024
c81fa1e
Add utility methods to retrieve configuration values with specific types
YunusEmreNalbant Feb 29, 2024
796957c
Add tests for new utility methods in LaravelConfig class
YunusEmreNalbant Feb 29, 2024
a47bca9
Update readme.md
YunusEmreNalbant Feb 29, 2024
681020b
Update LaravelConfig.php
YunusEmreNalbant Feb 29, 2024
31524ec
Apply style.
YunusEmreNalbant Feb 29, 2024
af613bf
Apply style
YunusEmreNalbant Feb 29, 2024
a312c0d
Apply style
YunusEmreNalbant Feb 29, 2024
7c694d2
Apply style
YunusEmreNalbant Feb 29, 2024
511b9c2
update type column in config table
YunusEmreNalbant Feb 29, 2024
42610d0
add ConfigCaster interface
YunusEmreNalbant Feb 29, 2024
6022416
Implement ConfigCaster interface in Boolean, Date, Integer, and Json …
YunusEmreNalbant Feb 29, 2024
e0f2771
Add dynamic casting support in LaravelConfig
YunusEmreNalbant Feb 29, 2024
912a49c
add new tests in LaravelConfigTest.php
YunusEmreNalbant Feb 29, 2024
ff6267e
Update README.md
YunusEmreNalbant Feb 29, 2024
3fda274
Apply style
YunusEmreNalbant Feb 29, 2024
f34546f
Apply style
YunusEmreNalbant Feb 29, 2024
a737622
Add new class DateTimeCaster
YunusEmreNalbant Feb 29, 2024
80fcf23
Add new enum classs ConfigDataType
YunusEmreNalbant Feb 29, 2024
b5b0a24
Apply style
YunusEmreNalbant Feb 29, 2024
aaba021
Add new class ConfigValueCast.php
YunusEmreNalbant Feb 29, 2024
ede9cf0
remove classes Caster
YunusEmreNalbant Feb 29, 2024
09968a2
Refactor LaravelConfigTest to use enums for config data types
YunusEmreNalbant Mar 1, 2024
a83ed2a
Update `database/migrations/2024_02_29_111020_update_type_column_in_c…
YunusEmreNalbant Mar 4, 2024
4d92252
Apply style
YunusEmreNalbant Mar 4, 2024
8015d31
Update LaravelConfig.php
YunusEmreNalbant Mar 4, 2024
fbacece
Update LaravelConfigTest.php
YunusEmreNalbant Mar 4, 2024
1347dd8
Update LaravelConfigTest.php
YunusEmreNalbant Mar 4, 2024
1aca5da
Update LaravelConfigTest.php
YunusEmreNalbant Mar 4, 2024
1360db3
Update LaravelConfigTest.php
YunusEmreNalbant Mar 4, 2024
786aa8d
Update LaravelConfigTest.php
YunusEmreNalbant Mar 4, 2024
3e07815
Update LaravelConfigTest.php
YunusEmreNalbant Mar 4, 2024
a3bb123
Update composer.json
YunusEmreNalbant Mar 4, 2024
a82ad87
Update composer.json
YunusEmreNalbant Mar 4, 2024
f15c660
Update test.yml
YunusEmreNalbant Mar 5, 2024
28813f2
Update CHANGELOG.md
YunusEmreNalbant Mar 5, 2024
91897d9
Update README.md
YunusEmreNalbant Mar 5, 2024
c99bf6c
Update CHANGELOG.md
YunusEmreNalbant Mar 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading