mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #9064 from creative-commoners/pulls/4.5/filter-interface
NEW Add FilterInterface and retrofit into URLSegmentFilter
This commit is contained in:
commit
e2443763a6
22
src/View/Parsers/FilterInterface.php
Normal file
22
src/View/Parsers/FilterInterface.php
Normal 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);
|
||||
}
|
@ -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 = [];
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user