Broker Contact
This commit is contained in:
60
app/src/Repository/Proyecto/Broker/Contact.php
Normal file
60
app/src/Repository/Proyecto/Broker/Contact.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Proyecto\Broker;
|
||||
|
||||
use Incoviba\Common;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Contact extends Common\Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Repository\Direccion $direccionRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('broker_contacts');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Proyecto\Broker\Contact
|
||||
{
|
||||
$map = (new Common\Implement\Repository\MapperParser(['name', 'email', 'phone']))
|
||||
->register('address_id', (new Common\Implement\Repository\Mapper())
|
||||
->setProperty('address')
|
||||
->setDefault(null)
|
||||
->setFunction(function($data) {
|
||||
if ($data['address_id'] === null) return null;
|
||||
try {
|
||||
return $this->direccionRepository->fetchById($data['address_id']);
|
||||
} catch (Common\Implement\Exception\EmptyResult) {
|
||||
return null;
|
||||
}
|
||||
}));
|
||||
return $this->parseData(new Model\Proyecto\Broker\Contact(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Proyecto\Broker\Contact
|
||||
{
|
||||
$model->id = $this->saveNew([
|
||||
'name', 'email', 'phone', 'address_id'
|
||||
], [
|
||||
$model->name, $model->email, $model->phone, $model->address?->id
|
||||
]);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Proyecto\Broker\Contact
|
||||
{
|
||||
return $this->update($model, ['name', 'email', 'phone', 'address_id'], $new_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Model\Proyecto\Broker\Contact
|
||||
* @throws Common\Implement\Exception\EmptyResult
|
||||
*/
|
||||
public function fetchByName(string $name): Model\Proyecto\Broker\Contact
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('name = :name');
|
||||
return $this->fetchOne($query, ['name' => $name]);
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ class Data extends Common\Ideal\Repository
|
||||
{
|
||||
public function __construct(Common\Define\Connection $connection,
|
||||
protected Repository\Proyecto\Broker $brokerRepository,
|
||||
protected Repository\Persona $personaRepository)
|
||||
protected Repository\Proyecto\Broker\Contact $contactRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
}
|
||||
@ -27,13 +27,13 @@ class Data extends Common\Ideal\Repository
|
||||
->setFunction(function($data) {
|
||||
return $this->brokerRepository->fetchById($data['broker_rut']);
|
||||
}))
|
||||
->register('representative_rut', (new Common\Implement\Repository\Mapper())
|
||||
->register('representative_id', (new Common\Implement\Repository\Mapper())
|
||||
->setProperty('representative')
|
||||
->setDefault(null)
|
||||
->setFunction(function($data) {
|
||||
if ($data['representative_rut'] == null) return null;
|
||||
if ($data['representative_id'] === null) return null;
|
||||
try {
|
||||
return $this->personaRepository->fetchById($data['representative_rut']);
|
||||
return $this->contactRepository->fetchById($data['representative_id']);
|
||||
} catch (Common\Implement\Exception\EmptyResult) {
|
||||
return null;
|
||||
}
|
||||
@ -46,8 +46,8 @@ class Data extends Common\Ideal\Repository
|
||||
public function save(Common\Define\Model $model): Model\Proyecto\Broker\Data
|
||||
{
|
||||
$model->id = $this->saveNew(
|
||||
['broker_rut', 'representative_rut', 'legal_name'],
|
||||
[$model->broker->rut, $model->representative?->rut, $model->legalName]);
|
||||
['broker_rut', 'representative_id', 'legal_name'],
|
||||
[$model->broker->rut, $model->representative?->id, $model->legalName]);
|
||||
return $model;
|
||||
}
|
||||
|
||||
@ -59,7 +59,7 @@ class Data extends Common\Ideal\Repository
|
||||
*/
|
||||
public function edit(Common\Define\Model $model, array $new_data): Model\Proyecto\Broker\Data
|
||||
{
|
||||
return $this->update($model, ['representative_rut', 'legal_name'], $new_data);
|
||||
return $this->update($model, ['representative_id', 'legal_name'], $new_data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user