| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Mindtwo\DynamicMutators\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Mindtwo\DynamicMutators\Handler\Handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Mindtwo\DynamicMutators\Interfaces\MutationHandlerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | trait HasDynamicMutators | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * Registered mutation handler. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var MutationHandlerInterface[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private static $mutation_handlers = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * Set attribute. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param mixed  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return self|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function setAttribute($name, $value): ?self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         foreach (self::$mutation_handlers as $handler) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $handler->setModel($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             if ($handler->hasGetMutator($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 $result = $handler->callSetMutator($name, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 if (! $handler->shouldStack(MutationHandlerInterface::OPERATOR_SET)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                     return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         return parent::setAttribute($name, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Get attribute. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param mixed $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function getAttribute($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         foreach (self::$mutation_handlers as $controller) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $controller->setModel($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             if ($controller->hasGetMutator($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 return $controller->callGetMutator($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return parent::getAttribute($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * Register a mutation handler. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param MutationHandlerInterface $handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public static function registerMutationHandler(MutationHandlerInterface $handler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         self::$mutation_handlers[] = $handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * Magic call method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param $arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |     public function __call($name, $arguments) | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |         if (preg_match('/^(set|get)(.+)Attribute$/', $name, $matches)) { | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |             $key = snake_case($matches[2]); | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |             foreach (self::$mutation_handlers as $handler) { | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |                 $handler->setModel($this); | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |                 if ($handler->hasGetMutator($key)) { | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |                     return $handler->callGetMutator($key); | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         return parent::__call($name, $arguments); | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  |