Skip to content

Commit 94f7dda

Browse files
Merge pull request #2 from ssntpl/feature/misc54_define_fields
add multifile option
2 parents 9278ca1 + c1e5ba9 commit 94f7dda

4 files changed

Lines changed: 105 additions & 63 deletions

File tree

src/Casts/FieldValueCast.php

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@
33
namespace Ssntpl\DataFields\Casts;
44

55
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
6-
use Ssntpl\DataFields\Enums\FieldType;
6+
use Ssntpl\DataFields\Models\DataField;
77
use Ssntpl\LaravelFiles\Models\File;
8+
use Carbon\Carbon;
89

910
class FieldValueCast implements CastsAttributes
1011
{
1112
public function get($model, string $key, $value, array $attributes)
1213
{
1314
if (is_null($value)) return null;
1415

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),
16+
return match($attributes['type'] ?? DataField::TEXT) {
17+
DataField::NUMBER => (float) $value,
18+
DataField::BOOL => (bool) $value,
19+
DataField::SELECT_MULTIPLE, DataField::ARRAY => json_decode($value, true),
20+
DataField::JSON => json_decode($value, true),
21+
DataField::FILE, DataField::FILES => $this->getFileFromJson($value),
22+
DataField::DATE => Carbon::parse($value)->toDateString(),
23+
DataField::TIME => Carbon::parse($value)->toTimeString(),
24+
DataField::DATETIME => Carbon::parse($value),
2125
default => (string) $value
2226
};
2327
}
@@ -26,10 +30,13 @@ public function set($model, string $key, $value, array $attributes)
2630
{
2731
if (is_null($value)) return null;
2832

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+
return match($attributes['type'] ?? DataField::TEXT) {
34+
DataField::SELECT_MULTIPLE, DataField::ARRAY, DataField::JSON => json_encode($value),
35+
DataField::BOOL => $value ? '1' : '0',
36+
DataField::FILE, DataField::FILES => $this->setFileAsJson($value),
37+
DataField::DATE => Carbon::parse($value)->toDateString(),
38+
DataField::TIME => Carbon::parse($value)->toTimeString(),
39+
DataField::DATETIME => Carbon::parse($value)->toDateTimeString(),
3340
default => (string) $value
3441
};
3542
}
@@ -38,15 +45,55 @@ private function getFileFromJson($value)
3845
{
3946
$data = json_decode($value, true);
4047

41-
if (!is_array($data) || !isset($data['model_type'], $data['model_id'])) {
48+
if (!is_array($data)) {
4249
return $value;
4350
}
4451

45-
return $data['model_type']::find($data['model_id']);
52+
try {
53+
// Handle multiple files
54+
if (isset($data[0]) && is_array($data[0])) {
55+
return collect($data)->map(function($item) {
56+
if (isset($item['model_type'], $item['model_id'])) {
57+
return $item['model_type']::find($item['model_id']);
58+
}
59+
return null;
60+
})->filter()->values()->all();
61+
}
62+
63+
// Handle single file
64+
if (isset($data['model_type'], $data['model_id'])) {
65+
return $data['model_type']::find($data['model_id']);
66+
}
67+
} catch (\Exception $e) {
68+
// Return original value if database connection fails
69+
return $value;
70+
}
71+
72+
return $value;
4673
}
4774

4875
private function setFileAsJson($value)
4976
{
77+
// Handle array of files
78+
if (is_array($value)) {
79+
return json_encode(collect($value)->map(function($file) {
80+
if ($file instanceof File) {
81+
return [
82+
'model_type' => get_class($file),
83+
'model_id' => $file->id
84+
];
85+
}
86+
if (is_numeric($file)) {
87+
return [
88+
'model_type' => File::class,
89+
'model_id' => (int) $file
90+
];
91+
}
92+
return null;
93+
})->filter()->values()->all());
94+
}
95+
96+
// Handle single file
5097
if ($value instanceof File) {
5198
return json_encode([
5299
'model_type' => get_class($value),

src/Enums/FieldType.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/Models/DataField.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,28 @@ class DataField extends Model
1111
{
1212
use HasDataFields;
1313
use HasFiles;
14+
15+
public const BOOL = 'bool';
16+
public const TEXT = 'text';
17+
public const NUMBER = 'number';
18+
public const SELECT_SINGLE = 'select_single';
19+
public const SELECT_MULTIPLE = 'select_multiple';
20+
public const DATE = 'date';
21+
public const TIME = 'time';
22+
public const DATETIME = 'datetime';
23+
public const FILE = 'file';
24+
public const FILES = 'files';
25+
public const JSON = 'json';
26+
public const ARRAY = 'array';
1427

28+
/**
29+
* Indicates if the model should be timestamped.
30+
*
31+
* @var bool
32+
*/
33+
public $timestamps = false;
34+
1535
protected $fillable = [
16-
'id',
1736
'owner_id',
1837
'owner_type',
1938
'description',
@@ -31,6 +50,24 @@ class DataField extends Model
3150
'value' => FieldValueCast::class,
3251
];
3352

53+
public static function getAllTypes()
54+
{
55+
return [
56+
self::BOOL,
57+
self::TEXT,
58+
self::NUMBER,
59+
self::SELECT_SINGLE,
60+
self::SELECT_MULTIPLE,
61+
self::DATE,
62+
self::TIME,
63+
self::DATETIME,
64+
self::FILE,
65+
self::FILES,
66+
self::JSON,
67+
self::ARRAY,
68+
];
69+
}
70+
3471
public function owner()
3572
{
3673
return $this->morphTo();
@@ -64,10 +101,4 @@ public function duplicate()
64101

65102
return $newDataSet;
66103
}
67-
/**
68-
* Indicates if the model should be timestamped.
69-
*
70-
* @var bool
71-
*/
72-
public $timestamps = false;
73104
}

src/Models/DataSet.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ class DataSet extends Model
99
{
1010
use HasDataFields;
1111

12+
/**
13+
* Indicates if the model should be timestamped.
14+
*
15+
* @var bool
16+
*/
17+
public $timestamps = false;
18+
1219
protected $fillable = [
1320
'id',
1421
'owner_id',
@@ -53,10 +60,4 @@ public function duplicate()
5360

5461
return $newDataSet;
5562
}
56-
/**
57-
* Indicates if the model should be timestamped.
58-
*
59-
* @var bool
60-
*/
61-
public $timestamps = false;
6263
}

0 commit comments

Comments
 (0)