castopod/app/Models/PageModel.php
Yassine Doghri 5c5c6da4be
refactor: add rector to enforce type declarations, code quality + style and remove dead code
- update CI process to include quality stage (tests + code review)
- add captainhook to install git pre-commit & pre-push hooks
- remove .devcontainer Dockerfile to use project's docker-compose services: all
services can now be started automatically using vscode
- update docs/setup-development.md
2021-05-12 10:48:30 +00:00

82 lines
1.5 KiB
PHP

<?php
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Models;
use App\Entities\Page;
use CodeIgniter\Model;
class PageModel extends Model
{
/**
* @var string
*/
protected $table = 'pages';
/**
* @var string
*/
protected $primaryKey = 'id';
/**
* @var string[]
*/
protected $allowedFields = ['id', 'title', 'slug', 'content'];
/**
* @var string
*/
protected $returnType = Page::class;
/**
* @var bool
*/
protected $useSoftDeletes = true;
/**
* @var bool
*/
protected $useTimestamps = true;
/**
* @var array<string, string>
*/
protected $validationRules = [
'title' => 'required',
'slug' =>
'required|regex_match[/^[a-zA-Z0-9\-]{1,191}$/]|is_unique[pages.slug,id,{id}]',
'content' => 'required',
];
/**
* @var string[]
*/
protected $afterInsert = ['clearCache'];
/**
* Before update because slug or title might change
*
* @var string[]
*/
protected $beforeUpdate = ['clearCache'];
/**
* @var string[]
*/
protected $beforeDelete = ['clearCache'];
/**
* @return array<string, array<string|int, mixed>>
*/
protected function clearCache(array $data): array
{
// Clear the cache of all pages
cache()->deleteMatching('page*');
return $data;
}
}