NEW Add FilterInterface and retrofit into URLSegmentFilter

There are other filters in the core SilverStripe product, e.g. FileNameFilter in silverstripe/assets
which would benefit from having a common filter interface. This retrofits one for URLSegmentFilter
in framework. I have not added any PHP 7 syntax in method signatures to avoid breaking backwards
compatibility.
This commit is contained in:
Robbie Averill 2019-06-14 15:31:01 +12:00 committed by Dylan Wagstaff
parent e49cec3a00
commit 4d5d7a34a1
2 changed files with 48 additions and 21 deletions

View File

@ -0,0 +1,22 @@
<?php
namespace SilverStripe\View\Parsers;
/**
* A FilterInterface is given an input string and returns a filtered string. Replacements will be provided and
* performed (typically in regex format), and transliteration may be used as a separate service to replace
* characters rather than remove them.
*
* For example implementations, see {@link URLSegmentFilter}.
*/
interface FilterInterface
{
/**
* Performs a set of replacement rules against the input string, applying transliteration if a service is
* provided, and returns the filtered result.
*
* @param string $input
* @return string
*/
public function filter($input);
}

View File

@ -15,7 +15,7 @@ use SilverStripe\Core\Injector\Injectable;
*
* See {@link FileNameFilter} for similar implementation for filesystem-based URLs.
*/
class URLSegmentFilter
class URLSegmentFilter implements FilterInterface
{
use Configurable;
use Injectable;
@ -58,6 +58,17 @@ class URLSegmentFilter
*/
public $replacements = array();
/**
* @var Transliterator
*/
protected $transliterator;
/**
* @var boolean
*/
protected $allowMultibyte;
/**
* Note: Depending on the applied replacement rules, this method might result in an empty string.
*
@ -96,50 +107,44 @@ class URLSegmentFilter
}
/**
* @param array $r Map of find/replace used for preg_replace().
* @param string[] $replacements Map of find/replace used for preg_replace().
* @return $this
*/
public function setReplacements($r)
public function setReplacements($replacements)
{
$this->replacements = $r;
$this->replacements = $replacements;
return $this;
}
/**
* @return array
* @return string[]
*/
public function getReplacements()
{
return ($this->replacements) ? $this->replacements : (array)$this->config()->default_replacements;
return $this->replacements ?: (array)$this->config()->get('default_replacements');
}
/**
* @var Transliterator
*/
protected $transliterator;
/**
* @return Transliterator
* @return Transliterator|null
*/
public function getTransliterator()
{
if ($this->transliterator === null && $this->config()->default_use_transliterator) {
if ($this->transliterator === null && $this->config()->get('default_use_transliterator')) {
$this->transliterator = Transliterator::create();
}
return $this->transliterator;
}
/**
* @param Transliterator $t
* @param Transliterator $transliterator
* @return $this
*/
public function setTransliterator($t)
public function setTransliterator($transliterator)
{
$this->transliterator = $t;
$this->transliterator = $transliterator;
return $this;
}
/**
* @var boolean
*/
protected $allowMultibyte;
/**
* @param boolean
*/