예제 #1
0
 /**
  * Default text style parser
  *
  * @param string               $string The string with the style properties
  * @param Jm_Console_TextStyle $style  The style which properites
  *                                     should be set
  *
  * @return void
  */
 protected function defaultTextStyleParser($string, $style)
 {
     foreach (explode(',', $string) as $statement) {
         $statement = trim($statement);
         $keyval = explode(':', $statement);
         if (count($keyval) < 2) {
             // it's a simple statement
             if (in_array($statement, array('black', 'red', 'green', 'yellow', 'blue', 'purple', 'cyan', 'white', 'default'))) {
                 $style->setForegroundColor($this->colornames[$statement]);
                 continue;
             } elseif (in_array($statement, array('bold', 'light', 'italic', 'underline', 'blink_slow', 'blink_rapid', 'blink', 'reverse', 'hidden'))) {
                 $style->setTextDecoration($this->decorations[$statement]);
                 continue;
             }
             // if its not a color or a text decoration it is an error
             throw new Jm_Console_TextStyleException('Failed to parse the style identifier \'' . $string . '\'' . '. Unknown statement \'' . $statement . '\'');
         }
         // fully qualified statemens have a key and a value
         // separated by a ':'
         list($key, $value) = $keyval;
         switch ($key) {
             case 'fg':
                 if (!isset($this->colornames[$value])) {
                     throw new Jm_Console_TextStyleException(sprintf('Failed to parse the style identifier \'%s\'' . '. Unknown foreground color value \'%s\'', $string, $value));
                 }
                 $style->setForegroundColor($this->colornames[$value]);
                 break;
             case 'bg':
                 if (!isset($this->colornames[$value])) {
                     throw new Jm_Console_TextStyleException(sprintf('Failed to parse the style identifier \'%s\'' . '. Unknown text decoration value \'%s\'', $string, $value));
                 }
                 $style->setBackgroundColor($this->colornames[$value]);
                 break;
             case 'td':
                 if (!isset($this->decorations[$value])) {
                     throw new Jm_Console_TextStyleException(sprintf('Failed to parse the style identifier \'%s\'' . '. Unknown text decoration value \'%s\'', $string, $value));
                 }
                 $style->setTextDecoration($this->decorations[$value]);
                 break;
             default:
                 // if we reached this point something failed
                 throw new Jm_Console_TextStyleException(sprintf('Failed to parse the style identifier \'%s\'' . '. Unknown text style property \'%s\'', $string, $key));
         }
     }
 }