1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160:
<?php
/*
* This file is part of the webmozart/console package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webmozart\Console\Api\Formatter;
use LogicException;
use OutOfBoundsException;
/**
* A set of styles used by the formatter.
*
* @since 1.0
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class StyleSet
{
/**
* @var Style[]
*/
private $styles = array();
/**
* Creates a new style set.
*
* @param Style[] $styles The styles to add.
*/
public function __construct(array $styles = array())
{
$this->replace($styles);
}
/**
* Adds a style.
*
* @param Style $style The style to add.
*
* @throws LogicException If the tag of the style is not set.
*/
public function add(Style $style)
{
if (!$style->getTag()) {
throw new LogicException('The tag of a style added to the style set must be set.');
}
$this->styles[$style->getTag()] = $style;
}
/**
* Adds styles to the style set.
*
* Existing styles are preserved.
*
* @param Style[] $styles The styles to add.
*/
public function merge(array $styles)
{
foreach ($styles as $style) {
$this->add($style);
}
}
/**
* Sets the styles of the style set.
*
* Existing styles are removed.
*
* @param Style[] $styles The styles to set.
*/
public function replace(array $styles)
{
$this->styles = array();
$this->merge($styles);
}
/**
* Removes a style.
*
* This method does nothing if the tag does not exist.
*
* @param string $tag The tag of the style.
*/
public function remove($tag)
{
unset($this->styles[$tag]);
}
/**
* Clears the contents of the style set.
*/
public function clear()
{
$this->styles = array();
}
/**
* Returns whether the style with the given tag exists.
*
* @param string $tag The tag of the style.
*
* @return bool Returns `true` if a style with the given tag exists and
* `false` otherwise.
*/
public function contains($tag)
{
return isset($this->styles[$tag]);
}
/**
* Returns whether the style set is empty.
*
* @return bool Returns `true` if the set contains no styles and `false`
* otherwise.
*/
public function isEmpty()
{
return 0 === count($this->styles);
}
/**
* Returns the style with the given tag.
*
* @param string $tag The tag of the style.
*
* @return Style The style.
*
* @throws OutOfBoundsException If no style is set for the given tag.
*/
public function get($tag)
{
if (!isset($this->styles[$tag])) {
throw new OutOfBoundsException(sprintf(
'The style tag "%s" does not exist.',
$tag
));
}
return $this->styles[$tag];
}
/**
* Returns all styles.
*
* @return Style[] The styles indexed by their tags.
*/
public function toArray()
{
return $this->styles;
}
}