feature/slug-handling #18
32
src/PersistentData/Model/ModelWithSlug.php
Normal file
32
src/PersistentData/Model/ModelWithSlug.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php namespace SokoWeb\PersistentData\Model;
|
||||
|
||||
use Cocur\Slugify\Slugify;
|
||||
|
||||
abstract class ModelWithSlug extends Model
|
||||
{
|
||||
protected static string $slugSource;
|
||||
|
||||
protected ?string $slug = null;
|
||||
|
||||
public static function getFields(): array
|
||||
{
|
||||
return array_merge(['id', 'slug'], static::$fields);
|
||||
}
|
||||
|
||||
public function setSlug(?string $slug): void
|
||||
{
|
||||
$this->slug = $slug;
|
||||
}
|
||||
|
||||
public function getSlug(): ?string
|
||||
{
|
||||
return $this->slug;
|
||||
}
|
||||
|
||||
public function generateSlug(): string
|
||||
{
|
||||
$slugSourceGetMethod = 'get' . str_replace('_', '', ucwords(static::$slugSource, '_'));
|
||||
$this->slug = Slugify::create()->slugify($this->$slugSourceGetMethod());
|
||||
return $this->slug;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user