33namespace Ssntpl \DataFields \Casts ;
44
55use Illuminate \Contracts \Database \Eloquent \CastsAttributes ;
6- use Ssntpl \DataFields \Enums \ FieldType ;
6+ use Ssntpl \DataFields \Models \ DataField ;
77use Ssntpl \LaravelFiles \Models \File ;
8+ use Carbon \Carbon ;
89
910class 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 ),
0 commit comments