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