-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlugField.php
49 lines (40 loc) · 1.24 KB
/
SlugField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
/*
* Studio 107 (c) 2017 Maxim Falaleev
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mindy\Orm\Fields;
use Mindy\Orm\ModelInterface;
/**
* Class SlugField.
*/
class SlugField extends AbstractSlugField
{
public function beforeInsert(ModelInterface $model, $value)
{
$this->value = empty($this->value) ? $this->createSlug($model->{$this->source}) : $this->value;
if ($this->unique) {
$this->value = $this->generateUniqueUrl($this->value);
}
$model->setAttribute($this->getAttributeName(), $this->value);
}
public function canBeEmpty()
{
return true;
}
public function beforeUpdate(ModelInterface $model, $value)
{
$this->value = $value;
// Случай когда обнулен slug, например из админки
if (empty($value)) {
$this->value = $this->createSlug($model->{$this->source});
}
if ($this->unique) {
$this->value = $this->generateUniqueUrl($this->value, 0, $model->pk);
}
$model->setAttribute($this->getAttributeName(), $this->value);
}
}