| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Thinktomorrow\Chief\Fragments; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Thinktomorrow\Chief\Fields\Fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Thinktomorrow\Chief\Fields\Types\AbstractField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Thinktomorrow\Chief\Fields\Types\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Thinktomorrow\Chief\Fields\Types\FieldType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Thinktomorrow\Chief\Fields\Types\MediaField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class FragmentField extends AbstractField implements Field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** @var Fields */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var null|string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $fragmentLabel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 43 |  |     /** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $max; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 43 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 43 |  |     public static function make(string $key, Fields $fields): Field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         return (new static(new FieldType(FieldType::FRAGMENT), $key)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 43 |  |             ->fields($fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 43 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private function fields(Fields $fields) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 43 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->fields = $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function getFields(): Fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return $this->fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |      * @param string|null $locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return Fields[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |     public function getFragments(?string $locale = null): array | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         // By default there is one empty Fragment provided to the user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |         $fragments = [Fragment::empty($this->getKey())]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         // Model is auto-injected by Manager::editFields() method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (($this->model)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             if (!method_exists($this->model, 'getFragments')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |                 throw new \RuntimeException(get_class($this->model) . ' is missing the ' . HasFragments::class . ' trait.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             if (count($modelFragments = $this->model->getFragments($this->getKey())) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 $fragments = $modelFragments->map(function (FragmentModel $fragmentModel) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     return Fragment::fromModel($fragmentModel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 })->all(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         foreach ($fragments as $k => $fragment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |             $fragments[$k] = $fragments[$k] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |                 ->setModelIdInputName($this->name . '[' . $k . '][modelId]') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 ->setFields($this->fields->clone()->map(function (Field $field) use ($k, $fragment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |                     return $field->name($this->name . '.' . $k . '.' . $field->getName()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                                  ->localizedFormat(':name.:locale') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                                  ->valueResolver(function ($model = null, $locale = null, $field) use ($fragment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                                      if (isset($field->value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |                                          return $field->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                                      } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                                      if ($field instanceof MediaField) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                                          if (!$fragment->hasModelId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                                              return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                                          } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |                                          return $field->getMedia(FragmentModel::find($fragment->getModelId()), $locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |                                      } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                                      return $fragment->getValue($field->getColumn(), $locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                                  }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 2 |  |                 })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $fragments; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * A fragmentlabel is shown in admin as the label above each fragment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param string $fragmentLabel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return $this | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     public function fragmentLabel(string $fragmentLabel): self | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         $this->fragmentLabel = $fragmentLabel; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function getFragmentLabel(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |         return $this->fragmentLabel ?? 'fragment'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     public function getDuplicatableFields(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         if (count($this->getFragments()) < 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |         // Take the fields from the first fragment as a starting point for duplication | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return array_map(function (\Thinktomorrow\Chief\Fields\Types\Field $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             return $field->valueResolver(function ($model = null, $locale = null, $field) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |                 if ($field instanceof \Thinktomorrow\Chief\Fields\Types\MediaField) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 2 |  |                     return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             })->render(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         }, $this->getFragments()[0]->getFields()->clone()->all()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     public function max(int $max) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->max = $max; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function getMax(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return $this->max ?: 50; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |  | 
            
                        
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.