Skip to content

Commit 9278ca1

Browse files
Merge pull request #1 from ssntpl/feature/misc54_define_fields
define data fields type and cast
2 parents a4b6bff + 408130c commit 9278ca1

7 files changed

Lines changed: 127 additions & 3 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"prefer-stable": true,
3737
"require": {
3838
"php": "*",
39-
"laravel/framework": "*"
39+
"laravel/framework": "*",
40+
"ssntpl/laravel-files": "^0.1.3"
4041
}
4142
}

src/Casts/FieldValueCast.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Ssntpl\DataFields\Casts;
4+
5+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
6+
use Ssntpl\DataFields\Enums\FieldType;
7+
use Ssntpl\LaravelFiles\Models\File;
8+
9+
class FieldValueCast implements CastsAttributes
10+
{
11+
public function get($model, string $key, $value, array $attributes)
12+
{
13+
if (is_null($value)) return null;
14+
15+
return match($attributes['type'] ?? FieldType::TEXT) {
16+
FieldType::NUMBER => (float) $value,
17+
FieldType::BOOLEAN, FieldType::CHECK => (bool) $value,
18+
FieldType::MULTIPLE, FieldType::ARRAY => json_decode($value, true),
19+
FieldType::JSON => json_decode($value, true),
20+
FieldType::FILE => $this->getFileFromJson($value),
21+
default => (string) $value
22+
};
23+
}
24+
25+
public function set($model, string $key, $value, array $attributes)
26+
{
27+
if (is_null($value)) return null;
28+
29+
return match($attributes['type'] ?? FieldType::TEXT) {
30+
FieldType::MULTIPLE, FieldType::ARRAY, FieldType::JSON => json_encode($value),
31+
FieldType::BOOLEAN, FieldType::CHECK => $value ? '1' : '0',
32+
FieldType::FILE => $this->setFileAsJson($value),
33+
default => (string) $value
34+
};
35+
}
36+
37+
private function getFileFromJson($value)
38+
{
39+
$data = json_decode($value, true);
40+
41+
if (!is_array($data) || !isset($data['model_type'], $data['model_id'])) {
42+
return $value;
43+
}
44+
45+
return $data['model_type']::find($data['model_id']);
46+
}
47+
48+
private function setFileAsJson($value)
49+
{
50+
if ($value instanceof File) {
51+
return json_encode([
52+
'model_type' => get_class($value),
53+
'model_id' => $value->id
54+
]);
55+
}
56+
57+
if (is_numeric($value)) {
58+
return json_encode([
59+
'model_type' => File::class,
60+
'model_id' => (int) $value
61+
]);
62+
}
63+
64+
return (string) $value;
65+
}
66+
}

src/Enums/FieldType.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Ssntpl\DataFields\Enums;
4+
5+
class FieldType
6+
{
7+
public const BOOLEAN = 'Boolean';
8+
public const CHECK = 'Check';
9+
public const TEXT = 'Text';
10+
public const NUMBER = 'Number';
11+
public const SINGLE = 'Single';
12+
public const MULTIPLE = 'Multiple';
13+
public const DATE = 'Date';
14+
public const TIME = 'Time';
15+
public const DATETIME = 'Datetime';
16+
public const FILE = 'File';
17+
public const JSON = 'Json';
18+
public const ARRAY = 'Array';
19+
20+
public static function getAllTypes()
21+
{
22+
return [
23+
self::BOOLEAN,
24+
self::CHECK,
25+
self::TEXT,
26+
self::NUMBER,
27+
self::SINGLE,
28+
self::MULTIPLE,
29+
self::DATE,
30+
self::TIME,
31+
self::DATETIME,
32+
self::FILE,
33+
self::JSON,
34+
self::ARRAY,
35+
];
36+
}
37+
}

src/Models/DataField.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
namespace Ssntpl\DataFields\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Ssntpl\DataFields\Casts\FieldValueCast;
67
use Ssntpl\DataFields\Traits\HasDataFields;
8+
use Ssntpl\LaravelFiles\Traits\HasFiles;
9+
710
class DataField extends Model
811
{
912
use HasDataFields;
13+
use HasFiles;
1014

1115
protected $fillable = [
1216
'id',
@@ -23,6 +27,8 @@ class DataField extends Model
2327

2428
protected $casts = [
2529
'validations' => 'array',
30+
'meta_data' => 'array',
31+
'value' => FieldValueCast::class,
2632
];
2733

2834
public function owner()

src/Models/DataSet.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class DataSet extends Model
1919
'meta_data',
2020
];
2121

22+
protected $casts = [
23+
'meta_data' => 'array',
24+
];
25+
2226
public function owner()
2327
{
2428
return $this->morphTo();

src/Traits/HasDataFields.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ trait HasDataFields
88
{
99
public function fields()
1010
{
11-
return $this->morphMany(config('data-fields.data_field_model', DataField::class), 'owner');
11+
return $this->morphMany($this->getDataFieldModel(), 'owner');
12+
}
13+
14+
protected function getDataFieldModel()
15+
{
16+
return config('data-fields.data_field_model', DataField::class);
1217
}
1318

1419
}

src/Traits/HasDataSets.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ trait HasDataSets
1010

1111
public function data_sets()
1212
{
13-
return $this->morphMany(config('data-fields.data_set_model', DataSet::class), 'owner');
13+
return $this->morphMany($this->getDataSetModel(), 'owner');
14+
}
15+
16+
protected function getDataSetModel()
17+
{
18+
return config('data-fields.data_set_model', DataSet::class);
1419
}
1520

1621
}

0 commit comments

Comments
 (0)