willy.club/app/App/Model/ChatModel.php

50 lines
1.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Model;
use App\Config;
ChatModel::init();
abstract class ChatModel
{
private static array $messages;
static function init()
{
if (!file_exists(Config::get('path_to_chat_json_file'))) {
file_put_contents(
Config::get('path_to_chat_json_file'),
json_encode([])
);
}
self::$messages = json_decode(
file_get_contents(Config::get('path_to_chat_json_file')),
true
);
}
static function get_messages(): array
{
return self::$messages;
}
static function save_message(string $nick, string $text)
{
if (count(self::$messages) > 100) {
array_pop($messages);
}
array_unshift(self::$messages, [
'nick' => $nick,
'date' => time(),
'text' => $text
]);
file_put_contents(Config::get('path_to_chat_json_file'),
json_encode(
self::$messages
)
);
}
}