1. Introduction

The AdvancedBBCodeParser is a generic implementation of a bbcode formatter. In contrast to the previous implementation, this component is much more lightweight and the parser logic is highly extensible. The parser itself contains a set of four output provider, that can handle font styles (bold, italic, underlined), font sizes and colors (depending on the corresponding configuration files) and new lines.

2. Class design

The bbcode parser consists of three classes: the BBCodeParserDefinition, that represent an output provider, a provider implementation interface class (BBCodeParserProvider) and the parser itself (AdvancedBBCodeParser). Due to the fact, that each provider must be able to load a configuration file depending on the current context of the application, the AdvancedBBCodeParser must be created via the service manager.

In case of extending the list of providers or replacing a particular one, the newly implemented class must inherit from the BBCodeParserProvider class. The following code box presents a simple provider implementation:

PHP code
use APF\tools\string\BBCodeParserProvider; class NewLineProvider extends BBCodeParserProvider { public function getOutput($string){ return nl2br($string); } }

Thanks to the generic implementation, the AdvancedBBCodeParser can execute as many providers as you like.

3. Application

In case of standard application, you need to provide the necessary configuration files, that contain the font size and color definitions used by the default provider. These configuration files are expected to reside in the APF\tools\string\bbcpprovider namespace and the current context path. Thereby, the font size definition must be included in the

APF template

file and the color definition must be stored in the file

APF template

The two configuration files do consist of a fixed section and a mutable amount of size or color directives:

APF configuration
[Sizes] 1 = "10px" 2 = "12px" 3 = "16px" 4 = "18px" 5 = "24px" 6 = "32px"
APF template
[Colors] green = "#56a437" blue = "#002488" light-blue = "rgb(102 153 255)" orange = "rgb(233 142 31)" gray = "#666666" red = "red"

After providing these configuration files, the parser can be used like described in the following code box:

PHP code
$bP = &$this->getServiceObject('APF\tools\string\AdvancedBBCodeParser'); echo $bP->parseCode($string);

4. Enhanced configuration

In order to add further provider or replace existing ones, the addProvider() and removeProvider() functions can be used. Please use the following alias names to address the existing providers:

  • Formats the font variants (bold, italic, underlined)
  • standard.font.size: Formats the font size declarations
  • standard.font.color: Formats the font color tags
  • standard.newline: Replaces newlines by HTML line breaks

When adding new output provider, the aliases can be chosen as desired. The following code sample explains, how to replace existing provider and how to add new output formatter:

PHP code
use APF\tools\string\AdvancedBBCodeParser; use APF\tools\string\BBCodeParserDefinition; $bP = &$this->getServiceObject('APF\tools\string\AdvancedBBCodeParser'); // replace an existing provider $fontProv = new BBCodeParserDefinition('VENDOR\pres\bbcp\MyFontParser'); $bP->addProvider('', $fontProv); // add a new provider $specProv = new BBCodeParserDefinition('VENDOR\pres\bbcp\MySpecialParser'); $bP->addProvider('special_provider', $specProv); // remove an existing provider $bP->removeProvider('standard.newline'); echo $bP->parseCode($string);


Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
There are no comments belonging to this article.