Files
oficial/app/src/Repository/Proyecto/Broker/Data.php

67 lines
2.5 KiB
PHP
Raw Normal View History

2025-02-18 16:02:10 -03:00
<?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);
}
2025-02-24 12:39:42 -03:00
public function getTable(): string
{
return 'broker_data';
}
2025-02-18 16:02:10 -03:00
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) {
2025-02-24 12:39:42 -03:00
if ($data['representative_rut'] == null) return null;
2025-02-18 16:02:10 -03:00
try {
return $this->personaRepository->fetchById($data['representative_rut']);
} catch (Common\Implement\Exception\EmptyResult) {
return null;
}
}))
2025-02-24 12:39:42 -03:00
->register('legal_name', (new Common\Implement\Repository\Mapper())
->setProperty('legalName')
2025-02-18 16:02:10 -03:00
->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
{
2025-02-24 12:39:42 -03:00
return $this->update($model, ['representative_rut', 'legal_name'], $new_data);
2025-02-18 16:02:10 -03:00
}
2025-02-24 12:39:42 -03:00
public function fetchByBroker(int $broker_rut): Model\Proyecto\Broker\Data
2025-02-18 16:02:10 -03:00
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('broker_rut = :broker_rut');
2025-02-24 12:39:42 -03:00
return $this->fetchOne($query, ['broker_rut' => $broker_rut]);
2025-02-18 16:02:10 -03:00
}
2025-02-24 12:39:42 -03:00
}