Back to ColorStyle class

Method fromString

public static $this
fromString
(string $string)
Create a color style from a parameter string.
Parameters
  • string $string The parameter string.
Returns
  • $this
Since
  • 4.0.0
-
  • \RuntimeException
Class: ColorStyle
Project: Joomla

Method fromString - Source code

/**
 * Create a color style from a parameter string.
 *
 * Example: fg=red;bg=blue;options=bold,blink
 *
 * @param   string  $string  The parameter string.
 *
 * @return  $this
 *
 * @since   4.0.0
 * @throws  \RuntimeException
 */
public static function fromString(string $string) : self
{
    $fg = '';
    $bg = '';
    $options = [];
    $parts = explode(';', $string);
    foreach ($parts as $part) {
        $subParts = explode('=', $part);
        if (\count($subParts) < 2) {
            continue;
        }
        switch ($subParts[0]) {
            case 'fg':
                $fg = $subParts[1];
                break;
            case 'bg':
                $bg = $subParts[1];
                break;
            case 'options':
                $options = explode(',', $subParts[1]);
                break;
            default:
                throw new \RuntimeException('Invalid option: ' . $subParts[0]);
        }
    }
    return new self($fg, $bg, $options);
}