-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Cocur\Slugify\Bridge\Laminas; | ||
|
||
use Cocur\Slugify\Slugify; | ||
use Laminas\Filter\AbstractFilter; | ||
|
||
/** | ||
* Class SlugifyFilter | ||
* | ||
* @package cocur/slugify | ||
* @subpackage bridge | ||
* @license http://www.opensource.org/licenses/MIT The MIT License | ||
*/ | ||
class SlugifyFilter extends AbstractFilter | ||
{ | ||
/** | ||
* @var array | ||
* @see Slugify::$options | ||
*/ | ||
protected $options = [ | ||
'regexp' => Slugify::LOWERCASE_NUMBERS_DASHES, | ||
'separator' => '-', | ||
'lowercase' => true, | ||
'lowercase_after_regexp' => false, | ||
'trim' => true, | ||
'strip_tags' => false | ||
]; | ||
|
||
/** | ||
* @param array|null $options | ||
*/ | ||
public function __construct(?array $options = null) | ||
{ | ||
if (!empty($options)) { | ||
$this->setOptions($options); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the result of filtering $value | ||
* | ||
* @param mixed $value | ||
* | ||
* @return mixed | ||
*/ | ||
public function filter($value) | ||
{ | ||
if (!empty($value)) { | ||
$slugify = new Slugify($this->options); | ||
return $slugify->slugify((string) $value); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Cocur\Slugify\Tests\Bridge\Laminas; | ||
|
||
use Cocur\Slugify\Bridge\Laminas\SlugifyFilter; | ||
|
||
use Laminas\Filter\FilterChain; | ||
use Laminas\InputFilter\Input; | ||
use Mockery\Adapter\Phpunit\MockeryTestCase; | ||
|
||
/** | ||
* Class SlugifyFilterTest | ||
* | ||
* @package cocur/slugify | ||
* @subpackage bridge | ||
* @license http://www.opensource.org/licenses/MIT The MIT License | ||
*/ | ||
class SlugifyFilterTest extends MockeryTestCase | ||
{ | ||
/** @var Input */ | ||
protected Input $input; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->input = new Input(''); | ||
} | ||
|
||
/** | ||
* @covers \Cocur\Slugify\Bridge\Laminas\SlugifyFilter::filter() | ||
*/ | ||
public function testSlugifyFilterWithoutCustomOptions() | ||
{ | ||
$chain = new FilterChain( | ||
[ | ||
'filters' => [ | ||
[ | ||
'name' => SlugifyFilter::class, | ||
], | ||
], | ||
] | ||
); | ||
$this->input->setFilterChain($chain); | ||
$this->input->setValue('foo Bar'); | ||
|
||
$this->assertSame('foo-bar', $this->input->getValue()); | ||
|
||
} | ||
|
||
/** | ||
* @covers \Cocur\Slugify\Bridge\Laminas\SlugifyFilter::filter() | ||
*/ | ||
public function testSlugifyFilterWithCustomOptions() | ||
{ | ||
$chain = new FilterChain( | ||
[ | ||
'filters' => [ | ||
[ | ||
'name' => SlugifyFilter::class, | ||
'options' => [ | ||
'regexp' => '/([^0-9test\/]|-)+/', | ||
'strip_tags' => false, | ||
] | ||
], | ||
], | ||
] | ||
); | ||
$this->input->setFilterChain($chain); | ||
$this->input->setValue('0123 foo bar <test>'); | ||
|
||
$this->assertSame('0123-test', $this->input->getValue()); | ||
} | ||
|
||
} |