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 20 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Get value with config name:
``` php
LaravelConfig::get('key');
```

Set value with config name and value:

``` php
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"require": {
"php": "^7.4|^8.0|^8.1|^8.2",
"illuminate/support": "^8.49|^9.0|^10.0",
"laravel/legacy-factories": "^1.0"
"laravel/legacy-factories": "^1.0",
"ext-json": "*"
},
"require-dev": {
"orchestra/testbench": "^6.13|^7.0|^8.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->enum('type', ['boolean', 'text', 'date', 'datetime', 'json', 'array', 'integer'])->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();
});
}
}
13 changes: 13 additions & 0 deletions src/Casters/BooleanCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace TarfinLabs\LaravelConfig\Casters;

use TarfinLabs\LaravelConfig\Contracts\ConfigCaster;

class BooleanCaster implements ConfigCaster
{
public function cast($value): bool
{
return (bool) $value;
}
}
14 changes: 14 additions & 0 deletions src/Casters/DateCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace TarfinLabs\LaravelConfig\Casters;

use Carbon\Carbon;
use TarfinLabs\LaravelConfig\Contracts\ConfigCaster;

class DateCaster implements ConfigCaster
{
public function cast($value): Carbon
{
return Carbon::createFromFormat('Y-m-d', $value);
}
}
14 changes: 14 additions & 0 deletions src/Casters/DateTimeCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace TarfinLabs\LaravelConfig\Casters;

use Carbon\Carbon;
use TarfinLabs\LaravelConfig\Contracts\ConfigCaster;

class DateTimeCaster implements ConfigCaster
{
public function cast($value): Carbon
{
return Carbon::createFromFormat('Y-m-d H:i', $value);
}
}
13 changes: 13 additions & 0 deletions src/Casters/IntegerCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace TarfinLabs\LaravelConfig\Casters;

use TarfinLabs\LaravelConfig\Contracts\ConfigCaster;

class IntegerCaster implements ConfigCaster
{
public function cast($value): int
{
return (int) $value;
}
}
23 changes: 23 additions & 0 deletions src/Casters/JsonCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace TarfinLabs\LaravelConfig\Casters;

use TarfinLabs\LaravelConfig\Contracts\ConfigCaster;

class JsonCaster implements ConfigCaster
{
public function __construct(public ?bool $associative = true, public int $depth = 512, public int $flags = 0)
{
}

public function cast($value)
{
$decodedValue = json_decode($value, $this->associative, $this->depth, $this->flags);

if (json_last_error() === JSON_ERROR_NONE) {
return $decodedValue;
}

return $value;
}
}
8 changes: 8 additions & 0 deletions src/Contracts/ConfigCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace TarfinLabs\LaravelConfig\Contracts;

interface ConfigCaster
{
public function cast($value);
}
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';
}
18 changes: 16 additions & 2 deletions src/LaravelConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
namespace TarfinLabs\LaravelConfig;

use Illuminate\Support\Collection;
use TarfinLabs\LaravelConfig\Casters\BooleanCaster;
use TarfinLabs\LaravelConfig\Casters\DateCaster;
use TarfinLabs\LaravelConfig\Casters\DateTimeCaster;
use TarfinLabs\LaravelConfig\Casters\IntegerCaster;
use TarfinLabs\LaravelConfig\Casters\JsonCaster;
use TarfinLabs\LaravelConfig\Config\Config;
use TarfinLabs\LaravelConfig\Config\ConfigItem;
use TarfinLabs\LaravelConfig\Enums\ConfigDataType;

class LaravelConfig
{
Expand All @@ -23,7 +29,14 @@ public function get(string $name, $default = null)

$config = Config::where('name', $name)->first();

return $config->val;
return match ($config->type) {
ConfigDataType::BOOLEAN->value => (new BooleanCaster())->cast($config->val),
ConfigDataType::INTEGER->value => (new IntegerCaster())->cast($config->val),
ConfigDataType::DATE->value => (new DateCaster())->cast($config->val),
ConfigDataType::DATE_TIME->value => (new DateTimeCaster())->cast($config->val),
ConfigDataType::JSON->value => (new JsonCaster())->cast($config->val),
default => $config->val,
};
}

/**
Expand All @@ -47,14 +60,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
101 changes: 88 additions & 13 deletions tests/LaravelConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TarfinLabs\LaravelConfig\Tests;

use Carbon\Carbon;
use Illuminate\Support\Str;
use TarfinLabs\LaravelConfig\Config\Config;
use TarfinLabs\LaravelConfig\Config\ConfigFactory;
Expand Down Expand Up @@ -32,9 +33,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,
]);
}
Expand All @@ -53,9 +54,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 Down Expand Up @@ -95,9 +96,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,
]);
}
Expand Down Expand Up @@ -191,13 +192,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 +207,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' => 'boolean',
tkaratug marked this conversation as resolved.
Show resolved Hide resolved
]);

$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' => 'integer',
tkaratug marked this conversation as resolved.
Show resolved Hide resolved
]);

$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' => 'datetime',
tkaratug marked this conversation as resolved.
Show resolved Hide resolved
]);

$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' => 'date',
tkaratug marked this conversation as resolved.
Show resolved Hide resolved
]);

$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' => 'json',
tkaratug marked this conversation as resolved.
Show resolved Hide resolved
]);

$response = $this->laravelConfig->get($config->name);

$this->assertIsArray($response);

$this->assertArrayHasKey(9, $response);
$this->assertArrayHasKey(2, $response);
$this->assertArrayHasKey(31, $response);
}
}
Loading