59 lines
2.4 KiB
PHP
59 lines
2.4 KiB
PHP
![]() |
<?php
|
||
|
namespace Incoviba\Repository\Proyecto\Broker;
|
||
|
|
||
|
use Incoviba\Common;
|
||
|
use Incoviba\Repository;
|
||
|
use Incoviba\Model;
|
||
|
|
||
|
class Data extends Common\Ideal\Repository
|
||
|
{
|
||
|
public function __construct(Common\Define\Connection $connection,
|
||
|
protected Repository\Proyecto\Broker $brokerRepository,
|
||
|
protected Repository\Persona $personaRepository)
|
||
|
{
|
||
|
parent::__construct($connection);
|
||
|
}
|
||
|
|
||
|
public function create(?array $data = null): Model\Proyecto\Broker\Data
|
||
|
{
|
||
|
$map = (new Common\Implement\Repository\MapperParser())
|
||
|
->register('broker_rut', (new Common\Implement\Repository\Mapper())
|
||
|
->setProperty('broker')
|
||
|
->setFunction(function($data) {
|
||
|
return $this->brokerRepository->fetchById($data['broker_rut']);
|
||
|
}))
|
||
|
->register('representative_rut', (new Common\Implement\Repository\Mapper())
|
||
|
->setProperty('representative')
|
||
|
->setDefault(null)
|
||
|
->setFunction(function($data) {
|
||
|
try {
|
||
|
return $this->personaRepository->fetchById($data['representative_rut']);
|
||
|
} catch (Common\Implement\Exception\EmptyResult) {
|
||
|
return null;
|
||
|
}
|
||
|
}))
|
||
|
->register('legalName', (new Common\Implement\Repository\Mapper())
|
||
|
->setDefault(null));
|
||
|
return $this->parseData(new Model\Proyecto\Broker\Data(), $data, $map);
|
||
|
}
|
||
|
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]);
|
||
|
return $model;
|
||
|
}
|
||
|
public function edit(Common\Define\Model $model, array $new_data): Model\Proyecto\Broker\Data
|
||
|
{
|
||
|
return $this->update($model, ['broker_rut', 'representative_rut', 'legal_name'], $new_data);
|
||
|
}
|
||
|
|
||
|
public function fetchByBroker(int $brokerRut): Model\Proyecto\Broker\Data
|
||
|
{
|
||
|
$query = $this->connection->getQueryBuilder()
|
||
|
->select()
|
||
|
->from($this->getTable())
|
||
|
->where('broker_rut = :broker_rut');
|
||
|
return $this->fetchOne($query, ['broker_rut' => $brokerRut]);
|
||
|
}
|
||
|
}
|