HtmlPurifierWrapper
in package
HtmlPurifierWrapper
- Wraps HTMLPurifier instance and proxies all public method calls to it.
- Provides convenient config helpers: setOption, getOption, setOptions, resetConfig.
- Accepts constructor input as: array $options, HTMLPurifier_Config or an existing HTMLPurifier instance.
Usage: $wrapper = new HtmlPurifierWrapper(['Core.Encoding' => 'UTF-8', 'HTML.SafeIframe' => true]); $clean = $wrapper->purify($dirtyHtml);
Table of Contents
Properties
- $config : HTMLPurifier_Config
- $options : array<string|int, mixed>
- store last provided options (flat array of key => mixed) for introspection
- $purifier : HTMLPurifier
Methods
- __call() : mixed
- Magic call: delegate any other public method invoked on wrapper to internal HTMLPurifier instance.
- __construct() : mixed
- Constructor accepts: - HTMLPurifier instance (will be used directly) - HTMLPurifier_Config instance (will create HTMLPurifier from it) - array of options (keyed by config keys like 'Core.Encoding', 'HTML.SafeIframe', etc.)
- applyOptions() : $this
- Convenience: set multiple options and return $this for fluent usage.
- getConfig() : HTMLPurifier_Config
- Provide direct access to the HTMLPurifier_Config instance.
- getInstance() : HTMLPurifier
- Provide direct access to the underlying HTMLPurifier instance.
- getOption() : mixed|null
- Get a single config option (if present in stored options), otherwise null.
- getOptions() : array<string, mixed>
- Get all options (as last set by constructor / setConfig / setOption / mergeOptions).
- listPurifierPublicMethods() : array<int, string>
- Utility: return a list of public method names available on underlying HTMLPurifier instance.
- mergeOptions() : void
- Merge provided options into current config and rebuild purifier.
- purify() : string
- Helper: call purifier->purify() (explicit typed wrapper).
- purifyArray() : array<int|string, string>
- Helper: convenience for purifying an array of values.
- rebuildAfterManualConfigChange() : void
- Rebuild purifier after manual modifications to config obtained by getConfig().
- resetConfig() : void
- Reset config to HTMLPurifier defaults (clears stored options).
- setConfig() : void
- Replace current config with provided array or HTMLPurifier_Config and rebuild internal purifier.
- setOption() : void
- Set a single config option and rebuild purifier.
- withConfig() : void
- Run callable with the config as parameter. Useful for advanced manipulations (addElement, addAttribute, etc.)
- withMergedOptions() : HtmlPurifierWrapper
- Create a clone of this wrapper with merged options (does not mutate original).
- createConfigFromArray() : HTMLPurifier_Config
- Create HTMLPurifier_Config from a flat array of options.
- createDefaultConfig() : HTMLPurifier_Config
- Create a default HTMLPurifier_Config instance.
- rebuildPurifier() : void
- Rebuild underlying HTMLPurifier instance using current $this->config.
Properties
$config
private
HTMLPurifier_Config
$config
$options
store last provided options (flat array of key => mixed) for introspection
private
array<string|int, mixed>
$options
= []
$purifier
private
HTMLPurifier
$purifier
Methods
__call()
Magic call: delegate any other public method invoked on wrapper to internal HTMLPurifier instance.
public
__call(string $name, array<int, mixed> $arguments) : mixed
This ensures the wrapper "exposes" all public methods of HTMLPurifier without listing them manually.
Parameters
- $name : string
- $arguments : array<int, mixed>
Tags
__construct()
Constructor accepts: - HTMLPurifier instance (will be used directly) - HTMLPurifier_Config instance (will create HTMLPurifier from it) - array of options (keyed by config keys like 'Core.Encoding', 'HTML.SafeIframe', etc.)
public
__construct([array<string, mixed>|HTMLPurifier_Config|HTMLPurifier $source = [] ][, array<string, mixed> $extraOptions = [] ]) : mixed
Parameters
- $source : array<string, mixed>|HTMLPurifier_Config|HTMLPurifier = []
- $extraOptions : array<string, mixed> = []
-
additional options to merge (only when $source is array)
Tags
applyOptions()
Convenience: set multiple options and return $this for fluent usage.
public
applyOptions(array<string, mixed> $opts) : $this
Parameters
- $opts : array<string, mixed>
Return values
$thisgetConfig()
Provide direct access to the HTMLPurifier_Config instance.
public
getConfig() : HTMLPurifier_Config
Note:
- mutating returned config will not automatically rebuild purifier until you call rebuild() or setConfig again.
Return values
HTMLPurifier_ConfiggetInstance()
Provide direct access to the underlying HTMLPurifier instance.
public
getInstance() : HTMLPurifier
Return values
HTMLPurifiergetOption()
Get a single config option (if present in stored options), otherwise null.
public
getOption(string $key) : mixed|null
Note: HTMLPurifier_Config does not provide a generic get() in all versions; we rely on our stored map.
Parameters
- $key : string
Return values
mixed|nullgetOptions()
Get all options (as last set by constructor / setConfig / setOption / mergeOptions).
public
getOptions() : array<string, mixed>
Return values
array<string, mixed>listPurifierPublicMethods()
Utility: return a list of public method names available on underlying HTMLPurifier instance.
public
listPurifierPublicMethods() : array<int, string>
Return values
array<int, string>mergeOptions()
Merge provided options into current config and rebuild purifier.
public
mergeOptions(array<string, mixed> $options) : void
Parameters
- $options : array<string, mixed>
purify()
Helper: call purifier->purify() (explicit typed wrapper).
public
purify(string $html[, mixed|null $configSpec = null ]) : string
Parameters
- $html : string
- $configSpec : mixed|null = null
-
optional config id or context accepted by HTMLPurifier (passed-through)
Return values
string —cleaned html
purifyArray()
Helper: convenience for purifying an array of values.
public
purifyArray(array<int|string, string> $items[, mixed|null $configSpec = null ]) : array<int|string, string>
Parameters
- $items : array<int|string, string>
- $configSpec : mixed|null = null
Return values
array<int|string, string>rebuildAfterManualConfigChange()
Rebuild purifier after manual modifications to config obtained by getConfig().
public
rebuildAfterManualConfigChange() : void
resetConfig()
Reset config to HTMLPurifier defaults (clears stored options).
public
resetConfig() : void
setConfig()
Replace current config with provided array or HTMLPurifier_Config and rebuild internal purifier.
public
setConfig(array<string, mixed>|HTMLPurifier_Config $cfg) : void
Parameters
- $cfg : array<string, mixed>|HTMLPurifier_Config
setOption()
Set a single config option and rebuild purifier.
public
setOption(string $key, mixed $value) : void
Parameters
- $key : string
-
e.g. 'Core.Encoding'
- $value : mixed
withConfig()
Run callable with the config as parameter. Useful for advanced manipulations (addElement, addAttribute, etc.)
public
withConfig(callable $cb) : void
Example: $wrapper->withConfig(function(HTMLPurifier_Config $c) { $def = $c->getHTMLDefinition(true); $def->addElement(...); });
Parameters
- $cb : callable
-
function(HTMLPurifier_Config $config): void
withMergedOptions()
Create a clone of this wrapper with merged options (does not mutate original).
public
withMergedOptions(array<string, mixed> $mergedOptions) : HtmlPurifierWrapper
Parameters
- $mergedOptions : array<string, mixed>
Return values
HtmlPurifierWrappercreateConfigFromArray()
Create HTMLPurifier_Config from a flat array of options.
private
createConfigFromArray(array<string, mixed> $options) : HTMLPurifier_Config
Keys should be full config keys, e.g. 'Core.Encoding', 'HTML.SafeIframe'.
Parameters
- $options : array<string, mixed>
Return values
HTMLPurifier_ConfigcreateDefaultConfig()
Create a default HTMLPurifier_Config instance.
private
createDefaultConfig() : HTMLPurifier_Config
Return values
HTMLPurifier_ConfigrebuildPurifier()
Rebuild underlying HTMLPurifier instance using current $this->config.
private
rebuildPurifier() : void