Skip to content

Commit

Permalink
Plain type json (#99)
Browse files Browse the repository at this point in the history
* 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 <bot@styleci.io>
  • Loading branch information
ngaspari and StyleCIBot authored Nov 12, 2024
1 parent 1fa1cab commit b48891a
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 1 deletion.
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';
}
}

0 comments on commit b48891a

Please sign in to comment.