Added Documentation
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Documentation\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $documentation_document_id
|
||||
* @property string $disk
|
||||
* @property string $path
|
||||
* @property string $original_name
|
||||
* @property string|null $mime_type
|
||||
* @property int|null $size
|
||||
*/
|
||||
class DocumentationAttachment extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/** @var list<string> */
|
||||
protected $fillable = [
|
||||
'documentation_document_id',
|
||||
'disk',
|
||||
'path',
|
||||
'original_name',
|
||||
'mime_type',
|
||||
'size',
|
||||
'uploaded_by',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'size' => 'integer',
|
||||
];
|
||||
|
||||
/**
|
||||
* Owning document.
|
||||
*
|
||||
* @return BelongsTo<DocumentationDocument, DocumentationAttachment>
|
||||
*/
|
||||
public function document(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(DocumentationDocument::class, 'documentation_document_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a readable file size label.
|
||||
*/
|
||||
public function getSizeLabelAttribute(): string
|
||||
{
|
||||
$bytes = (int) $this->size;
|
||||
|
||||
if ($bytes <= 0) {
|
||||
return 'Unknown size';
|
||||
}
|
||||
|
||||
if ($bytes < 1024) {
|
||||
return $bytes . ' B';
|
||||
}
|
||||
|
||||
if ($bytes < 1048576) {
|
||||
return number_format($bytes / 1024, 1) . ' KB';
|
||||
}
|
||||
|
||||
return number_format($bytes / 1048576, 1) . ' MB';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Documentation\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $slug
|
||||
* @property string|null $icon
|
||||
* @property string $colour
|
||||
* @property string|null $description
|
||||
* @property int $sort_order
|
||||
* @property bool $is_active
|
||||
*/
|
||||
class DocumentationCategory extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/** @var list<string> */
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'icon',
|
||||
'colour',
|
||||
'description',
|
||||
'sort_order',
|
||||
'is_active',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
|
||||
/**
|
||||
* Bootstrap the model.
|
||||
*/
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (DocumentationCategory $category): void {
|
||||
if (blank($category->slug)) {
|
||||
$category->slug = Str::slug($category->name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Documents assigned to this category.
|
||||
*
|
||||
* @return HasMany<DocumentationDocument>
|
||||
*/
|
||||
public function documents(): HasMany
|
||||
{
|
||||
return $this->hasMany(DocumentationDocument::class, 'documentation_category_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Active category scope.
|
||||
*
|
||||
* @param Builder<DocumentationCategory> $query
|
||||
* @return Builder<DocumentationCategory>
|
||||
*/
|
||||
public function scopeActive(Builder $query): Builder
|
||||
{
|
||||
return $query->where('is_active', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ordered category scope.
|
||||
*
|
||||
* @param Builder<DocumentationCategory> $query
|
||||
* @return Builder<DocumentationCategory>
|
||||
*/
|
||||
public function scopeOrdered(Builder $query): Builder
|
||||
{
|
||||
return $query->orderBy('sort_order')->orderBy('name');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Documentation\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int|null $documentation_category_id
|
||||
* @property string $title
|
||||
* @property string $slug
|
||||
* @property string|null $summary
|
||||
* @property string|null $body
|
||||
* @property string $status
|
||||
* @property bool $is_featured
|
||||
* @property string $version
|
||||
* @property string|null $reference
|
||||
* @property-read string $rendered_body
|
||||
*/
|
||||
class DocumentationDocument extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use SoftDeletes;
|
||||
|
||||
public const STATUS_DRAFT = 'draft';
|
||||
public const STATUS_PUBLISHED = 'published';
|
||||
public const STATUS_ARCHIVED = 'archived';
|
||||
|
||||
/** @var list<string> */
|
||||
protected $fillable = [
|
||||
'documentation_category_id',
|
||||
'title',
|
||||
'slug',
|
||||
'summary',
|
||||
'body',
|
||||
'status',
|
||||
'is_featured',
|
||||
'version',
|
||||
'reference',
|
||||
'attachment_disk',
|
||||
'attachment_path',
|
||||
'attachment_name',
|
||||
'attachment_mime',
|
||||
'attachment_size',
|
||||
'published_at',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'is_featured' => 'boolean',
|
||||
'published_at' => 'datetime',
|
||||
'attachment_size' => 'integer',
|
||||
];
|
||||
|
||||
/**
|
||||
* Bootstrap the model.
|
||||
*/
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (DocumentationDocument $document): void {
|
||||
if (blank($document->slug)) {
|
||||
$document->slug = Str::slug($document->title);
|
||||
}
|
||||
|
||||
if ($document->status === self::STATUS_PUBLISHED && blank($document->published_at)) {
|
||||
$document->published_at = now();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Owning category.
|
||||
*
|
||||
* @return BelongsTo<DocumentationCategory, DocumentationDocument>
|
||||
*/
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(DocumentationCategory::class, 'documentation_category_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Document attachments.
|
||||
*
|
||||
* @return HasMany<DocumentationAttachment>
|
||||
*/
|
||||
public function attachments(): HasMany
|
||||
{
|
||||
return $this->hasMany(DocumentationAttachment::class, 'documentation_document_id')->latest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Published document scope.
|
||||
*
|
||||
* @param Builder<DocumentationDocument> $query
|
||||
* @return Builder<DocumentationDocument>
|
||||
*/
|
||||
public function scopePublished(Builder $query): Builder
|
||||
{
|
||||
return $query->where('status', self::STATUS_PUBLISHED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Featured document scope.
|
||||
*
|
||||
* @param Builder<DocumentationDocument> $query
|
||||
* @return Builder<DocumentationDocument>
|
||||
*/
|
||||
public function scopeFeatured(Builder $query): Builder
|
||||
{
|
||||
return $query->where('is_featured', true);
|
||||
}
|
||||
/**
|
||||
* Render the document body as safe Markdown HTML for display.
|
||||
*/
|
||||
public function getRenderedBodyAttribute(): string
|
||||
{
|
||||
$body = trim((string) $this->body);
|
||||
|
||||
if ($body === '') {
|
||||
return '<p>No document body has been added yet.</p>';
|
||||
}
|
||||
|
||||
if (method_exists(Str::class, 'markdown')) {
|
||||
return (string) Str::markdown($body, [
|
||||
'html_input' => 'strip',
|
||||
'allow_unsafe_links' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
return '<p>' . nl2br(e($body), false) . '</p>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the attached file is a Markdown document.
|
||||
*/
|
||||
public function hasMarkdownAttachment(): bool
|
||||
{
|
||||
$name = strtolower((string) $this->attachment_name);
|
||||
$mime = strtolower((string) $this->attachment_mime);
|
||||
|
||||
return str_ends_with($name, '.md') || in_array($mime, ['text/markdown', 'text/x-markdown', 'text/plain'], true);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user