66
77use Illuminate \Database \Eloquent \Model ;
88use Illuminate \Support \Collection ;
9+ use Livewire \Wireable ;
910use Relaticle \Flowforge \Contracts \IKanbanAdapter ;
1011
11- class DefaultKanbanAdapter implements IKanbanAdapter
12+ class DefaultKanbanAdapter implements IKanbanAdapter, Wireable
1213{
1314 /**
1415 * The status field for the model.
@@ -196,28 +197,28 @@ public function createCard(array $attributes): ?Model
196197 {
197198 $ modelClass = $ this ->getModel ();
198199 $ card = new $ modelClass ();
199-
200+
200201 // Set status if provided, otherwise use the first status as default
201202 $ status = $ attributes [$ this ->getStatusField ()] ?? array_key_first ($ this ->getStatusValues ());
202203 $ card ->{$ this ->getStatusField ()} = $ status ;
203-
204+
204205 // Set title
205206 if (isset ($ attributes [$ this ->getTitleAttribute ()])) {
206207 $ card ->{$ this ->getTitleAttribute ()} = $ attributes [$ this ->getTitleAttribute ()];
207208 }
208-
209+
209210 // Set description if the attribute exists
210211 if ($ this ->getDescriptionAttribute () && isset ($ attributes [$ this ->getDescriptionAttribute ()])) {
211212 $ card ->{$ this ->getDescriptionAttribute ()} = $ attributes [$ this ->getDescriptionAttribute ()];
212213 }
213-
214+
214215 // Set additional card attributes
215216 foreach ($ this ->getCardAttributes () as $ attribute ) {
216217 if (isset ($ attributes [$ attribute ])) {
217218 $ card ->{$ attribute } = $ attributes [$ attribute ];
218219 }
219220 }
220-
221+
221222 return $ card ->save () ? $ card : null ;
222223 }
223224
@@ -234,24 +235,24 @@ public function updateCard(Model $card, array $attributes): bool
234235 if (isset ($ attributes [$ this ->getStatusField ()])) {
235236 $ card ->{$ this ->getStatusField ()} = $ attributes [$ this ->getStatusField ()];
236237 }
237-
238+
238239 // Update title if provided
239240 if (isset ($ attributes [$ this ->getTitleAttribute ()])) {
240241 $ card ->{$ this ->getTitleAttribute ()} = $ attributes [$ this ->getTitleAttribute ()];
241242 }
242-
243+
243244 // Update description if provided and the attribute exists
244245 if ($ this ->getDescriptionAttribute () && isset ($ attributes [$ this ->getDescriptionAttribute ()])) {
245246 $ card ->{$ this ->getDescriptionAttribute ()} = $ attributes [$ this ->getDescriptionAttribute ()];
246247 }
247-
248+
248249 // Update additional card attributes
249250 foreach ($ this ->getCardAttributes () as $ attribute ) {
250251 if (isset ($ attributes [$ attribute ])) {
251252 $ card ->{$ attribute } = $ attributes [$ attribute ];
252253 }
253254 }
254-
255+
255256 return $ card ->save ();
256257 }
257258
@@ -265,4 +266,39 @@ public function deleteCard(Model $card): bool
265266 {
266267 return $ card ->delete ();
267268 }
269+
270+ /**
271+ * Convert the adapter to a Livewire-compatible array.
272+ *
273+ * @return array<string, mixed>
274+ */
275+ public function toLivewire (): array
276+ {
277+ return [
278+ 'model ' => $ this ->getModel (),
279+ 'statusField ' => $ this ->getStatusField (),
280+ 'statusValues ' => $ this ->getStatusValues (),
281+ 'titleAttribute ' => $ this ->getTitleAttribute (),
282+ 'descriptionAttribute ' => $ this ->getDescriptionAttribute (),
283+ 'cardAttributes ' => $ this ->getCardAttributes (),
284+ ];
285+ }
286+
287+ /**
288+ * Create a new adapter instance from a Livewire-compatible array.
289+ *
290+ * @param array<string, mixed> $value The Livewire-compatible array
291+ * @return static
292+ */
293+ public static function fromLivewire ($ value )
294+ {
295+ return new static (
296+ $ value ['model ' ],
297+ $ value ['statusField ' ],
298+ $ value ['statusValues ' ],
299+ $ value ['titleAttribute ' ],
300+ $ value ['descriptionAttribute ' ] ?? null ,
301+ $ value ['cardAttributes ' ] ?? []
302+ );
303+ }
268304}
0 commit comments