Skip to content

Commit b48891a

Browse files
ngaspariStyleCIBot
andauthored
Plain type json (#99)
* Add JSON type CF Add JSON type CF * Apply fixes from StyleCI * Update 2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php a * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent 1fa1cab commit b48891a

7 files changed

+133
-1
lines changed

config/asseco-custom-fields.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Asseco\CustomFields\App\PlainTypes\DateType;
1717
use Asseco\CustomFields\App\PlainTypes\FloatType;
1818
use Asseco\CustomFields\App\PlainTypes\IntegerType;
19+
use Asseco\CustomFields\App\PlainTypes\JsonType;
1920
use Asseco\CustomFields\App\PlainTypes\StringType;
2021
use Asseco\CustomFields\App\PlainTypes\TextType;
2122
use Asseco\CustomFields\App\PlainTypes\TimeType;
@@ -48,6 +49,7 @@
4849
'string' => StringType::class,
4950
'text' => TextType::class,
5051
'time' => TimeType::class,
52+
'json' => JsonType::class,
5153
],
5254

5355
'migrations' => [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Support\Facades\DB;
5+
use Illuminate\Support\Str;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
$exists = DB::table('custom_field_plain_types')->where('name', 'json')->exists();
17+
if ($exists) {
18+
// already exists
19+
return;
20+
}
21+
22+
$types = ['json'];
23+
24+
$plainTypes = [];
25+
foreach ($types as $type) {
26+
if (config('asseco-custom-fields.migrations.uuid')) {
27+
$plainTypes[] = [
28+
'id' => Str::uuid(),
29+
'name' => $type,
30+
'created_at' => now(),
31+
'updated_at' => now(),
32+
];
33+
} else {
34+
$plainTypes[] = [
35+
'name' => $type,
36+
'created_at' => now(),
37+
'updated_at' => now(),
38+
];
39+
}
40+
}
41+
42+
DB::table('custom_field_plain_types')->insert($plainTypes);
43+
}
44+
45+
/**
46+
* Reverse the migrations.
47+
*
48+
* @return void
49+
*/
50+
public function down()
51+
{
52+
DB::table('custom_field_plain_types')
53+
->where('name', 'json')
54+
->delete();
55+
}
56+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('custom_field_values', function (Blueprint $table) {
17+
$table->json('json')->nullable()->default(null)->after('time');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('custom_field_values', function (Blueprint $table) {
29+
$table->dropColumn('json');
30+
});
31+
}
32+
};
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Asseco\CustomFields\App\Contracts\PlainTypes;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* @mixin Model|\Asseco\CustomFields\App\PlainTypes\TimeType
9+
*/
10+
interface JsonType
11+
{
12+
}

src/App/Http/Requests/ValueRequest.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function rules()
3838
'datetime' => 'nullable|string',
3939
'date' => 'nullable|string',
4040
'time' => 'nullable|string',
41+
'json' => 'nullable|array',
4142
];
4243
}
4344

src/App/Models/Value.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Value extends Model implements \Asseco\CustomFields\App\Contracts\Value
2323
* Columns which are classified as value columns.
2424
*/
2525
public const VALUE_COLUMNS = [
26-
'string', 'integer', 'float', 'text', 'boolean', 'datetime', 'date', 'time',
26+
'string', 'integer', 'float', 'text', 'boolean', 'datetime', 'date', 'time', 'json',
2727
];
2828

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

3838
protected $appends = ['value'];
39+
protected $casts = [
40+
'float' => 'float',
41+
'boolean' => 'boolean',
42+
'json' => 'array',
43+
];
3944

4045
protected static function booted()
4146
{

src/App/PlainTypes/JsonType.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Asseco\CustomFields\App\PlainTypes;
6+
7+
use Asseco\CustomFields\App\Contracts\Mappable;
8+
use Asseco\CustomFields\App\Models\PlainType;
9+
use Illuminate\Database\Eloquent\Builder;
10+
11+
class JsonType extends PlainType implements Mappable, \Asseco\CustomFields\App\Contracts\PlainTypes\JsonType
12+
{
13+
protected static function booted()
14+
{
15+
static::addGlobalScope('name', function (Builder $builder) {
16+
$builder->where('name', 'json');
17+
});
18+
}
19+
20+
public static function mapToValueColumn(): string
21+
{
22+
return 'json';
23+
}
24+
}

0 commit comments

Comments
 (0)