| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Glorand\Model\Settings\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Glorand\Model\Settings\Contracts\SettingsManagerContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Glorand\Model\Settings\Exceptions\ModelSettingsException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Glorand\Model\Settings\Managers\FieldSettingsManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\Facades\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Support\Facades\Schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Trait HasSettingsField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package Glorand\Model\Settings\Traits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @property array $settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @property string $settingsFieldName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @property boolean $persistSettings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @SuppressWarnings(PHPMD.StaticAccess) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | trait HasSettingsField | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 1 |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     use HasSettings; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     private $persistSettings = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 234 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     protected static function bootHasSettingsField() | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 156 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         static::saving(function ($model) { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 234 |  |             /** @var self $model */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 234 |  |             $model->fixSettingsValue(); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 234 |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @throws ModelSettingsException | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 111 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     public function settings(): SettingsManagerContract | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 111 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         return new FieldSettingsManager($this); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 234 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     public function fixSettingsValue() | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 234 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 234 |  |         $settingsFieldName = $this->getSettingsFieldName(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 234 |  |         $attributes = $this->getAttributes(); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 96 |  |         if (Arr::has($attributes, $settingsFieldName)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 6 |  |             if (is_array($this->$settingsFieldName)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 $this->$settingsFieldName = json_encode($this->$settingsFieldName); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 234 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @throws ModelSettingsException | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 111 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     public function getSettingsValue(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 111 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 111 |  |         $settingsFieldName = $this->getSettingsFieldName(); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 9 |  |         if (!$this->hasSettingsField()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}"); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 102 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $value = json_decode($this->getAttributeValue($settingsFieldName) ?? '[]', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 102 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         return is_array($value) ? $value : []; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 234 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     public function getSettingsFieldName(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 234 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         return $this->settingsFieldName ?? config('model_settings.settings_field_name'); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 234 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     public function getConnectionName(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 234 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         return $this->connection ?? config('database.default'); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 87 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function isPersistSettings(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 87 |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  |         return boolval($this->persistSettings ?? config('model_settings.settings_persistent')); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * @param bool $val | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 6 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     public function setPersistSettings(bool $val = true) | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 6 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 6 |  |         $this->persistSettings = $val; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 111 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     private function hasSettingsField() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 111 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 111 |  |         return Cache::remember( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 111 |  |             config('model_settings.settings_table_cache_prefix') . '::has_field', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 74 |  |             now()->addDays(1), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 111 |  |             function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 111 |  |                 return Schema::connection($this->getConnectionName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 111 |  |                     ->hasColumn( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 111 |  |                         $this->getTable(), | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |                         $this->getSettingsFieldName() | 
            
                                                                        
                                                                
            
                                    
            
            
                | 116 | 111 |  |                     ); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 117 |  |  |             } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 118 |  |  |         ); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 121 |  |  |     abstract public function getTable(); | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |     abstract public function getAttributes(); | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |     abstract public function getAttributeValue($key); | 
            
                                                        
            
                                    
            
            
                | 126 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |  |