Erebot  latest
A modular IRC bot for PHP 5.3+
Erebot\Styling Class Reference

Provides styling (formatting) features. More...

+ Inheritance diagram for Erebot\Styling:

Public Member Functions

 _ ($template, array $vars=array())
 
 __construct (\Erebot\IntlInterface $translator)
 
 getClass ($type)
 
 getTranslator ()
 
 render ($template, array $vars=array())
 
 setClass ($type, $cls)
 

Protected Member Functions

 parseNode ($node, &$attributes, $vars)
 
 wrapScalar ($var, $name)
 

Static Protected Member Functions

static checkVariableName ($var)
 
static parseTemplate ($source)
 

Protected Attributes

 $cls
 Maps some scalar types to a typed variable.
 
 $translator
 Translator to use to improve rendering.
 

Private Member Functions

 parseChildren ($node, &$attributes, $vars)
 

Additional Inherited Members

- Public Attributes inherited from Erebot\StylingInterface
const CODE_BOLD = "\002"
 Mark the following text as being bold.
 
const CODE_COLOR = "\003"
 Change the color of the following text.
 
const CODE_RESET = "\017"
 Reset the styles.
 
const CODE_REVERSE = "\026"
 Swao the foreground & background colors.
 
const CODE_UNDERLINE = "\037"
 Underline the text that follows.
 
const COLOR_AQUA_LIGHT = 11
 Alias for Erebot::StylingInterface::COLOR_LIGHT_CYAN.
 
const COLOR_BLACK = 1
 Black.
 
const COLOR_BLUE = 2
 Blue.
 
const COLOR_BROWN = 5
 Brown.
 
const COLOR_CYAN = 10
 Cyan.
 
const COLOR_DARK_BLUE = 2
 Alias for Erebot::StylingInterface::COLOR_DARK_BLUE.
 
const COLOR_DARK_CYAN = 10
 Alias for Erebot::StylingInterface::COLOR_CYAN.
 
const COLOR_DARK_GRAY = 14
 Alias for Erebot::StylingInterface::COLOR_GRAY.
 
const COLOR_DARK_GREEN = 3
 Alias for Erebot::StylingInterface::COLOR_DARK_GREEN.
 
const COLOR_DARK_GREY = 14
 Alias for Erebot::StylingInterface::COLOR_GRAY.
 
const COLOR_GRAY = 14
 Gray.
 
const COLOR_GREEN = 3
 Green.
 
const COLOR_GREY = 14
 Alias for Erebot::StylingInterface::COLOR_GRAY.
 
const COLOR_HOT_PINK = 13
 Alias for Erebot::StylingInterface::COLOR_HOT_PINK.
 
const COLOR_LIGHT_BLUE = 12
 Light blue.
 
const COLOR_LIGHT_CYAN = 11
 Light cyan.
 
const COLOR_LIGHT_GRAY = 15
 Light gray.
 
const COLOR_LIGHT_GREEN = 9
 Light green.
 
const COLOR_LIGHT_GREY = 15
 Alias for Erebot::StylingInterface::COLOR_LIGHT_GRAY.
 
const COLOR_LIME_GREEN = 9
 Alias for Erebot::StylingInterface::COLOR_LIGHT_GREEN.
 
const COLOR_NAVY_BLUE = 2
 Alias for Erebot::StylingInterface::COLOR_NAVY_BLUE.
 
const COLOR_OLIVE = 7
 Alias for Erebot::StylingInterface::COLOR_ORANGE.
 
const COLOR_ORANGE = 7
 Orange.
 
const COLOR_PINK = 13
 Pink.
 
const COLOR_PURPLE = 6
 Purple.
 
const COLOR_RED = 4
 Red.
 
const COLOR_ROYAL_BLUE = 12
 Alias for Erebot::StylingInterface::COLOR_LIGHT_BLUE.
 
const COLOR_TEAL = 10
 Alias for Erebot::StylingInterface::COLOR_CYAN.
 
const COLOR_WHITE = 0
 White.
 
const COLOR_YELLOW = 8
 Yellow.
 

Detailed Description

Provides styling (formatting) features.

Given a format string (a template), this class can perform styling on that template to produce complex messages.

A template is composed of a single string, which may contain special markup to insert dynamic content, add formatting attributes to the text (like bold, underline, colors), etc.

Special markup in templates
Markup

Role

<b>...</b>

The text is rendered in bold

<u>...</u>

The text is rendered underlined

<var name="..."/>

This markup gets replaced by the content of the given variable

<color
  fg="..."
  bg="...">
    ...
</color>

The text is rendered with the given foreground (fg) and background (bg) colors. The value of the fg and bg attributes may be either an integer (see the COLOR_* constants in this class) or the name of the color (again, supported colors are named after the COLOR_* constants).

<for
  from="..."
  item="..."
  key="..."
  sep=", "
  last=" &amp; ">
    ...
</for>

This markup loops over the associative array in from. The key for each entry in that array is stored in the temporary variable named by the key attribute if given, while the associated value is stored in the temporary variable named by item. The value of sep (alias separator) is appended automatically between each entry of the array, except between the last two entries. The value of last (alias last_separator) is used to separate the last two entries. By default, no temporary variable is created for the key, ", " is used as the main separator and " & " is used as the last_separator.

<plural var="...">
  <case form="...">
    ...
  </case>
</plural>
Handles plurals. Depending on the value of the variable pointed by var, one of the cases will be used. The page at http://unicode.org/cldr/data/charts/supplemental/language_plural_rules.html references every available form per language.

Definition at line 113 of file Styling.php.

Constructor & Destructor Documentation

Erebot\Styling::__construct ( \Erebot\IntlInterface  $translator)

Construct a new styling object.

Parameters
Erebot::IntlInterface$translatorA translator object, used to determine the correct pluralization rules.

Implements Erebot\StylingInterface.

Definition at line 128 of file Styling.php.

Member Function Documentation

Erebot\Styling::_ (   $template,
array  $vars = array() 
)

Alias for Erebot::StylingInterface::render(), compatible with internationalization tools such as xgettext.

Parameters
string$templateA template to render.
array$vars(optional) An array of variables that can be used to change the way the template will be rendered.
Return values
stringThe formatted result for this template.
Note
The name of this method was chosen so that tools such as xgettext can mark the template as being some message to translate. If you DO NOT want this behaviour, use Erebot::StylingInterface::render() instead.

Implements Erebot\StylingInterface.

Definition at line 216 of file Styling.php.

static Erebot\Styling::checkVariableName (   $var)
staticprotected

Checks whether the given variable name is valid and throws an exception if its not.

Parameters
string$varVariable name to test.
Exceptions
::InvalidArgumentExceptionThe given variable name is invalid.
Returns
This method does not return anything.

Definition at line 204 of file Styling.php.

Erebot\Styling::getClass (   $type)

Returns the class used to wrap scalar types.

Parameters
string$typeName of a scalar type that can be wrapped by this class automatically. Must be one of "int", "string" or "float".
Return values
stringName of the class that can be used to wrap variables of the given type.

Definition at line 150 of file Styling.php.

Erebot\Styling::getTranslator ( )

Returns the translator associated with this formatter.

Return values
Erebot::IntlInterfaceTranslator associated with this formatter.

Implements Erebot\StylingInterface.

Definition at line 265 of file Styling.php.

Erebot\Styling::parseChildren (   $node,
$attributes,
  $vars 
)
private

This method is used to apply the parsing method to children of an XML node.

Parameters
DOMNode$nodeThe node being parsed.
array$attributesArray of styling attributes.
array$varsTemplate variables that can be injected in the result.
Return values
stringParsing result, with styles applied as appropriate.

Definition at line 652 of file Styling.php.

Erebot\Styling::parseNode (   $node,
$attributes,
  $vars 
)
protected

This is the main parsing method.

Parameters
DOMNode$nodeThe node being parsed.
array$attributesArray of styling attributes.
array$varsTemplate variables that can be injected in the return.
Return values
stringParsing result, with styles applied as appropriate.

Definition at line 401 of file Styling.php.

static Erebot\Styling::parseTemplate (   $source)
staticprotected

Parses a template into a DOM.

Parameters
string$sourceTemplate to parse.
Return values
Erebot::DOMDOM object constructed from the template.
Exceptions
::InvalidArgumentExceptionThe template was malformed or invalid.

Definition at line 353 of file Styling.php.

Erebot\Styling::render (   $template,
array  $vars = array() 
)

Renders a template using the given variables.

Parameters
string$templateA template to render.
array$vars(optional) An array of variables that can be used to change the way the template will be rendered.
Return values
stringThe formatted result for this template.
Note
Erebot::StylingInterface::_() is preferred as it makes external tools such as xgettext add the template to the list of messages to translate.

Implements Erebot\StylingInterface.

Definition at line 223 of file Styling.php.

Erebot\Styling::setClass (   $type,
  $cls 
)

Sets the class to use to wrap a certain scalar type.

Parameters
string$typeName of a scalar type that can be wrapped by this class automatically. Must be one of "int", "string" or "float".
string$clsName of the class that can be used to wrap variables of the given type.

Definition at line 170 of file Styling.php.

Erebot\Styling::wrapScalar (   $var,
  $name 
)
protected

Wraps a scalar into the appropriate styling object.

Parameters
mixed$varEither a scalar, an array or an object implementing the Erebot::Styling::VariableInterface interface. Scalar values will be wrapped with the appropriate object while arrays and objects are returned untouched.
string$nameName of given variable.
Return values
arrayIf $var referred to an array, it is returned without any modification.
::Erebot::Styling::VariableInterface_VariableObjects that implement Erebot::Styling::VariableInterface are returned without any modification. Scalar values are wrapped into the appropriate object implementing the Erebot::Styling::VariableInterface interface and the resulting object is returned.
Exceptions
::InvalidArgumentExceptionEither the given name is invalid, or the given value was not a scalar.
Note
In the context of this method, objects that can be converted to a string (ie. implement __toString()) are treated as if they were a string (and thus, are considered as scalar values).

Definition at line 304 of file Styling.php.


The documentation for this class was generated from the following file: