| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace A17\Twill\Services\MediaLibrary; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Base class to implement image service parameter compatibility. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This class was introduced along with the TwicPics image service to implement a basic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * compatibility layer with Imgix-type parameters. There are a few instances in the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Twill and Twill Image [1] source code where parameters were hardcoded, such as: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * - `w` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * - `h` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * - `fm` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * - `q` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * - `fit=crop` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * This was adopted internally as the minimum set of parameters for which | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Twill image services need to provide compatibility. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * [1] https://github.com/area17/twill-image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @see TwicPicsParamsProcessor | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | abstract class AbstractParamsProcessor | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     const COMPATIBLE_PARAMS = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         'w' => 'width', | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         'h' => 'height', | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         'fm' => 'format', | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         'q' => 'quality', | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         'fit' => 'fit', | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     protected $params; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     protected $width; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     protected $height; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     protected $format; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     protected $quality; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     protected $fit; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * Abstract method to be implemented in concrete params processor classes. | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * This method is called after all parameters have been processed and | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * must return a finalized params array to generate the image URL. | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     abstract public function finalizeParams(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * Receives the original params array and calls the appropriate handler method | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * for each param. Custom handlers can be defined by following this naming | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * convention: `handleParamNAME`, where NAME is the name of the param. | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @param array $params | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     public function process($params) | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $this->params = $params; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         foreach ($params as $key => $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             $handler = "handleParam{$key}"; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             if (method_exists($this, $handler)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 $this->{$handler}($key, $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                 $this->handleParam($key, $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         return $this->finalizeParams(); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * The generic param handler. Known parameter values will be extracted into the | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * corresponding properties as defined in COMPATIBLE_PARAMS. Unknown params | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * will remain untouched. | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param string $key | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * @param mixed $value | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     protected function handleParam($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if ($property = static::COMPATIBLE_PARAMS[$key] ?? false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $this->{$property} = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |             unset($this->params[$key]); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  |