| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SleepingOwl\Admin\Display; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Eloquent\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use KodiComponents\Support\HtmlAttributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SleepingOwl\Admin\Contracts\ColumnInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SleepingOwl\Admin\Contracts\Display\TableHeaderColumnInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SleepingOwl\Admin\Contracts\WithModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SleepingOwl\Admin\Display\Column\OrderByClause; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SleepingOwl\Admin\Traits\Assets; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SleepingOwl\Admin\Traits\Renderable; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | abstract class TableColumn implements ColumnInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     use HtmlAttributes, Assets, Renderable; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * Column header. | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @var TableHeaderColumnInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     protected $header; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * Model instance currently rendering. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @var Model | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     protected $model; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * Column appendant. | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @var ColumnInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     protected $append; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * Column width. | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     protected $width = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @var OrderByClauseInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     protected $orderByClause; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * TableColumn constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param string|null $label | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function __construct($label = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->header = app(TableHeaderColumnInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (! is_null($label)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $this->setLabel($label); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->initializePackage(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * Initialize column. | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function initialize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->includePackage(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @return TableHeaderColumnInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function getHeader() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this->header; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @return int | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function getWidth() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $this->width; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * @param int|string $width | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function setWidth($width) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if (is_int($width)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $width = $width.'px'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->width = $width; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      * @return ColumnInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function getAppends() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return $this->append; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      * @param ColumnInterface $append | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     public function append(ColumnInterface $append) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $this->append = $append; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      * @return Model $model | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function getModel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return $this->model; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * @param Model $model | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     public function setModel(Model $model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $this->model = $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $append = $this->getAppends(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         if ($append instanceof WithModel) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             $append->setModel($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |      * Set column header label. | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |      * @param string $title | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     public function setLabel($title) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $this->getHeader()->setTitle($title); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |      * @param OrderByClauseInterface|bool|string|\Closure $orderable | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     public function setOrderable($orderable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         if ($orderable instanceof \Closure || is_string($orderable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             $orderable = new OrderByClause($orderable); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             throw new \InvalidArgumentException('Argument must be instance of SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface interface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         $this->orderByClause = $orderable; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         $this->getHeader()->setOrderable($this->isOrderable()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |      * Check if column is orderable. | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     public function isOrderable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         return $this->orderByClause instanceof OrderByClauseInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 197 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 199 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 200 |  |  |      * @param Builder $query | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  |      * @param string $direction | 
            
                                                                        
                            
            
                                    
            
            
                | 202 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 203 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 204 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     public function orderBy(Builder $query, $direction) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         if (! $this->isOrderable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |             throw new \InvalidArgumentException('Argument must be instance of SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface interface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         $this->orderByClause->modifyQuery($query, $direction); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 214 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 216 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 217 |  |  |      * Get the instance as an array. | 
            
                                                                        
                            
            
                                    
            
            
                | 218 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 219 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 220 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |     public function toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |             'attributes' => $this->htmlAttributesToString(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |             'model'      => $this->getModel(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |             'append' => $this->getAppends(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |         ]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 228 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 229 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 230 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 231 |  |  |      * Get related model configuration. | 
            
                                                                        
                            
            
                                    
            
            
                | 232 |  |  |      * @return ModelConfigurationInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 233 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |     protected function getModelConfiguration() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |         return app('sleeping_owl')->getModel(get_class($this->getModel())); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 237 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 238 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 239 |  |  |  | 
            
                        
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.