Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
"authors": [
{
"name": "Moamen Eltouny",
"email": "raggi@pharaonic.io"
"email": "raggigroup@gmail.com"
}
],
"require": {
"php": "^8.0",
"ext-mbstring": "*"
"ext-mbstring": "*",
"voku/portable-ascii": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
Expand All @@ -29,7 +30,7 @@
"Pharaonic\\Slugify\\": "src"
},
"files": [
"src/Helpers.php"
"src/helpers.php"
]
},
"autoload-dev": {
Expand Down
49 changes: 49 additions & 0 deletions src/Facades/Facade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Pharaonic\Slugify\Facades;

abstract class Facade
{
protected static $resolvedInstance;

/**
* Get the facade accessor for the underlying instance.
*
* @return string
* @throws \RuntimeException
*/
protected static function getFacadeAccessor()
{
throw new \RuntimeException('Facade does not implement getFacadeAccessor.');
}

/**
* Resolve the instance for the facade.
*
* @return object
*/
protected static function resolveInstance()
{
$class = static::getFacadeAccessor();

if (!static::$resolvedInstance) {
static::$resolvedInstance = new $class();
}

return static::$resolvedInstance;
}

/**
* Get the facade accessor for the underlying instance.
*
* @param string $method
* @param mixed $args
* @return mixed
*/
public static function __callStatic($method, $args)
{
$instance = static::resolveInstance();

return $instance->$method(...$args);
}
}
17 changes: 17 additions & 0 deletions src/Facades/Slugify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Pharaonic\Slugify\Facades;

use Pharaonic\Slugify\Services\SlugifyService;

/**
* @method static string get(string $value, string $separator = '-', bool $ascii_only = false, string $ascii_lang = 'en')
* @method static void rule(string $key, string $value)
*/
class Slugify extends Facade
{
protected static function getFacadeAccessor()
{
return SlugifyService::class;
}
}
14 changes: 0 additions & 14 deletions src/Helpers.php

This file was deleted.

107 changes: 107 additions & 0 deletions src/Services/SlugifyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Pharaonic\Slugify\Services;

use voku\helper\ASCII;

class SlugifyService
{
/**
* The list of main slugify rules.
*
* @var array
*/
protected $dictionary = [];

/**
* The list of extra slugify rules.
*
* @var array
*/
protected $extra = [];

public function __construct()
{
$this->dictionary = include __DIR__ . '/../resources/rules.php';
}

/**
* Add or update a slugify rule.
*
* @param string $key
* @param string $value
* @return void
*/
public function rule(string $key, string $value)
{
$this->extra[mb_strtolower($key, 'UTF-8')] = $value;
$this->dictionary[mb_strtolower($key, 'UTF-8')] = $value;
}

/**
* Generate a slug from the given value.
*
* @param mixed $value
* @param string $separator
* @param boolean $ascii_only
* @param string $ascii_lang
* @return string
*/
public function get($value, string $separator = '-', bool $ascii_only = false, string $ascii_lang = 'en')
{
if (empty($value)) {
return '';
}

$value = mb_strtolower($this->handleAbbreviations($value), "UTF-8");

if ($ascii_only) {
$value = ASCII::to_ascii($value, $ascii_lang);
$value = str_replace(array_keys($this->extra), $this->extra, $value);
} else {
$value = str_replace(array_keys($this->dictionary), $this->dictionary, $value);
}
return $this->prepareValue($value, $separator);
}

/**
* Handle abbreviations in the given string.
*
* @param string $value
* @return string
*/
private function handleAbbreviations(string $value)
{
preg_match_all('/(?<!\s)?[A-Z]+/m', substr($value, 1), $matches, PREG_OFFSET_CAPTURE);

if (!empty($matches[0])) {
foreach (array_reverse($matches[0]) as $match) {
$value = substr_replace($value, ' ', $match[1] + 1, 0);
}
}

return $value;
}

/**
* Prepare the given value.
*
* @param string $value
* @param string $separator
* @return string
*/
private function prepareValue(string $value, string $separator)
{
// Convert all dashes/underscores into separator
$flip = $separator === '-' ? '_' : '-';
$value = preg_replace('!['.preg_quote($flip).']+!u', $separator, $value);

// Remove all characters that are not the separator, letters, numbers, or whitespace
$value = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', ' ', $value);

// Replace all separator characters and whitespace by a single separator
$value = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $value);

return trim($value, $separator);
}
}
Loading