50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?php
|
|
namespace Incoviba\Repository;
|
|
|
|
use Incoviba\Common\Define;
|
|
use Incoviba\Common\Ideal;
|
|
use Incoviba\Common\Implement;
|
|
use Incoviba\Model;
|
|
|
|
class Persona extends Ideal\Repository
|
|
{
|
|
public function __construct(Define\Connection $connection)
|
|
{
|
|
parent::__construct($connection);
|
|
$this->setTable('personas');
|
|
}
|
|
|
|
public function create(?array $data = null): Model\Persona
|
|
{
|
|
$map = (new Implement\Repository\MapperParser(['rut', 'digito', 'nombres']))
|
|
->register('apellido_paterno', (new Implement\Repository\Mapper())->setProperty('apellidoPaterno'))
|
|
->register('apellido_materno', (new Implement\Repository\Mapper())->setProperty('apellidoMaterno'))
|
|
;
|
|
return $this->parseData(new Model\Persona(), $data, $map);
|
|
}
|
|
public function save(Define\Model $model): Model\Persona
|
|
{
|
|
$this->saveNew(['rut', 'digito', 'nombres', 'apellido_paterno', 'apellido_materno'],
|
|
[$model->rut, $model->digito, $model->nombres, $model->apellidoPaterno, $model->apellidoMaterno]);
|
|
return $model;
|
|
}
|
|
public function edit(Define\Model $model, array $new_data): Model\Persona
|
|
{
|
|
return $this->update($model, $new_data, ['rut', 'digito', 'nombres', 'apellido_paterno', 'apellido_materno']);
|
|
}
|
|
|
|
public function fetchByRut(int $rut): Model\Persona
|
|
{
|
|
$query = $this->connection->getQueryBuilder()
|
|
->select()
|
|
->from($this->getTable())
|
|
->where('rut = ?');
|
|
return $this->fetchOne($query, [$rut]);
|
|
}
|
|
|
|
protected function getKey(): string
|
|
{
|
|
return 'rut';
|
|
}
|
|
}
|