| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Mediadevs\ResponseHandler; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 5 |  |  | class Response | 
            
                                                                        
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  |      * Constants for response levels. Order: LOW -> HIGH | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     const DEFAULT       = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     const INFO          = 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     const SUCCESS       = 2; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     const WARNING       = 3; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     const ERROR         = 4; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * Uses bootstrap class names to define alert type | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     private $levels = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         self::DEFAULT   => 'Default', | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         self::INFO      => 'Information', | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         self::SUCCESS   => 'Success', | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         self::WARNING   => 'Warning', | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         self::ERROR     => 'Error', | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * Delimiters for wrapping the variables | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     private $delimiters = array(0 => '', 1 => ''); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * Responses will be stored in here | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     private $responses = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * Handling response messages and stores them inside $this->responses. | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * @param string      $message | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param array       $parameters | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param int         $level | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @return Response | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     public function add( | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         string  $message, | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         array   $parameters = array(), | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         int     $level      = self::DEFAULT | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     ): self | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         foreach ($parameters as $key => $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             // Wrapping the delimiters around the value | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             $value = $this->delimiters[0] . $value . $this->delimiters[1]; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             // Replacing the placeholders in the message with the parameters | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $message = str_replace('{%' . $key . '%}', $value, $message); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         // Adding this response to the response array | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $this->responses[] = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             'level'     => $this->levels[$level], | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             'message'   => $message | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      * @param string      $left | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * @param string|null $right | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * @return Response | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     public function delimiters(string $left, string $right = null) : self | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         // Checking whether the highlighting is enabled, else clearing the values. | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         $this->delimiters[0] = $left; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $this->delimiters[1] = $right ?? $left; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |        return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * Changing the label for the defined level | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @param int    $level | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @param string $label | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * @return Response | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     public function changeLabelForLevel(int $level, string $label) : self | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         if (array_key_exists($level, $this->levels)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |             $this->levels[$level] = $label; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * Returns the error messages as JSON. | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function toJSON() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |         return json_encode($this->responses, JSON_PRETTY_PRINT); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      * Returns the error messages as an Array. | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     public function toArray() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  |         return $this->responses; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 119 |  |  | } |