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

79 lines
2.9 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,
2025-03-07 17:11:59 -03:00
protected Repository\Proyecto\Broker\Contact $contactRepository)
2025-02-18 16:02:10 -03:00
{
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']);
}))
2025-03-07 17:11:59 -03:00
->register('representative_id', (new Common\Implement\Repository\Mapper())
2025-02-18 16:02:10 -03:00
->setProperty('representative')
->setDefault(null)
->setFunction(function($data) {
2025-03-07 17:11:59 -03:00
if ($data['representative_id'] === null) return null;
2025-02-18 16:02:10 -03:00
try {
2025-03-07 17:11:59 -03:00
return $this->contactRepository->fetchById($data['representative_id']);
2025-02-18 16:02:10 -03:00
} 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(
2025-03-07 17:11:59 -03:00
['broker_rut', 'representative_id', 'legal_name'],
[$model->broker->rut, $model->representative?->id, $model->legalName]);
2025-02-18 16:02:10 -03:00
return $model;
}
2025-03-03 14:55:57 -03:00
/**
* @param Common\Define\Model $model
* @param array $new_data
* @return Model\Proyecto\Broker\Data
* @throws Common\Implement\Exception\EmptyResult
*/
2025-02-18 16:02:10 -03:00
public function edit(Common\Define\Model $model, array $new_data): Model\Proyecto\Broker\Data
{
2025-03-07 17:11:59 -03:00
return $this->update($model, ['representative_id', 'legal_name'], $new_data);
2025-02-18 16:02:10 -03:00
}
2025-03-03 14:55:57 -03:00
/**
* @param int $broker_rut
* @return Model\Proyecto\Broker\Data
* @throws Common\Implement\Exception\EmptyResult
*/
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
}