PHP code example of phpfacile / chat-db

1. Go to this page and download the library: Download phpfacile/chat-db library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpfacile / chat-db example snippets


$config = [
    'driver' => 'Pdo_Sqlite',
    'database' => 'my_chat_database.sqlite',
];
$adapter = new Zend\Db\Adapter\Adapter($config);

$config = [
    'driver' => 'Pdo_Mysql',
    'host' => 'localhost'
    'dbname' => 'my_database',
    'user' => 'my_user',
    'password' => 'my_pass',
];
$adapter = new Zend\Db\Adapter\Adapter($config);

use PHPFacile\Chat\Service\ChatChannelService;

$chatChannelService = new ChatChannelService();

use PHPFacile\Chat\Service\ChatService;

$chatService = new ChatService($adapter, $chatChannelService);

$chatService->addMessage($text, $channelId, $userId);

$msgs = $chatService->getMessages($channelId, $userId);

foreach ($msgs as $msg) {
    echo 'Id of the msg = '.$msg->id."\n";
    echo 'Text = '.$msg->text."\n";
    echo 'User id = '.$msg->user->id."\n";
    echo 'Posted at = '.$msg->insertionDateTimeUTC."\n";
}

$dateTime = $chatService->getLastUserMessageDateTimeUTC($channelId, $userId);

use PHPFacile\Chat\Service\ChatChannelServiceInterface;

class CustomChatChannelService implements ChatChannelServiceInterface
{
    public function isUserAllowedToAccessChannelMessages($userId, $channelId, $right)
    {
        // Return true if the user must be allowed to access
        // to the content of the chat channel either for
        // reading ($right = self::RIGHT_CHANNEL_MSG_READ) or for
        // writing ($right = self::RIGHT_CHANNEL_MSG_WRITE)
        return true;
    }
}

$chatChannelService = new CustomChatChannelService();

$extraData = [
    'software' => 'MyChatApp',
];
$chatService->addMessage($text, $channelId, $userId, extraData);