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 a rule to slug #47
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9e6905c
Adds a rule to slug a field with its own value or from another field's
jhuet c5821af
Completing annotations / comments
jhuet 51b1405
Using allowNotSet = true
jhuet 043e299
Make the slug non existant if null with setEmpty
106bccc
Fix PR comments
8cc1c4f
Fix gap in the filter-rules.md
6b06704
Merge pull request #1 from RickvdStaaij/feature/filter-rule-slug
jhuet 61161e3
Removing now useless documentation
jhuet d8afa70
Precising Slug behavior if no value is given
jhuet d6a0884
Asserting identity on test result
jhuet 4199725
Test readability is key
jhuet File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,67 @@ | ||
<?php | ||
/** | ||
* Particle. | ||
* | ||
* @link http://github.com/particle-php for the canonical source repository | ||
* @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com) | ||
* @license https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License | ||
*/ | ||
namespace Particle\Filter\FilterRule; | ||
|
||
use Particle\Filter\FilterRule; | ||
|
||
/** | ||
* Class Slug | ||
* | ||
* @package Particle\Filter\FilterRule | ||
*/ | ||
class Slug extends FilterRule | ||
{ | ||
/** | ||
* Allows a default value to be set if no data key was provided | ||
* | ||
* @var bool | ||
*/ | ||
protected $allowNotSet = true; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $fieldToSlugFrom; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $transliterator = 'Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();'; | ||
|
||
/** | ||
* @param string $fieldToSlugFrom | ||
*/ | ||
public function __construct($fieldToSlugFrom) | ||
{ | ||
$this->fieldToSlugFrom = $fieldToSlugFrom; | ||
} | ||
|
||
/** | ||
* Slug the value of either the actual field of the given one. | ||
* | ||
* @param mixed $value | ||
* @return string | ||
*/ | ||
public function filter($value) | ||
{ | ||
if (empty($value) && isset($this->filterData[$this->fieldToSlugFrom])) { | ||
$value = $this->filterData[$this->fieldToSlugFrom]; | ||
} | ||
|
||
if (is_null($value)) { | ||
return $this->setEmpty(); | ||
} | ||
|
||
$value = transliterator_transliterate($this->transliterator, $value); | ||
$value = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $value); | ||
$value = preg_replace('/[-$?\s]+/', '-', $value); | ||
$value = trim($value, '-'); | ||
return strtolower($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,69 @@ | ||
<?php | ||
namespace Particle\Tests\Filter\FilterRule; | ||
|
||
use Particle\Filter\Filter; | ||
|
||
class SlugTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Filter | ||
*/ | ||
protected $filter; | ||
|
||
/** | ||
* Prepare the filter | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->filter = new Filter(); | ||
} | ||
|
||
/** | ||
* @dataProvider getSlugResults | ||
* @param string $value | ||
* @param string $filteredValue | ||
* @param string $field | ||
* @param string $fieldValue | ||
*/ | ||
public function testSlugFilterRule($value, $filteredValue, $field, $fieldValue) | ||
{ | ||
$this->filter->value('test')->slug($field); | ||
|
||
$result = $this->filter->filter([ | ||
'test' => $value, | ||
$field => $fieldValue, | ||
]); | ||
|
||
$this->assertSame($filteredValue, $result['test']); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getSlugResults() | ||
{ | ||
return [ | ||
['', '', '', ''], | ||
['This is a great stuff to slug !', 'this-is-a-great-stuff-to-slug', '', ''], | ||
['That too with somê spéciàl châractèr$ from €ope !', 'that-too-with-some-special-character-from-europe', '', ''], | ||
['A æ Übérmensch på høyeste nivå! И я люблю PHP ! fi', 'a-ae-ubermensch-pa-hoyeste-niva-i-a-lublu-php-fi', '', ''], | ||
['', 'this-is-a-great-stuff-to-slug', 'test', 'This is a great stuff to slug !'], | ||
['', 'that-too-with-some-special-character-from-europe', 'test', 'That too with somê spéciàl châractèr$ from €ope !'], | ||
['', 'a-ae-ubermensch-pa-hoyeste-niva-i-a-lublu-php-fi', 'test', 'A æ Übérmensch på høyeste nivå! И я люблю PHP ! fi'], | ||
]; | ||
} | ||
|
||
/** | ||
* Test that a slug based of a non existing key, does not exist. | ||
*/ | ||
public function testSlugFilterEmptyIfNotAvailable() | ||
{ | ||
$this->filter->value('slug')->slug('title'); | ||
|
||
$result = $this->filter->filter([ | ||
'not-title' => 'Definitely not a title to slug', | ||
]); | ||
|
||
$this->assertSame(['not-title' => 'Definitely not a title to slug'], $result); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jhuet
How about introducing an explaining variable here to improve clarity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, you got it, i'm in a good mood today :)