Slugify.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * This file is part of cocur/slugify.
  4. *
  5. * (c) Florian Eckerstorfer <florian@eckerstorfer.co>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Cocur\Slugify;
  11. use Cocur\Slugify\RuleProvider\DefaultRuleProvider;
  12. use Cocur\Slugify\RuleProvider\RuleProviderInterface;
  13. /**
  14. * Slugify
  15. *
  16. * @package Cocur\Slugify
  17. * @author Florian Eckerstorfer <florian@eckerstorfer.co>
  18. * @author Ivo Bathke <ivo.bathke@gmail.com>
  19. * @author Marchenko Alexandr
  20. * @copyright 2012-2015 Florian Eckerstorfer
  21. * @license http://www.opensource.org/licenses/MIT The MIT License
  22. */
  23. class Slugify implements SlugifyInterface
  24. {
  25. const LOWERCASE_NUMBERS_DASHES = '/([^A-Za-z0-9]|-)+/';
  26. /**
  27. * @var array<string,string>
  28. */
  29. protected $rules = [];
  30. /**
  31. * @var RuleProviderInterface
  32. */
  33. protected $provider;
  34. /**
  35. * @var array<string,mixed>
  36. */
  37. protected $options = [
  38. 'regexp' => self::LOWERCASE_NUMBERS_DASHES,
  39. 'lowercase' => true,
  40. 'rulesets' => [
  41. 'default',
  42. // Languages are preferred if they appear later, list is ordered by number of
  43. // websites in that language
  44. // https://en.wikipedia.org/wiki/Languages_used_on_the_Internet#Content_languages_for_websites
  45. 'azerbaijani',
  46. 'burmese',
  47. 'hindi',
  48. 'georgian',
  49. 'norwegian',
  50. 'vietnamese',
  51. 'ukrainian',
  52. 'latvian',
  53. 'finnish',
  54. 'greek',
  55. 'czech',
  56. 'arabic',
  57. 'turkish',
  58. 'polish',
  59. 'german',
  60. 'russian',
  61. ],
  62. ];
  63. /**
  64. * @param array $options
  65. * @param RuleProviderInterface $provider
  66. */
  67. public function __construct(array $options = [], RuleProviderInterface $provider = null)
  68. {
  69. $this->options = array_merge($this->options, $options);
  70. $this->provider = $provider ? $provider : new DefaultRuleProvider();
  71. foreach ($this->options['rulesets'] as $ruleSet) {
  72. $this->activateRuleSet($ruleSet);
  73. }
  74. }
  75. /**
  76. * Returns the slug-version of the string.
  77. *
  78. * @param string $string String to slugify
  79. * @param string $separator Separator
  80. *
  81. * @return string Slugified version of the string
  82. */
  83. public function slugify($string, $separator = '-')
  84. {
  85. $string = strtr($string, $this->rules);
  86. if ($this->options['lowercase']) {
  87. $string = mb_strtolower($string);
  88. }
  89. $string = preg_replace($this->options['regexp'], $separator, $string);
  90. return trim($string, $separator);
  91. }
  92. /**
  93. * Adds a custom rule to Slugify.
  94. *
  95. * @param string $character Character
  96. * @param string $replacement Replacement character
  97. *
  98. * @return Slugify
  99. */
  100. public function addRule($character, $replacement)
  101. {
  102. $this->rules[$character] = $replacement;
  103. return $this;
  104. }
  105. /**
  106. * Adds multiple rules to Slugify.
  107. *
  108. * @param array <string,string> $rules
  109. *
  110. * @return Slugify
  111. */
  112. public function addRules(array $rules)
  113. {
  114. foreach ($rules as $character => $replacement) {
  115. $this->addRule($character, $replacement);
  116. }
  117. return $this;
  118. }
  119. /**
  120. * @param string $ruleSet
  121. *
  122. * @return Slugify
  123. */
  124. public function activateRuleSet($ruleSet)
  125. {
  126. return $this->addRules($this->provider->getRules($ruleSet));
  127. }
  128. /**
  129. * Static method to create new instance of {@see Slugify}.
  130. *
  131. * @param array <string,mixed> $options
  132. *
  133. * @return Slugify
  134. */
  135. public static function create(array $options = [])
  136. {
  137. return new static($options);
  138. }
  139. }