| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Thinkone\NovaPageSettings\QueryAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Thinkone\NovaPageSettings\Templates\SettingsTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | abstract class InternalSettingsModel extends Model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     const ATTR_CLASS = '__nps_class'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     const ATTR_ID    = 'id'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     const ATTR_NAME  = '__nps_name'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $_buffer = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @return class-string \Thinkone\NovaPageSettings\Model | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     abstract public function getDBModel(): string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 3 |  |     public function getTemplatesPath(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 3 |  |         return config('nova-page-settings.default.templates_path'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |     public function keyPrefix(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  |         return config('nova-page-settings.key_prefix'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 3 |  |     public function getTable(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 3 |  |         return $this->getTemplatesPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 3 |  |     protected function newBaseQueryBuilder(): InternalSettingsQueryBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 3 |  |         return new InternalSettingsQueryBuilder( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 3 |  |             new InternalSettingsConnection(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |             new InternalSettingsGrammar(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |             new InternalSettingsProcessor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 3 |  |     public function newEloquentBuilder($query): SettingsQueryBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  |         return (new SettingsQueryBuilder($query))->setModel($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |     public function template(): ?SettingsTemplate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |         if ($class = $this->attributes[self::ATTR_CLASS]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |             return new $class(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 3 |  |     protected function predefinedKeys(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 3 |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 3 |  |             self::ATTR_CLASS, | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 3 |  |             self::ATTR_ID, | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 3 |  |             self::ATTR_NAME, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 3 |  |     public function getAttribute($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 3 |  |         if (!in_array($key, $this->predefinedKeys()) && Str::startsWith($key, static::keyPrefix())) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |             $newKey = Str::after($key, static::keyPrefix()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |             if (!array_key_exists($newKey, $this->_buffer)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |                 $template = $this->template(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 /** @psalm-suppress UndefinedClass */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |                 $model    = $this->getDBModel()::query() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |                     ->page($template::getSlug()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |                     ->key($newKey)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |                 if ($model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     $this->_buffer[$newKey] = $template->mutateAttribute($newKey, $model->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |                     $this->_buffer[$newKey] = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |             return $this->_buffer[$newKey]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 3 |  |         return parent::getAttribute($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |     public function setAttribute($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         if (!in_array($key, $this->predefinedKeys()) && Str::startsWith($key, static::keyPrefix())) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $newKey = Str::after($key, static::keyPrefix()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $this->_buffer[$newKey] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |         return parent::setAttribute($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function save(array $options = []): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         /** @var SettingsTemplate $template */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $template = $this->template(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if ($template) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $page = $template::getSlug(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             foreach ($this->_buffer as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $this->getDBModel()::updateOrCreate( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                     ['page' => $page, 'key' => $key], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                     ['value' => $value] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  |