Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zelenin committed Dec 23, 2015
1 parent 555b7d2 commit 621d0af
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013-2014 Aleksandr Zelenin
Copyright (c) 2013-2016 Aleksandr Zelenin

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
39 changes: 39 additions & 0 deletions Service/Slugifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Zelenin\yii\behaviors\Service;

use Zelenin\Ddd\String\Domain\Model\TransformerCollection;
use Zelenin\Ddd\String\Infrastructure\Service\Transformer;
use Zelenin\Ddd\String\Infrastructure\Service\Transformer\IntlTransliterateTransformer;
use Zelenin\Ddd\String\Infrastructure\Service\Transformer\UrlifyTransformer;

class Slugifier
{
/**
* @var Transformer
*/
private $transformer;

/**
* @param string $transliterateOptions
* @param string $replacement
* @param bool $lowercase
*/
public function __construct($transliterateOptions, $replacement, $lowercase)
{
$this->transformer = new Transformer(new TransformerCollection([
new IntlTransliterateTransformer($transliterateOptions),
new UrlifyTransformer($replacement, $lowercase)
]));
}

/**
* @param $string
*
* @return string
*/
public function slugify($string)
{
return $this->transformer->transform($string);
}
}
19 changes: 7 additions & 12 deletions Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\validators\UniqueValidator;
use Zelenin\Slugifier\Slugifier;
use Zelenin\yii\behaviors\Service\Slugifier;

class Slug extends SluggableBehavior
{
Expand Down Expand Up @@ -86,10 +86,10 @@ protected function getValue($event)
if ($this->attribute !== null) {
$attributes = $this->attribute;

$slugParts = [];
foreach ($attributes as $attribute) {
$slugParts[] = ArrayHelper::getValue($this->owner, $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) {
Expand Down Expand Up @@ -122,13 +122,8 @@ protected function getValue($event)
*/
private function slugify($string, $replacement = '-', $lowercase = true)
{
$slugifier = new Slugifier($string);
if ($this->transliterateOptions !== null) {
$slugifier->transliterateOptions = $this->transliterateOptions;
}
$slugifier->replacement = $replacement;
$slugifier->lowercase = $lowercase;
return $slugifier->getSlug();
$transliterateOptions = $this->transliterateOptions !== null ? $this->transliterateOptions : 'Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFKC;';
return (new Slugifier($transliterateOptions, $replacement, $lowercase))->slugify($string);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"description": "Yii2 slug behavior",
"type": "yii2-extension",
"keywords": [
"yii",
"yii2",
"slug",
"behavior"
Expand All @@ -24,7 +23,7 @@
},
"require": {
"yiisoft/yii2": "~2.0",
"zelenin/slugifier": "~0.0"
"zelenin/string": "~0.0.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

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

Either run ```php composer.phar require zelenin/yii2-slug-behavior "~1.0@stable"```
Either run ```php composer.phar require zelenin/yii2-slug-behavior "~1.5.0"```

or add ```"zelenin/yii2-slug-behavior": "~1.0@stable"``` to the require section of your ```composer.json```
or add ```"zelenin/yii2-slug-behavior": "~1.5.0"``` to the require section of your ```composer.json```

### Using

Expand Down

0 comments on commit 621d0af

Please sign in to comment.