Skip to content

Commit 5f00a00

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 909fbcb + b991114 commit 5f00a00

File tree

4 files changed

+94
-25
lines changed

4 files changed

+94
-25
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ language: php
33
php:
44
- 7.1
55
- 7.2
6+
- 7.3
7+
- 7.4
8+
- 8.0
69

710
env:
811
matrix:

src/HasSlug.php

+24-20
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,20 @@ protected function generateNonUniqueSlug(): string
6363
{
6464
if ($this->hasCustomSlugBeenUsed()) {
6565
$slugField = $this->slugOptions->slugCustomField;
66-
if(!$this->$slugField){
66+
if (!$this->$slugField) {
6767
return '';
6868
}
69-
return Str::slug($this->$slugField, $this->slugOptions->separator);
69+
return !$this->slugOptions->slugifyCustomSlug ? $this->$slugField : Str::slug($this->$slugField, $this->slugOptions->separator);
7070
}
7171
if ($this->hasSlugBeenUsed()) {
7272
$slugField = $this->slugOptions->slugField;
7373
return $this->$slugField ?? '';
7474
}
75-
return Str::slug($this->getSlugSourceString(), $this->slugOptions->separator);
75+
$generatedSlug = $this->getSlugSourceString();
76+
if (!$this->slugOptions->slugifySlugSourceString) {
77+
return $generatedSlug;
78+
}
79+
return Str::slug($generatedSlug, $this->slugOptions->separator);
7680
}
7781

7882
/**
@@ -82,7 +86,7 @@ protected function generateNonUniqueSlug(): string
8286
protected function hasCustomSlugBeenUsed(): bool
8387
{
8488
$slugField = $this->slugOptions->slugCustomField;
85-
if(!$slugField || trim($slugField)==''|| !$this->$slugField || trim($this->$slugField)==''){
89+
if (!$slugField || trim($slugField) == '' || !$this->$slugField || trim($this->$slugField) == '') {
8690
return false;
8791
}
8892
return true;
@@ -96,7 +100,7 @@ protected function hasSlugBeenUsed(): bool
96100
{
97101
$slugField = $this->slugOptions->slugField;
98102

99-
if(!$slugField || trim($slugField)==''|| !$this->$slugField || trim($this->$slugField)==''){
103+
if (!$slugField || trim($slugField) == '' || !$this->$slugField || trim($this->$slugField) == '') {
100104
return false;
101105
}
102106
return $this->getOriginal($slugField) != $this->$slugField;
@@ -116,8 +120,8 @@ protected function getSlugSourceString(): string
116120

117121
$slugFrom = $this->getSlugFrom($this->slugOptions->generateSlugFrom);
118122

119-
if(is_null($slugFrom) || (!is_array($slugFrom) && trim($slugFrom)=='')){
120-
if(!$this->slugOptions->generateSlugIfAllSourceFieldsEmpty){
123+
if (is_null($slugFrom) || (!is_array($slugFrom) && trim($slugFrom) == '')) {
124+
if (!$this->slugOptions->generateSlugIfAllSourceFieldsEmpty) {
121125
throw InvalidOption::missingFromField();
122126
}
123127

@@ -136,35 +140,35 @@ protected function getSlugSourceString(): string
136140
*/
137141
protected function getSlugFrom($fieldName)
138142
{
139-
if(!is_callable($fieldName) && !is_array($fieldName) && trim($fieldName)==''){
143+
if (!is_callable($fieldName) && !is_array($fieldName) && trim($fieldName) == '') {
140144
return '';
141145
}
142146

143-
if(!is_callable($fieldName) && !is_array($fieldName) && (!data_get($this, $fieldName))){
147+
if (!is_callable($fieldName) && !is_array($fieldName) && (!data_get($this, $fieldName))) {
144148
return '';
145-
}elseif (!is_array($fieldName)){
149+
} elseif (!is_array($fieldName)) {
146150
return $fieldName;
147151
}
148152

149153
$slugSourceString = '';
150154
$countFieldName = count($fieldName);
151-
for($i=0;$i<$countFieldName;$i++){
155+
for ($i = 0; $i < $countFieldName; $i++) {
152156

153157
$currFieldName = $fieldName[$i];
154-
if(!is_array($currFieldName) && trim($currFieldName)==''){
158+
if (!is_array($currFieldName) && trim($currFieldName) == '') {
155159
continue;
156160
}
157-
if (!is_array($currFieldName) && (!data_get($this, $currFieldName))){
161+
if (!is_array($currFieldName) && (!data_get($this, $currFieldName))) {
158162
continue;
159163
}
160-
if (!is_array($currFieldName) && data_get($this, $currFieldName)){
164+
if (!is_array($currFieldName) && data_get($this, $currFieldName)) {
161165
$slugSourceString = $currFieldName;
162166
break;
163167
}
164168

165169
$slugSourceString = $this->getImplodeSourceString($currFieldName, '');
166170

167-
if($slugSourceString!=''){
171+
if ($slugSourceString != '') {
168172
$slugSourceString = $currFieldName;
169173
break;
170174
}
@@ -184,7 +188,7 @@ protected function makeSlugUnique(string $slug): string
184188
$i = 1;
185189

186190
while ($this->otherRecordExistsWithSlug($slug) || $slug === '') {
187-
$slug = $originalSlug.$this->slugOptions->separator.$i++;
191+
$slug = $originalSlug . $this->slugOptions->separator . $i++;
188192
}
189193

190194
return $slug;
@@ -197,7 +201,7 @@ protected function makeSlugUnique(string $slug): string
197201
*/
198202
protected function otherRecordExistsWithSlug(string $slug): bool
199203
{
200-
return (bool) static::where($this->slugOptions->slugField, $slug)
204+
return (bool)static::where($this->slugOptions->slugField, $slug)
201205
->where($this->getKeyName(), '!=', $this->getKey() ?? '0')
202206
->first();
203207
}
@@ -208,7 +212,7 @@ protected function otherRecordExistsWithSlug(string $slug): bool
208212
*/
209213
protected function guardAgainstInvalidSlugOptions()
210214
{
211-
if (is_array($this->slugOptions->generateSlugFrom) && count($this->slugOptions->generateSlugFrom)<1) {
215+
if (is_array($this->slugOptions->generateSlugFrom) && count($this->slugOptions->generateSlugFrom) < 1) {
212216
throw InvalidOption::missingFromField();
213217
}
214218

@@ -226,10 +230,10 @@ protected function guardAgainstInvalidSlugOptions()
226230
* @param string $separator
227231
* @return string
228232
*/
229-
protected function getImplodeSourceString($slugFrom, string $separator) : string
233+
protected function getImplodeSourceString($slugFrom, string $separator): string
230234
{
231235
$slugSourceString = collect($slugFrom)
232-
->map(function (string $fieldName) : string {
236+
->map(function (string $fieldName): string {
233237
if ($fieldName == '') {
234238
return '';
235239
}

src/SlugOptions.php

+28-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ class SlugOptions
2626
public $separator = '-';
2727

2828
/** @var int */
29-
public $randomUrlLen=50;
29+
public $randomUrlLen = 50;
3030

31+
/** @var bool */
32+
public $slugifySlugSourceString = true;//if setted to false don't call Str::slug on the slug generated automatically
33+
34+
/** @var bool */
35+
public $slugifyCustomSlug = true;//if setted to false don't call Str::slug on the slug custom field
3136

3237
/**
3338
* @return SlugOptions
@@ -37,6 +42,28 @@ public static function create(): SlugOptions
3742
return new static();
3843
}
3944

45+
/**
46+
* @param bool $bool
47+
* @return $this
48+
*/
49+
public function slugifySourceString($bool)
50+
{
51+
$this->slugifySlugSourceString = $bool ? true : false;
52+
53+
return $this;
54+
}
55+
56+
/**
57+
* @param bool $bool
58+
* @return $this
59+
*/
60+
public function slugifyCustomSlug($bool)
61+
{
62+
$this->slugifyCustomSlug = $bool ? true : false;
63+
64+
return $this;
65+
}
66+
4067
/**
4168
* @param string|array|callable $fieldName
4269
*

tests/Integration/HasSlugTest.php

+39-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,28 @@ public function getSlugOptions(): SlugOptions
2222
$model->save();
2323
$this->assertEquals('hello-dad', $model->url);
2424

25-
$model->url_custom = 'this is a custom test';
25+
$model->url_custom = 'this is a custom/test';
2626
$model->save();
27-
$this->assertEquals('this-is-a-custom-test', $model->url);
27+
$this->assertEquals('this-is-a-customtest', $model->url);
28+
}
29+
30+
/** @test */
31+
public function it_will_save_a_custom_slug_without_escaping_it()
32+
{
33+
$model = new class extends TestModel
34+
{
35+
public function getSlugOptions(): SlugOptions
36+
{
37+
return parent::getSlugOptions()->generateSlugsFrom('name')->saveCustomSlugsTo('url_custom')->slugifyCustomSlug(false);
38+
}
39+
};
40+
$model->name = 'hello dad';
41+
$model->save();
42+
$this->assertEquals('hello-dad', $model->url);
43+
44+
$model->url_custom = 'this/is/a/custom-test';
45+
$model->save();
46+
$this->assertEquals('this/is/a/custom-test', $model->url);
2847
}
2948

3049
/**
@@ -90,9 +109,25 @@ public function getSlugOptions(): SlugOptions
90109
/** @test */
91110
public function it_will_save_a_slug_when_saving_a_model()
92111
{
93-
$model = TestModel::create(['name' => 'this is a test']);
112+
$model = TestModel::create(['name' => 'this/is a test']);
94113

95-
$this->assertEquals('this-is-a-test', $model->url);
114+
$this->assertEquals('thisis-a-test', $model->url);
115+
}
116+
117+
/** @test */
118+
public function it_will_save_a_slug_without_escapes()
119+
{
120+
$model = new class extends TestModel
121+
{
122+
public function getSlugOptions(): SlugOptions
123+
{
124+
return parent::getSlugOptions()->generateSlugsFrom('name')->slugifySourceString(false);
125+
}
126+
};
127+
$model->name = 'this/is-a-test';
128+
$model->save();
129+
130+
$this->assertEquals('this/is-a-test', $model->url);
96131
}
97132

98133
/** @test */

0 commit comments

Comments
 (0)