This commit is contained in:
2022-11-25 20:52:52 -03:00
parent dd0410a0fb
commit efed50cd7f
39 changed files with 2777 additions and 5 deletions

View 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]);
}
}