setTable('datos_personas'); } public function create(?array $data = null): Model\Persona\Datos { $map = (new Implement\Repository\MapperParser()) ->register('persona_rut', (new Implement\Repository\Mapper()) ->setProperty('persona') ->setFunction(function($data) { return $this->personaRepository->fetchById($data['persona_rut']); })) ->register('direccion_id', (new Implement\Repository\Mapper()) ->setProperty('direccion') ->setFunction(function($data) { if ($data['direccion_id'] === null) { return null; } return $this->direccionRepository->fetchById($data['direccion_id']); })->setDefault(null)) ->register('telefono', (new Implement\Repository\Mapper())->setFunction(function($data) { return $data['telefono']; })->setDefault(null)) ->register('email', (new Implement\Repository\Mapper())->setFunction(function($data) { return $data['email']; })->setDefault(null)) ->register('fecha_nacimiento', (new Implement\Repository\Mapper\DateTime('fecha_nacimiento')) ->setDefault(null) ->setProperty('fechaNacimiento')) ->register('sexo', (new Implement\Repository\Mapper())->setFunction(function($data) { return $data['sexo']; })->setDefault(null)) ->register('estado_civil', (new Implement\Repository\Mapper())->setFunction(function($data) { return $data['estado_civil']; })->setDefault(null)->setProperty('estadoCivil')) ->register('nacionalidad', (new Implement\Repository\Mapper())->setFunction(function($data) { return $data['nacionalidad']; })->setDefault(null)) ->register('ocupacion', (new Implement\Repository\Mapper())->setFunction(function($data) { return $data['ocupacion']; })->setDefault(null)); return $this->parseData(new Model\Persona\Datos(), $data, $map); } public function save(Define\Model $model): Model\Persona\Datos { $model->id = $this->saveNew([ 'persona_rut', 'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil', 'nacionalidad', 'ocupacion' ], [ $model->persona->rut, $model->direccion?->id, $model->telefono, $model->email, $model->fechaNacimiento, $model->sexo, $model->estadoCivil, $model->nacionalidad, $model->ocupacion ]); return $model; } public function edit(Define\Model $model, array $new_data): Model\Persona\Datos { return $this->update($model, [ 'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil', 'nacionalidad', 'ocupacion' ], $new_data); } /** * @param int $persona_rut * @return Model\Persona\Datos * @throws Implement\Exception\EmptyResult */ public function fetchByPersona(int $persona_rut): Model\Persona\Datos { $query = $this->connection->getQueryBuilder() ->select() ->from($this->getTable()) ->where('persona_rut = ?'); return $this->fetchOne($query, [$persona_rut]); } /** * @param array $data * @return void * @throws PDOException */ public function saveMissing(array $data): void { $dataQuery = array_fill(0, count($data), ['?', '?', '?', '?', '?', '?', '?', '?', '?']); $query = $this->connection->getQueryBuilder() ->insert() ->into($this->getTable()) ->columns([ 'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil', 'nacionalidad', 'ocupacion' ]) ->values($dataQuery); $flattened = []; foreach ($data as $col => $value) { $row = [ $value['direccion_id'] ?? null, $value['telefono'] ?? null, $value['email'] ?? null, $value['fecha_nacimiento'] ?? null, $value['sexo'] ?? null, $value['estado_civil'] ?? null, $value['nacionalidad'] ?? null, $value['ocupacion'] ?? null ]; $flattened = array_merge($flattened, $row); } $this->connection->execute($query, $flattened); } public function filterData(array $data): array { $fields = ['persona_rut', 'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil', 'nacionalidad', 'ocupacion']; return array_intersect_key($data, array_flip($fields)); } }