add class that handles model with slug
This commit is contained in:
parent
9637ebc52b
commit
afec6f1103
35
src/PersistentData/Model/ModelWithSlug.php
Normal file
35
src/PersistentData/Model/ModelWithSlug.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php namespace SokoWeb\PersistentData\Model;
|
||||
|
||||
use Cocur\Slugify\Slugify;
|
||||
|
||||
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
|
||||
{
|
||||
if ($this->slug === null) {
|
||||
$this->slug = $this->generateSlug();
|
||||
}
|
||||
|
||||
return $this->slug;
|
||||
}
|
||||
|
||||
private function generateSlug(): string
|
||||
{
|
||||
$slugSourceGetMethod = 'get' . str_replace('_', '', ucwords(static::$slugSource, '_'));
|
||||
return Slugify::create()->slugify($this->$slugSourceGetMethod());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user