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

Plain type json #99

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions config/asseco-custom-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Asseco\CustomFields\App\PlainTypes\DateType;
use Asseco\CustomFields\App\PlainTypes\FloatType;
use Asseco\CustomFields\App\PlainTypes\IntegerType;
use Asseco\CustomFields\App\PlainTypes\JsonType;
use Asseco\CustomFields\App\PlainTypes\StringType;
use Asseco\CustomFields\App\PlainTypes\TextType;
use Asseco\CustomFields\App\PlainTypes\TimeType;
Expand Down Expand Up @@ -48,6 +49,7 @@
'string' => StringType::class,
'text' => TextType::class,
'time' => TimeType::class,
'json' => JsonType::class,
],

'migrations' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$exists = DB::table('custom_field_plain_types')->where('name', 'json')->exists();
if ($exists) {
// already exists
return;
}

$types = ['json'];

$plainTypes = [];
foreach ($types as $type) {
if (config('asseco-custom-fields.migrations.uuid')) {
$plainTypes[] = [
'id' => Str::uuid(),
'name' => $type,
'created_at' => now(),
'updated_at' => now(),
];
} else {
$plainTypes[] = [
'name' => $type,
'created_at' => now(),
'updated_at' => now(),
];
}
}

DB::table('custom_field_plain_types')->insert($plainTypes);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::table('custom_field_plain_types')
->where('name', 'json')
->delete();
}
};
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;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('custom_field_values', function (Blueprint $table) {
$table->json('json')->nullable()->default(null)->after('time');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('custom_field_values', function (Blueprint $table) {
$table->dropColumn('json');
});
}
};
12 changes: 12 additions & 0 deletions src/App/Contracts/PlainTypes/JsonType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Asseco\CustomFields\App\Contracts\PlainTypes;

use Illuminate\Database\Eloquent\Model;

/**
* @mixin Model|\Asseco\CustomFields\App\PlainTypes\TimeType
*/
interface JsonType
{
}
1 change: 1 addition & 0 deletions src/App/Http/Requests/ValueRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function rules()
'datetime' => 'nullable|string',
'date' => 'nullable|string',
'time' => 'nullable|string',
'json' => 'nullable|array',
];
}

Expand Down
7 changes: 6 additions & 1 deletion src/App/Models/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Value extends Model implements \Asseco\CustomFields\App\Contracts\Value
* Columns which are classified as value columns.
*/
public const VALUE_COLUMNS = [
'string', 'integer', 'float', 'text', 'boolean', 'datetime', 'date', 'time',
'string', 'integer', 'float', 'text', 'boolean', 'datetime', 'date', 'time', 'json',
];

/**
Expand All @@ -36,6 +36,11 @@ class Value extends Model implements \Asseco\CustomFields\App\Contracts\Value
protected $guarded = ['id', 'created_at', 'updated_at'];

protected $appends = ['value'];
protected $casts = [
'float' => 'float',
'boolean' => 'boolean',
'json' => 'array',
];

protected static function booted()
{
Expand Down
24 changes: 24 additions & 0 deletions src/App/PlainTypes/JsonType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Asseco\CustomFields\App\PlainTypes;

use Asseco\CustomFields\App\Contracts\Mappable;
use Asseco\CustomFields\App\Models\PlainType;
use Illuminate\Database\Eloquent\Builder;

class JsonType extends PlainType implements Mappable, \Asseco\CustomFields\App\Contracts\PlainTypes\JsonType
{
protected static function booted()
{
static::addGlobalScope('name', function (Builder $builder) {
$builder->where('name', 'json');
});
}

public static function mapToValueColumn(): string
{
return 'json';
}
}
Loading