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 b770088 commit 6d18382
Show file tree
Hide file tree
Showing 5 changed files with 341 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CHANGELOG
==============

1.0.0
---------------
* Ready
105 changes: 105 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
Yii2 slug behavior (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)

Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

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

or add

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


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

Attach the behavior in your model:

```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
'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:

```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'
]
]
]
];
}

```

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)


Demo (view urls)
----------
* [https://skeeks.com](https://skeeks.com)

___

> [![skeeks!](https://skeeks.com/img/logo/logo-no-title-80px.png)](https://skeeks.com)
<i>SkeekS CMS (Yii2) — fast, simple, effective!</i>
[skeeks.com](https://skeeks.com) | [cms.skeeks.com](https://cms.skeeks.com)

34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "skeeks/yii2-ya-slug",
"description": "Yii2 yandex slug",
"keywords": ["yii", "slug", "behavior", "yii2"],
"homepage": "https://skeeks.com/",
"type": "yii2-extension",
"license": "GPL-3.0+",
"support": {
"issues": "https://github.com/skeeks-semenov/yii2-ya-slug/issues",
"source": "https://github.com/skeeks-semenov/yii2-ya-slug",
"wiki": "https://cms.skeeks.com/",
"authors": "https://skeeks.com"
},
"authors": [
{
"name": "SkeekS",
"email": "[email protected]",
"homepage": "https://skeeks.com"
},
{
"name": "Semenov Alexander",
"email": "[email protected]"
}
],
"require": {
"yiisoft/yii2": "^2",
"cocur/slugify": "^3.0"
},
"autoload": {
"psr-4": {
"skeeks\\yii2\\yaslug\\": "src/"
}
}
}
140 changes: 140 additions & 0 deletions src/YaSlugBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* @author Semenov Alexander <[email protected]>
* @link https://skeeks.com/
* @copyright (c) 2010 SkeekS
* @date 04.11.2017
*/
namespace skeeks\yii2\yaslug;
use Cocur\Slugify\RuleProvider\RuleProviderInterface;
use Cocur\Slugify\Slugify;

use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord;
use yii\behaviors\AttributeBehavior;
use yii\base\Event;

/**
* Class SlugBehavior
* @package skeeks\yii2\slug
*/
class YaSlugBehavior extends AttributeBehavior
{
/**
* The attribute to be generated
* @var string
*/
public $slugAttribute = '';

/**
* The attribute from which will be generated
* @var string
*/
public $attribute = '';

/**
* Slug attribute must be unique
* @var bool
*/
public $ensureUnique = true;

/**
* Maximum length of attribute slug
* @var int
*/
public $maxLength = 64;

/**
* Min length of attribute slug
* @var int
*/
public $minLength = 3;

/**
* @var
*/
public $value;

/**
* @inheritdoc
*/
public function init()
{
parent::init();

if (!$this->slugAttribute || !$this->attribute) {
throw new InvalidConfigException('Incorrectly configured behavior.');
}

if (empty($this->attributes))
{
$this->attributes =
[
BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->slugAttribute],
BaseActiveRecord::EVENT_BEFORE_UPDATE => [$this->slugAttribute],
];
}
}

/**
* @param Event $event
* @return mixed|string
*/
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});
} else
{
$slug = $slugify->slugify($this->owner->{$this->attribute});
}

if (strlen($slug) < $this->minLength) {
$slug = $slug . "-" . md5(microtime());
}

if (strlen($slug) > $this->maxLength) {
$slug = substr($slug, 0, $this->maxLength);
}

if ($this->ensureUnique)
{
if (!$this->owner->isNewRecord)
{
if ($founded = $this->owner->find()->where([
$this->slugAttribute => $slug
])->andWhere(["!=", "id", $this->owner->id])->one())
{
if ($last = $this->owner->find()->orderBy('id DESC')->one())
{
$slug = $slug . '-' . $last->id;
return $slugify->slugify($slug);
}
}
} else
{
if ($founded = $this->owner->find()->where([
$this->slugAttribute => $slug
])->one())
{
if ($last = $this->owner->find()->orderBy('id DESC')->one())
{
$slug = $slug . '-' . $last->id;
return $slugify->slugify($slug);
}
}
}
}

return $slug;
} else
{
return call_user_func($this->value, $event);
}
}
}
56 changes: 56 additions & 0 deletions src/YaSlugHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* @author Semenov Alexander <[email protected]>
* @link https://skeeks.com/
* @copyright (c) 2010 SkeekS
* @date 04.11.2017
*/
namespace skeeks\yii2\yaslug;
use Cocur\Slugify\RuleProvider\RuleProviderInterface;
use Cocur\Slugify\Slugify;

use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord;
use yii\behaviors\AttributeBehavior;
use yii\base\Event;

/**
* Class YaSlugHelper
* @package skeeks\yii2\yaslug
*/
class YaSlugHelper
{
static public function slugify($string)
{
$slugify = new Slugify([
'rulesets' => ['default', 'russian'],
'regexp' => '/([^A-Za-z0-9хХ]|-)+/',
'lowercase' => true,
'separator' => "-",
'trim' => true,
]);

$slugify->addRule('ё', 'yo');
$slugify->addRule('Ё', 'Yo');

$slugify->addRule('й', 'j');
$slugify->addRule('Й', 'J');

$slugify->addRule('х', 'Х');
$slugify->addRule('Х', 'Х');

$slugify->addRule('ц', 'c');
$slugify->addRule('Ц', 'C');

$slugify->addRule('щ', 'shch');
$slugify->addRule('Щ', 'Shch');

$slugify->addRule('э', 'eh');
$slugify->addRule('Э', 'Eh');

//1 step
$string = $slugify->slugify($string);

return $string;
}
}

0 comments on commit 6d18382

Please sign in to comment.