Newer
Older

Yassine Doghri
committed
<?php

Yassine Doghri
committed
declare(strict_types=1);

Yassine Doghri
committed
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Models;

Yassine Doghri
committed
use App\Entities\Page;

Yassine Doghri
committed
use CodeIgniter\Model;
class PageModel extends Model
{

Yassine Doghri
committed
/**
* @var string
*/

Yassine Doghri
committed
protected $table = 'pages';

Yassine Doghri
committed
/**
* @var string
*/

Yassine Doghri
committed
protected $primaryKey = 'id';

Yassine Doghri
committed
/**
* @var string[]
*/
protected $allowedFields = ['id', 'title', 'slug', 'content_markdown', 'content_html'];

Yassine Doghri
committed

Yassine Doghri
committed
/**
* @var string
*/
protected $returnType = Page::class;

Yassine Doghri
committed
/**
* @var bool
*/

Yassine Doghri
committed
protected $useSoftDeletes = true;

Yassine Doghri
committed
/**
* @var bool
*/

Yassine Doghri
committed
protected $useTimestamps = true;

Yassine Doghri
committed
/**
* @var array<string, string>
*/

Yassine Doghri
committed
protected $validationRules = [
'title' => 'required',
'slug' =>
'required|regex_match[/^[a-zA-Z0-9\-]{1,191}$/]|is_unique[pages.slug,id,{id}]',
'content_markdown' => 'required',

Yassine Doghri
committed
];

Yassine Doghri
committed
/**
* @var string[]
*/

Yassine Doghri
committed
protected $afterInsert = ['clearCache'];

Yassine Doghri
committed
/**
* Before update because slug or title might change
*
* @var string[]
*/

Yassine Doghri
committed
protected $beforeUpdate = ['clearCache'];

Yassine Doghri
committed
/**
* @var string[]
*/

Yassine Doghri
committed
protected $beforeDelete = ['clearCache'];

Yassine Doghri
committed
/**
* @param mixed[] $data
*

Yassine Doghri
committed
* @return array<string, array<string|int, mixed>>
*/
protected function clearCache(array $data): array

Yassine Doghri
committed
{
// Clear the cache of all pages
cache()
->deleteMatching('page*');

Yassine Doghri
committed
return $data;
}
}