| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Hechoenlaravel\JarvisFoundation\UI\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use JavaScript; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Hechoenlaravel\JarvisFoundation\Field\FieldTypes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Hechoenlaravel\JarvisFoundation\FieldGenerator\FieldModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Hechoenlaravel\JarvisFoundation\EntityGenerator\EntityModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Class FormBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package Hechoenlaravel\JarvisFoundation\UI\Field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class FormBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * The entity the field is being created for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var EntityModel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * URL to return to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * Field Types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var FieldTypes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $types; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * When edit, it should have a model to work with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Is the edit form? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     protected $isEdit = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Field Type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     protected $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Create a new Object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param EntityModel $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function __construct(EntityModel $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->entity = $entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->types = app('field.types'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * set the return URL after the post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param bool $url | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     public function setReturnUrl($url = false) | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $this->url = $url; | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param FieldModel $model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function setModel(FieldModel $model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->model = $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->isEdit = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->type = $this->types->getFieldClass($model->type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * render the form to add the field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function render() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if (!$this->isEdit) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $view = view('jarvisPlatform::field.admin.fieldform') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 ->with('entity', $this->entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 ->with('returnUrl', $this->url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 ->with('types', $this->getFieldTypes()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 ->render(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             return $view; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         JavaScript::put([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             'fieldForm' => $this->model->transformed()->toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $view = view('jarvisPlatform::field.admin.fieldformedit') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ->with('entity', $this->entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ->with('returnUrl', $this->url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ->with('fieldAssignmentForm', $this->type->getOptionsForm()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ->with('types', $this->getFieldTypes()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             ->render(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return $view; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Get the Field Types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     public function getFieldTypes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $types = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         foreach ($this->types->types as $type => $class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $c = app($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $types[$type] = $c->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return $types; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                        
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..