| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace NovaFlexibleContent\Layouts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Collection as BaseCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Laravel\Nova\Makeable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use NovaFlexibleContent\Layouts\Collections\LayoutsCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class ContentToFlexibleCollectionTransformer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     use Makeable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 | 4 |  |     public function transform(mixed $value, array|Preset $layoutMapping = []): LayoutsCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 4 |  |         $flexible = $this->getFlexibleArrayFromValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 4 |  |         if (is_null($flexible)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 2 |  |             return LayoutsCollection::make(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 3 |  |         return LayoutsCollection::make($this->getMappedFlexibleLayouts($flexible, $layoutMapping))->filter()->values(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Transform incoming value into an array of usable layouts. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 4 |  |     protected function getFlexibleArrayFromValue(mixed $value): ?array | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 4 |  |         if (is_string($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 1 |  |             $value = json_decode($value, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 1 |  |             return is_array($value) ? $value : null; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 4 |  |         if (is_a($value, BaseCollection::class)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 2 |  |             return $value->all(); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 2 |  |         if (is_array($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 1 |  |             return $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 2 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Map array with Flexible Content Layouts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  |     protected function getMappedFlexibleLayouts(array $flexible, array|Preset $layoutMapping = []): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |         return array_map(function ($item) use ($layoutMapping) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |             return $this->getMappedLayout($item, $layoutMapping); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |         }, $flexible); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Transform given layout value into a usable Layout instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |     protected function getMappedLayout(mixed $item, array|Preset $layoutMapping = []): ?Layout | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |         $name       = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |         $key        = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |         $attributes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |         if (is_a($item, \stdClass::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |             $item = json_decode(json_encode($item), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |         if (is_string($item)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |             $item = json_decode($item, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 3 |  |         if (is_array($item)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |             $name       = $item['layout']             ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 3 |  |             $key        = $item['key']                ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 3 |  |             $attributes = (array) $item['attributes'] ?? []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         } elseif (is_a($item, Layout::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |             $name       = $item->name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |             $key        = (string) $item->key(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |             $attributes = $item->getAttributes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |         if (!$name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 3 |  |         return $this->createMappedLayout($name, $key, $attributes, $layoutMapping); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * Transform given layout value into a usable Layout instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 3 |  |     protected function createMappedLayout(string $name, string $key, array $attributes, array|Preset $layoutMapping = []): Layout | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 3 |  |         if($layoutMapping instanceof Preset) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |             $layoutMapping = $layoutMapping->layouts(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 3 |  |         $classname = array_key_exists($name, $layoutMapping) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 3 |  |             ? $layoutMapping[$name] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |             : Layout::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 3 |  |         return new $classname($name, $name, [], $key, $attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |