Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
altynbek committed Jun 7, 2018
0 parents commit 23d831e
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Yii2 Bridge Slug Behavior

[Yii2](http://www.yiiframework.com) [bridge](https://github.com/naffiq/yii2-bridge) slug behavior

This code is inspired by [https://github.com/zelenin/yii2-slug-behavior](https://github.com/zelenin/yii2-slug-behavior), with correction of errors related to the Kazakh language.
Full documentation about this behavior, you can read on [the above repository](https://github.com/zelenin/yii2-slug-behavior).
## Installation

### Composer

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

Either run ```composer require yii2-bridge/slug-behavior:^0.1```

or add ```"yii2-bridge/slug-behavior": "^0.1"``` to the require section of your ```composer.json```

### Using

Attach the behavior in your model:

```php
public function behaviors()
{
return [
'slug' => [
'class' => 'naffiq\bridge\behaviors\BridgeSlugBehavior',
'slugAttribute' => 'slug',
'attribute' => 'title',
// If intl extension is enabled, see http://userguide.icu-project.org/transforms/general.
'transliterateOptions' => 'Russian-Latin/BGN; Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC;'
],
];
}
```

## Author

[Altynbek Kazezov](https://github.com/altynbek07/), e-mail: [altinbek__97@mail.ru](mailto:altinbek__97@mail.ru)
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "yii2-bridge/slug-behavior",
"description": "Slug behavior for Bridge",
"type": "yii2-extension",
"keywords": [
"yii2",
"bridge",
"slug",
"sluggable",
"admin",
],
"license": "BSD-3-Clause",
"authors": [
{
"name": "Altynbek Kazezov",
"email": "altinbek__97@mail.ru",
"role": "Developer"
}
],
"autoload": {
"psr-4": {
"naffiq\\bridge\\behaviors\\": "src/behaviors/"
}
},
"minimum-stability": "stable",
"require": {
"zelenin/yii2-slug-behavior": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "^5.4"
}
}
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
115 changes: 115 additions & 0 deletions src/behaviors/BridgeSlugBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace naffiq\bridge\behaviors


use Zelenin\yii\behaviors\Slug;
use Zelenin\yii\behaviors\Service\Slugifier;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

class BridgeSlugBehavior extends Slug
{
/**
* @var bool
*/
private $slugIsEmpty = false;

/**
* @inheritdoc
*/
protected function getValue($event)
{
/* @var $owner ActiveRecord */
$owner = $this->owner;

if (!empty($owner->{$this->slugAttribute}) && !$this->slugIsEmpty && $this->immutable) {
$slug = $owner->{$this->slugAttribute};
} else {
if ($owner->getIsNewRecord()) {
$this->slugIsEmpty = true;
}
if ($this->attribute !== null) {
$attributes = $this->attribute;

$slugParts = array_map(function ($attribute) {
return ArrayHelper::getValue($this->owner, $attribute);
}, $attributes);

$slug = $this->slugify(implode($this->replacement, $slugParts), $this->replacement, $this->lowercase);

if (!$owner->getIsNewRecord() && $this->slugIsEmpty) {
$owner->{$this->slugAttribute} = $slug;
$owner->save(false, [$this->slugAttribute]);
}
} else {
$slug = parent::getValue($event);
}
}

if ($this->ensureUnique) {
$baseSlug = $slug;
$iteration = 0;
while (!$this->validateSlug($slug)) {
$iteration++;
$slug = $this->generateUniqueSlug($baseSlug, $iteration);
}
}

return $slug;
}

/**
* @param string $string
* @param string $replacement
* @param bool $lowercase
*
* @return string
*/
private function slugify($string, $replacement = '-', $lowercase = true)
{
$string = $this->transliterateKazakhToLatin($string);

$transliterateOptions = $this->transliterateOptions !== null ? $this->transliterateOptions : 'Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFKC;';
return (new Slugifier($transliterateOptions, $replacement, $lowercase))->slugify($string);
}

/**
* Заменяем казахские буквы на латинские
*
* @param $string
* @return string
*/
private function transliterateKazakhToLatin($string)
{
return strtr($string, self::getKazakhAlphabet());
}

/**
* Возвращает массив с казахсикими буквами с латиницой
*
* @return array
*/
private function getKazakhAlphabet() {
return [
'ә' => 'a',
'ғ' => 'g',
'қ' => 'q',
'ң' => 'n',
'ө' => 'o',
'ұ' => 'u',
'ү' => 'u',
'һ' => 'h',
'і' => 'i',

'Ә' => 'A',
'Ғ' => 'G',
'Қ' => 'Q',
'Ө' => 'O',
'Ұ' => 'U',
'Ү' => 'U',
'Һ' => 'H',
'І' => 'I',
];
}
}
7 changes: 7 additions & 0 deletions tests/BridgeSlugBehaviorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
use naffiq\bridge\behaviors\BridgeSlugBehavior;

class BridgeSlugBehaviorTest extends \PHPUnit\Framework\TestCase
{
//TODO: Надо написать тесты
}
7 changes: 7 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: rocketscientist
* Date: 07.06.2018
* Time: 11:00
*/

0 comments on commit 23d831e

Please sign in to comment.