Compare commits
3 Commits
a5fff1b710
...
6989e1dcf3
Author | SHA1 | Date | |
---|---|---|---|
6989e1dcf3 | |||
1288a33ff6 | |||
a5bfc61db8 |
@ -12,6 +12,8 @@ interface IPersistentDataManager
|
||||
|
||||
public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = []);
|
||||
|
||||
public function selectFromDbBySlug(string $slug, string $type, bool $useRelations = false, array $withRelations = []);
|
||||
|
||||
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void;
|
||||
|
||||
public function saveToDb(Model $model): void;
|
||||
|
@ -41,7 +41,7 @@ abstract class Model
|
||||
{
|
||||
$array = [];
|
||||
|
||||
foreach (self::getFields() as $key) {
|
||||
foreach (static::getFields() as $key) {
|
||||
$method = 'get' . str_replace('_', '', ucwords($key, '_'));
|
||||
|
||||
if (method_exists($this, $method)) {
|
||||
|
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;
|
||||
}
|
||||
}
|
@ -8,9 +8,12 @@ use SokoWeb\Interfaces\Database\IAuditLogger;
|
||||
use SokoWeb\Interfaces\Database\IResultSet;
|
||||
use SokoWeb\Interfaces\PersistentData\IPersistentDataManager;
|
||||
use SokoWeb\PersistentData\Model\Model;
|
||||
use SokoWeb\PersistentData\Model\ModelWithSlug;
|
||||
|
||||
class PersistentDataManager implements IPersistentDataManager
|
||||
{
|
||||
const SLUG_MAX_LENGTH = 255;
|
||||
|
||||
private IConnection $dbConnection;
|
||||
|
||||
private ?IAuditLogger $auditLogger;
|
||||
@ -58,6 +61,14 @@ class PersistentDataManager implements IPersistentDataManager
|
||||
return $this->selectFromDb($select, $type, $useRelations, $withRelations);
|
||||
}
|
||||
|
||||
public function selectFromDbBySlug(string $slug, string $type, bool $useRelations = false, array $withRelations = [])
|
||||
{
|
||||
$select = new Select($this->dbConnection);
|
||||
$select->where('slug', '=', $slug);
|
||||
|
||||
return $this->selectFromDb($select, $type, $useRelations, $withRelations);
|
||||
}
|
||||
|
||||
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void
|
||||
{
|
||||
$relations = $model::getRelations();
|
||||
@ -103,12 +114,21 @@ class PersistentDataManager implements IPersistentDataManager
|
||||
}
|
||||
|
||||
if (count($modified) > 0) {
|
||||
if ($model instanceof ModelWithSlug && isset($modified['slug'])) {
|
||||
$modified['slug'] = $this->generateUniqueSlug($model, $modified['slug']);
|
||||
}
|
||||
|
||||
$modify->setId($id);
|
||||
$modify->setDiff($diff);
|
||||
$modify->fill($modified);
|
||||
$modify->save();
|
||||
}
|
||||
} else {
|
||||
if ($model instanceof ModelWithSlug) {
|
||||
$slug = $model->generateSlug();
|
||||
$modified['slug'] = $this->generateUniqueSlug($model, $slug);
|
||||
}
|
||||
|
||||
$modify->fill($modified);
|
||||
$modify->save();
|
||||
|
||||
@ -129,6 +149,29 @@ class PersistentDataManager implements IPersistentDataManager
|
||||
$model->resetSnapshot();
|
||||
}
|
||||
|
||||
private function generateUniqueSlug(ModelWithSlug $model, string $notUniqueSlug): string
|
||||
{
|
||||
$numbered = 1;
|
||||
|
||||
do {
|
||||
if ($numbered > 1) {
|
||||
$slug = substr($notUniqueSlug, 0, static::SLUG_MAX_LENGTH - (strlen((string)$numbered) + 1));
|
||||
$slug = $notUniqueSlug . '_' . (string)$numbered;
|
||||
} else {
|
||||
$slug = substr($notUniqueSlug, 0, static::SLUG_MAX_LENGTH);
|
||||
}
|
||||
|
||||
$select = new Select($this->dbConnection, $model::getTable());
|
||||
$select->where('slug', '=', $slug);
|
||||
|
||||
$numbered++;
|
||||
} while ($select->count() != 0);
|
||||
|
||||
$model->setSlug($slug);
|
||||
|
||||
return $slug;
|
||||
}
|
||||
|
||||
private function createSelect(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Select
|
||||
{
|
||||
$table = call_user_func([$type, 'getTable']);
|
||||
|
Loading…
Reference in New Issue
Block a user