Merge pull request #9064 from creative-commoners/pulls/4.5/filter-interface

NEW Add FilterInterface and retrofit into URLSegmentFilter
This commit is contained in:
Garion Herman 2020-07-27 11:18:23 +12:00 committed by GitHub
commit e2443763a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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. * See {@link FileNameFilter} for similar implementation for filesystem-based URLs.
*/ */
class URLSegmentFilter class URLSegmentFilter implements FilterInterface
{ {
use Configurable; use Configurable;
use Injectable; use Injectable;
@ -58,6 +58,17 @@ class URLSegmentFilter
*/ */
public $replacements = []; public $replacements = [];
/**
* @var Transliterator
*/
protected $transliterator;
/**
* @var boolean
*/
protected $allowMultibyte;
/** /**
* Note: Depending on the applied replacement rules, this method might result in an empty string. * 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() public function getReplacements()
{ {
return ($this->replacements) ? $this->replacements : (array)$this->config()->default_replacements; return $this->replacements ?: (array)$this->config()->get('default_replacements');
} }
/** /**
* @var Transliterator * @return Transliterator|null
*/
protected $transliterator;
/**
* @return Transliterator
*/ */
public function getTransliterator() 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(); $this->transliterator = Transliterator::create();
} }
return $this->transliterator; 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 * @param boolean
*/ */