Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
skeeks-semenov committed Nov 8, 2017
1 parent 6d18382 commit 0f49e01
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 44 deletions.
55 changes: 17 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
Yii2 slug behavior (Semantic URL)
Yii2 yandex slug (Semantic URL)
===================================

This solution allows you to generate good slug urls. ([slug wiki](https://en.wikipedia.org/wiki/Semantic_URL)).

Direct generation is engaged in a proven solution [cocur/slugify](https://github.com/cocur/slugify).

[![Latest Stable Version](https://poser.pugx.org/skeeks/yii2-slug-behavior/v/stable.png)](https://packagist.org/packages/skeeks/yii2-slug-behavior)
[![Total Downloads](https://poser.pugx.org/skeeks/yii2-slug-behavior/downloads.png)](https://packagist.org/packages/skeeks/yii2-slug-behavior)
[![GPL-3.0 License](https://img.shields.io/packagist/l/skeeks/yii2-slug-behavior.svg)](https://opensource.org/licenses/GPL-3.0)
[![Reference Status](https://www.versioneye.com/php/skeeks:yii2-slug-behavior/reference_badge.svg)](https://www.versioneye.com/php/skeeks:yii2-slug-behavior/references)
[![Dependency Status](https://www.versioneye.com/php/skeeks:yii2-slug-behavior/dev-master/badge.png)](https://www.versioneye.com/php/skeeks:yii2-slug-behavior/dev-master)
Transliteration yandex http://translit-online.ru/yandex.html

[![Latest Stable Version](https://poser.pugx.org/skeeks/yii2-ya-slug/v/stable.png)](https://packagist.org/packages/skeeks/yii2-ya-slug)
[![Total Downloads](https://poser.pugx.org/skeeks/yii2-ya-slug/downloads.png)](https://packagist.org/packages/skeeks/yii2-ya-slug)
[![GPL-3.0 License](https://img.shields.io/packagist/l/skeeks/yii2-ya-slug.svg)](https://opensource.org/licenses/GPL-3.0)
[![Reference Status](https://www.versioneye.com/php/skeeks:yii2-ya-slug/reference_badge.svg)](https://www.versioneye.com/php/skeeks:yii2-ya-slug/references)
[![Dependency Status](https://www.versioneye.com/php/skeeks:yii2-ya-slug/dev-master/badge.png)](https://www.versioneye.com/php/skeeks:yii2-ya-slug/dev-master)

Installation
------------
Expand All @@ -19,78 +21,55 @@ The preferred way to install this extension is through [composer](http://getcomp
Either run

```
php composer.phar require --prefer-dist skeeks/yii2-slug-behavior "*"
php composer.phar require --prefer-dist skeeks/yii2-ya-slug "*"
```

or add

```
"skeeks/yii2-slug-behavior": "*"
"skeeks/yii2-ya-slug": "*"
```


How to use
----------

### behavior

Attach the behavior in your model:

```php
public function behaviors()
{
return [
'slug' => [
'class' => 'skeeks\yii2\slug\SlugBehavior',
'class' => 'skeeks\yii2\yaslug\YaSlugBehavior',
'slugAttribute' => 'slug', //The attribute to be generated
'attribute' => 'name', //The attribute from which will be generated
// optional params
'maxLength' => 64, //Maximum length of attribute slug
'minLength' => 3, //Min length of attribute slug
'ensureUnique' => true,
'slugifyOptions' => [
'lowercase' => true,
'separator' => '-',
'trim' => true
//'regexp' => '/([^A-Za-z0-9]|-)+/',
//'rulesets' => ['russian'],
//@see all options https://github.com/cocur/slugify
]
]
];
}

```



Yandex translit http://translit-online.ru/yandex.html:
### helper

```php
public function behaviors()
{
return [
'slug' => [
'class' => 'skeeks\yii2\slug\SlugBehavior',
'slugAttribute' => 'slug', //The attribute to be generated
'attribute' => 'name', //The attribute from which will be generated
// optional params
'slugifyOptions' => [
'rulesets' => [
skeeks\yii2\slug\SlugRuleProvider::YANDEX,
'default'
]
]
]
];
}

<?php
echo skeeks\yii2\yaslug\YaSlugBehavior::slugify("Тестовая строка");
```

Links
----------
* [Github](https://github.com/skeeks-semenov/yii2-slug-behavior)
* [Changelog](https://github.com/skeeks-semenov/yii2-slug-behavior/blob/master/CHANGELOG.md)
* [Issues](https://github.com/skeeks-semenov/yii2-slug-behavior/issues)
* [Packagist](https://packagist.org/packages/skeeks/yii2-slug-behavior)
* [Packagist](https://packagist.org/packages/skeeks/yii2-ya-slug)


Demo (view urls)
Expand Down
10 changes: 4 additions & 6 deletions src/YaSlugBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ public function getValue($event)
{
if (!$this->value)
{
$slugify = new Slugify((array) $this->slugifyOptions, $this->slugifyRuleProvider);

if ($this->owner->{$this->slugAttribute})
{
$slug = $slugify->slugify($this->owner->{$this->slugAttribute});
$slug = YaSlugHelper::slugify($this->owner->{$this->slugAttribute});
} else
{
$slug = $slugify->slugify($this->owner->{$this->attribute});
$slug = YaSlugHelper::slugify($this->owner->{$this->attribute});
}

if (strlen($slug) < $this->minLength) {
Expand All @@ -113,7 +111,7 @@ public function getValue($event)
if ($last = $this->owner->find()->orderBy('id DESC')->one())
{
$slug = $slug . '-' . $last->id;
return $slugify->slugify($slug);
return YaSlugHelper::slugify($slug);
}
}
} else
Expand All @@ -125,7 +123,7 @@ public function getValue($event)
if ($last = $this->owner->find()->orderBy('id DESC')->one())
{
$slug = $slug . '-' . $last->id;
return $slugify->slugify($slug);
return YaSlugHelper::slugify($slug);
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/YaSlugHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
*/
class YaSlugHelper
{
/**
* @see http://translit-online.ru/yandex.html
*
* @param $string
* @return string
*/
static public function slugify($string)
{
$slugify = new Slugify([
Expand Down Expand Up @@ -51,6 +57,40 @@ static public function slugify($string)
//1 step
$string = $slugify->slugify($string);

$data = array_map(function ($i) use ($string) {
return mb_substr($string, $i, 1);
}, range(0, mb_strlen($string) -1));

$newData = [];

if ($data)
{
$lastWord = "";
foreach ($data as $word)
{
/**
* @see http://translit-online.ru/yandex.html
*/
if ($lastWord && in_array($lastWord, ['c', 's', 'e', 'h']) && $word == "х") {
$word = "kh";
} else if ($word == "х") {
$word = "h";
}

$newData[] = $word;
if (mb_strlen($word) > 1) {
$lastWord = mb_substr($word, mb_strlen($word) - 1, mb_strlen($word));
} else {
$lastWord = $word;
}

}
}

if ($newData) {
return implode($newData);
}

return $string;
}
}

0 comments on commit 0f49e01

Please sign in to comment.