Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds possibility to configure Purifier-Filter dynamically #2

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/Soflomo/Purifier/Filter/Purifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,55 @@ class Purifier implements FilterInterface
{
protected $purifier;

/**
* @var array
*/
protected $purifierConfig;

/**
* @var bool
*/
protected $persistentConf;



public function __construct(HTMLPurifier $purifier)
{
$this->purifier = $purifier;
}

/**
* Sets config after initialization, that is used and passed to the
* purify-method on filter() call
* @param array $config
* @param bool $persistentConf
*/
public function setConfig($config, $persistentConf = false)
{
$this->purifierConfig = $config;
$this->persistentConf = (bool) $persistentConf;
}



/**
* Retrieves the configuration set after initialization, resets config if
* $this->persistentConf is set to FALSE (means in this case method returns
* NULL on second call).
*
* @return array|null
*/
public function getConfig()
{
$config = $this->purifierConfig;
if (!$this->persistentConf) {
$this->purifierConfig = null;
}

return $config;
}


protected function getPurifier()
{
return $this->purifier;
Expand All @@ -24,6 +68,9 @@ protected function getPurifier()
*/
public function filter($value)
{
if ($this->purifierConfig) {
return $this->getPurifier()->purify($value, $this->getConfig());
}
return $this->getPurifier()->purify($value);
}
}
}