| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Matks\Vivian\Color; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Matks\Vivian\Output\TextElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Foreground Color manager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Thanks Thijs Lensselink | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @link http://blog.lenss.nl/2012/05/adding-colors-to-php-cli-script-output/ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | class TextColorManager extends AbstractColorManager | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     const BLACK  = 'black'; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     const RED    = 'red'; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     const GREEN  = 'green'; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     const YELLOW = 'yellow'; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     const BLUE   = 'blue'; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     const PURPLE = 'purple'; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     const CYAN   = 'cyan'; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     const WHITE  = 'white'; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     const BASH_FOREGROUND_BLACK  = 30; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     const BASH_FOREGROUND_RED    = 31; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     const BASH_FOREGROUND_GREEN  = 32; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     const BASH_FOREGROUND_YELLOW = 33; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     const BASH_FOREGROUND_BLUE   = 34; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     const BASH_FOREGROUND_PURPLE = 35; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     const BASH_FOREGROUND_CYAN   = 36; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     const BASH_FOREGROUND_WHITE  = 37; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * Static calls interface to allow calls such as green('hello'); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public static function __callstatic($name, $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |         $knownColors = static::getKnownColors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |         $colorID     = $knownColors[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |         $color = static::color($colorID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // target string is expected to be: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         $targetString = $params[0][0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |         $element = new TextElement($targetString); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         $element->setTextColor($color); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |         return $element->render(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * Get allowed colors | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @return string[] | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public static function getKnownColors() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $colors = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |             static::BLACK  => static::BASH_FOREGROUND_BLACK, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |             static::RED    => static::BASH_FOREGROUND_RED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |             static::GREEN  => static::BASH_FOREGROUND_GREEN, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |             static::YELLOW => static::BASH_FOREGROUND_YELLOW, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |             static::BLUE   => static::BASH_FOREGROUND_BLUE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |             static::PURPLE => static::BASH_FOREGROUND_PURPLE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |             static::CYAN   => static::BASH_FOREGROUND_CYAN, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |             static::WHITE  => static::BASH_FOREGROUND_WHITE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         return $colors; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * Get Color class | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public static function getColorClass() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         return '\Matks\Vivian\Color\TextColor'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  |