API
This commit is contained in:
80
api/src/Repository/State/Message.php
Normal file
80
api/src/Repository/State/Message.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
namespace ProVM\Emails\Repository\State;
|
||||
|
||||
use PDO;
|
||||
use ProVM\Common\Define\Model;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ProVM\Common\Implement\Repository;
|
||||
|
||||
class Message extends Repository
|
||||
{
|
||||
public function __construct(PDO $connection, LoggerInterface $logger)
|
||||
{
|
||||
parent::__construct($connection, $logger);
|
||||
$this->setTable('messages_states');
|
||||
}
|
||||
|
||||
protected function fieldsForUpdate(): array
|
||||
{
|
||||
return $this->fieldsForInsert();
|
||||
}
|
||||
protected function valuesForUpdate(Model $model): array
|
||||
{
|
||||
return array_merge($this->valuesForInsert($model), [$model->getId()]);
|
||||
}
|
||||
public function fieldsForInsert(): array
|
||||
{
|
||||
return [
|
||||
'message_id',
|
||||
'name',
|
||||
'value'
|
||||
];
|
||||
}
|
||||
protected function valuesForInsert(Model $model): array
|
||||
{
|
||||
return [
|
||||
$model->getMessage()->getId(),
|
||||
$model->getName(),
|
||||
$model->getValue() ? 1 : 0
|
||||
];
|
||||
}
|
||||
protected function defaultFind(Model $model): Model
|
||||
{
|
||||
return $this->fetchByMessageAndName($model->getMessage()->getId(), $model->getName());
|
||||
}
|
||||
protected function fieldsForCreate(): array
|
||||
{
|
||||
return $this->fieldsForInsert();
|
||||
}
|
||||
protected function valuesForCreate(array $data): array
|
||||
{
|
||||
return [
|
||||
$data['message_id'],
|
||||
$data['name'],
|
||||
$data['value'] ? 1 : 0
|
||||
];
|
||||
}
|
||||
protected function defaultSearch(array $data): Model
|
||||
{
|
||||
return $this->fetchByMessageAndName($data['message_id'], $data['name']);
|
||||
}
|
||||
|
||||
public function load(array $row): \ProVM\Emails\Model\State\Message
|
||||
{
|
||||
return (new \ProVM\Emails\Model\State\Message())
|
||||
->setId($row['id'])
|
||||
->setName($row['name'])
|
||||
->setValue(($row['value'] ?? 0) !== 0);
|
||||
}
|
||||
|
||||
public function fetchByMessage(int $message_id): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `message_id` = ?";
|
||||
return $this->fetchMany($query, [$message_id]);
|
||||
}
|
||||
public function fetchByMessageAndName(int $message_id, string $name): \ProVM\Emails\Model\State\Message
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `message_id` = ? AND `name` = ?";
|
||||
return $this->fetchOne($query, [$message_id, $name]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user