mirror of
https://github.com/flarum/core.git
synced 2025-10-28 05:56:12 +01:00
- Support slug drivers for core's sluggable models, easily extends to other models - Add automated testing for affected single-model API routes - Fix nickname selection UI - Serialize slugs as `slug` attribute - Make min search length a constant
43 lines
1002 B
PHP
43 lines
1002 B
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Flarum.
|
|
*
|
|
* For detailed copyright and license information, please view the
|
|
* LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Flarum\Discussion;
|
|
|
|
use Flarum\Database\AbstractModel;
|
|
use Flarum\Http\SlugDriverInterface;
|
|
use Flarum\User\User;
|
|
|
|
class IdWithTransliteratedSlugDriver implements SlugDriverInterface
|
|
{
|
|
/**
|
|
* @var DiscussionRepository
|
|
*/
|
|
protected $discussions;
|
|
|
|
public function __construct(DiscussionRepository $discussions)
|
|
{
|
|
$this->discussions = $discussions;
|
|
}
|
|
|
|
public function toSlug(AbstractModel $instance): string
|
|
{
|
|
return $instance->id.(trim($instance->slug) ? '-'.$instance->slug : '');
|
|
}
|
|
|
|
public function fromSlug(string $slug, User $actor): AbstractModel
|
|
{
|
|
if (strpos($slug, '-')) {
|
|
$slug_array = explode('-', $slug);
|
|
$slug = $slug_array[0];
|
|
}
|
|
|
|
return $this->discussions->findOrFail($slug, $actor);
|
|
}
|
|
}
|