Compare commits
3 Commits
585d469b69
...
b8ae295de6
Author | SHA1 | Date | |
---|---|---|---|
b8ae295de6 | |||
afec6f1103 | |||
9637ebc52b |
@ -6,7 +6,8 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"vlucas/phpdotenv": "^5.5",
|
"vlucas/phpdotenv": "^5.5",
|
||||||
"symfony/console": "^5.4",
|
"symfony/console": "^5.4",
|
||||||
"phpmailer/phpmailer": "^6.8"
|
"phpmailer/phpmailer": "^6.8",
|
||||||
|
"cocur/slugify": "^4.3"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^9.6",
|
"phpunit/phpunit": "^9.6",
|
||||||
|
76
composer.lock
generated
76
composer.lock
generated
@ -4,8 +4,82 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "079e8a8443fdc25cbe3b03322494b73b",
|
"content-hash": "d873b245eaaf660d70c21e06ee4bcf0a",
|
||||||
"packages": [
|
"packages": [
|
||||||
|
{
|
||||||
|
"name": "cocur/slugify",
|
||||||
|
"version": "v4.3.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/cocur/slugify.git",
|
||||||
|
"reference": "652234ef5f1be844a2ae1c36ad1b4c88b05160f9"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/cocur/slugify/zipball/652234ef5f1be844a2ae1c36ad1b4c88b05160f9",
|
||||||
|
"reference": "652234ef5f1be844a2ae1c36ad1b4c88b05160f9",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"ext-mbstring": "*",
|
||||||
|
"php": "^7.1 || ~8.0.0 || ~8.1.0 || ~8.2.0"
|
||||||
|
},
|
||||||
|
"conflict": {
|
||||||
|
"symfony/config": "<3.4 || >=4,<4.3",
|
||||||
|
"symfony/dependency-injection": "<3.4 || >=4,<4.3",
|
||||||
|
"symfony/http-kernel": "<3.4 || >=4,<4.3",
|
||||||
|
"twig/twig": "<2.12.1"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"laravel/framework": "^5.0|^6.0|^7.0|^8.0",
|
||||||
|
"latte/latte": "~2.2",
|
||||||
|
"league/container": "^2.2.0",
|
||||||
|
"mikey179/vfsstream": "~1.6.8",
|
||||||
|
"mockery/mockery": "^1.3",
|
||||||
|
"nette/di": "~2.4",
|
||||||
|
"pimple/pimple": "~1.1",
|
||||||
|
"plumphp/plum": "~0.1",
|
||||||
|
"symfony/config": "^3.4 || ^4.3 || ^5.0 || ^6.0",
|
||||||
|
"symfony/dependency-injection": "^3.4 || ^4.3 || ^5.0 || ^6.0",
|
||||||
|
"symfony/http-kernel": "^3.4 || ^4.3 || ^5.0 || ^6.0",
|
||||||
|
"symfony/phpunit-bridge": "^5.4 || ^6.0",
|
||||||
|
"twig/twig": "^2.12.1 || ~3.0",
|
||||||
|
"zendframework/zend-modulemanager": "~2.2",
|
||||||
|
"zendframework/zend-servicemanager": "~2.2",
|
||||||
|
"zendframework/zend-view": "~2.2"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Cocur\\Slugify\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Florian Eckerstorfer",
|
||||||
|
"email": "florian@eckerstorfer.co",
|
||||||
|
"homepage": "https://florian.ec"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ivo Bathke",
|
||||||
|
"email": "ivo.bathke@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Converts a string into a slug.",
|
||||||
|
"keywords": [
|
||||||
|
"slug",
|
||||||
|
"slugify"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/cocur/slugify/issues",
|
||||||
|
"source": "https://github.com/cocur/slugify/tree/v4.3.0"
|
||||||
|
},
|
||||||
|
"time": "2022-12-07T19:48:48+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "graham-campbell/result-type",
|
"name": "graham-campbell/result-type",
|
||||||
"version": "v1.1.1",
|
"version": "v1.1.1",
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,7 @@ use SokoWeb\Interfaces\Database\IAuditLogger;
|
|||||||
use SokoWeb\Interfaces\Database\IResultSet;
|
use SokoWeb\Interfaces\Database\IResultSet;
|
||||||
use SokoWeb\Interfaces\PersistentData\IPersistentDataManager;
|
use SokoWeb\Interfaces\PersistentData\IPersistentDataManager;
|
||||||
use SokoWeb\PersistentData\Model\Model;
|
use SokoWeb\PersistentData\Model\Model;
|
||||||
|
use SokoWeb\PersistentData\Model\ModelWithSlug;
|
||||||
|
|
||||||
class PersistentDataManager implements IPersistentDataManager
|
class PersistentDataManager implements IPersistentDataManager
|
||||||
{
|
{
|
||||||
@ -58,6 +59,14 @@ class PersistentDataManager implements IPersistentDataManager
|
|||||||
return $this->selectFromDb($select, $type, $useRelations, $withRelations);
|
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
|
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void
|
||||||
{
|
{
|
||||||
$relations = $model::getRelations();
|
$relations = $model::getRelations();
|
||||||
@ -103,12 +112,20 @@ class PersistentDataManager implements IPersistentDataManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (count($modified) > 0) {
|
if (count($modified) > 0) {
|
||||||
|
if ($model instanceof ModelWithSlug && isset($modified['slug'])) {
|
||||||
|
$modified['slug'] = $this->generateUniqueSlug($model, $modified['slug']);
|
||||||
|
}
|
||||||
|
|
||||||
$modify->setId($id);
|
$modify->setId($id);
|
||||||
$modify->setDiff($diff);
|
$modify->setDiff($diff);
|
||||||
$modify->fill($modified);
|
$modify->fill($modified);
|
||||||
$modify->save();
|
$modify->save();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if ($model instanceof ModelWithSlug) {
|
||||||
|
$modified['slug'] = $this->generateUniqueSlug($model, $modified['slug']);
|
||||||
|
}
|
||||||
|
|
||||||
$modify->fill($modified);
|
$modify->fill($modified);
|
||||||
$modify->save();
|
$modify->save();
|
||||||
|
|
||||||
@ -129,6 +146,27 @@ class PersistentDataManager implements IPersistentDataManager
|
|||||||
$model->resetSnapshot();
|
$model->resetSnapshot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function generateUniqueSlug(ModelWithSlug $model, string $notUniqueSlug): string
|
||||||
|
{
|
||||||
|
$slug = $notUniqueSlug;
|
||||||
|
$numbered = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
if ($numbered > 0) {
|
||||||
|
$slug = $slug . '_' . ($numbered + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$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
|
private function createSelect(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Select
|
||||||
{
|
{
|
||||||
$table = call_user_func([$type, 'getTable']);
|
$table = call_user_func([$type, 'getTable']);
|
||||||
|
Loading…
Reference in New Issue
Block a user