| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Prateekkarki\Laragen\Models\Types; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Prateekkarki\Laragen\Models\TypeResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |   * @method integer getSize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |   * @method array getPivotColumns() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |   */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | abstract class LaragenType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $unique; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $required; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $isDisplay; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $dataType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $formType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $stubs = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $size = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $validationRule = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $moduleName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $columnName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $optionString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $optionArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $typeOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function __construct($moduleName, $columnName, $optionString) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->moduleName = $moduleName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->columnName = $columnName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->optionString = $optionString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->optionArray = is_string($optionString) ? explode('|', $optionString) : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $typePieces = array_shift($this->optionArray); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $type = explode(':', $typePieces); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->typeOption = is_array($type) && count($type) >= 2 ? $type[1] : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if (in_array(TypeResolver::COLUMN_UNIQUE, $this->optionArray)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $this->setUnique(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if (in_array(TypeResolver::COLUMN_REQUIRED, $this->optionArray)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $this->setRequired(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (in_array("*", $this->optionArray)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $this->setIsDisplay(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     protected function __call($method, $params) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $var = lcfirst(substr($method, 3)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (strncasecmp($method, "get", 3) === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             return property_exists($this, $var) ? $this->$var : ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if (strncasecmp($method, "set", 3) === 0 && isset($params[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $this->$var = $params[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         return property_exists($this, $method) ? $this->$method : ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function getSchema() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $schema = '$table->'.$this->getDataType()."('{$this->getColumn()}'"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $schema .= $this->getSize() ? ", {$this->getSize()})" : ")"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $schema .= $this->isUnique() ? "->unique()" : ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $schema .= $this->isRequired() ? "" : "->nullable()"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $schema .= ";"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function getFilteredColumns($options = [], $columnsOnly = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $filteredTypes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $options = is_array($options) ? $options : [$options]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         foreach($this->getPivotColumns() as $type){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             foreach ($options as $option) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 if($type->$option()){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     $filteredTypes[] = $columnsOnly ? $type->getColumn() : $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return $filteredTypes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function getFormOptions() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $options = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $options .= $this->isRequired() ? 'required="required" ' : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function getForeignKey() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $this->columnName . "_id"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function getRelatedModel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return $this->getChildModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function getRelatedModule() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return Str::plural(strtolower(Str::snake($this->getRelatedModel()))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function getRelatedModelLowercase() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return strtolower($this->getRelatedModel()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     public function getChildModel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return ucfirst(Str::camel(Str::singular($this->typeOption ?? $this->columnName ))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function getParentModel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         return ucfirst(Str::camel(Str::singular($this->moduleName))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public function getParentModule() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return $this->moduleName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     public function getParentModelLowercase() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return Str::singular($this->moduleName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function getStub($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         return isset($this->stubs[$type]) ? $this->stubs[$type] : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     public function getTextRows() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         if (!$this->size) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |            return 4; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         return floor($this->getsize() / 120); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     public function isUnique() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         return $this->unique; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     public function isRequired() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         return $this->required; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     public function getDisplay() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         return Str::title(str_replace("_", " ", $this->columnName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     public function getColumn() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         return $this->columnName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     public function getColumnKey() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         return $this->columnName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     protected function setOptions($optionType, $optionParam) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         switch ($optionType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             case 'max': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 $this->setSize($optionParam); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 $this->$optionType = $optionParam; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     public function getTabs($number) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         $schema = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         for ($i = 0; $i < $number; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             $schema .= "    "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         return $schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  |