-
Notifications
You must be signed in to change notification settings - Fork 301
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
Extract slug generator to separate method #94
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Knp\DoctrineBehaviors\Model\Sluggable; | ||
|
||
/** | ||
* This class contains function which transliterates string in russian using traditional russian way of transliterating instead of ISO transliteration rules. | ||
* | ||
* @package Knplabs\DoctrineBehaviors\Model\Sluggable | ||
* @author Alex Panshin <deadyaga@gmail.com> | ||
*/ | ||
class Utils | ||
{ | ||
private static $ru_alphabet = array( 'а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я'); | ||
private static $translit_alphabet = array('a','b','v','g','d','e','yo','zh', 'z', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u','f', 'h', 'ts', 'ch', 'sh', 'sch', '', 'y', 'j', 'e', 'yu', 'ya'); | ||
|
||
/** | ||
* This method prepares string to be an url's part | ||
* @param string $string | ||
* @return string | ||
*/ | ||
public static function transliterateRussian($string) | ||
{ | ||
$string = function_exists('mb_strtolower') ? mb_strtolower($string, 'UTF-8') : strtolower($string); | ||
|
||
$string = str_replace(self::$ru_alphabet, self::$translit_alphabet, $string); | ||
|
||
$string = transliterator_transliterate("Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;", $string); | ||
$string = preg_replace('/[-\s]+/', '-', $string); | ||
|
||
return trim($string, '-'); | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
tests/fixtures/BehaviorFixtures/ORM/RussianSluggableEntity.php
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,18 @@ | ||
<?php | ||
|
||
namespace BehaviorFixtures\ORM; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Knp\DoctrineBehaviors\Model; | ||
|
||
/** | ||
* @ORM\Entity | ||
* @ORM\Table(name="SluggableEntity") | ||
*/ | ||
class RussianSluggableEntity extends SluggableEntity | ||
{ | ||
protected function getTransliterator() | ||
{ | ||
return ['\Knp\DoctrineBehaviors\Model\Sluggable\Utils', 'transliterateRussian']; | ||
} | ||
} |
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
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.
This look kinda weird. Inception function in a method.
If you need to pass parameter, try
call_user_func_array
Also it would be nice to keep inner compatibility of this package. Checkout this localeCallable in Translatable.
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.
I agree that you don't need to return a closure to call it directly after.
Just name the method "transliterate" (or someting) and return this result.
Anyone will be able to replace it in its class anyway. See what I mean?
Otherwise, (and it was the case before already with iconv), but we need to put ext-intl (or ext-iconv) in the list of requires (or suggests) in composer.json IMHO.
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.
Ok, I see :)