<?php
namespace Incoviba\Repository;

use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;

abstract class Tipo extends Ideal\Repository
{
    abstract protected function getBlank(): Define\Model;
    public function create(?array $data = null): Define\Model
    {
        $map = new Implement\Repository\MapperParser(['descripcion']);
        return $this->parseData($this->getBlank(), $data, $map);
    }
    public function save(Define\Model $model): Define\Model
    {
        $model->id = $this->saveNew(
            ['descripcion'],
            [$model->descripcion]
        );
        return $model;
    }
    public function edit(Define\Model $model, array $new_data): Define\Model
    {
        return $this->update($model, ['descripcion'], $new_data);
    }

    public function fetchByDescripcion(string $descripcion): Define\Model
    {
        $query = $this->connection->getQueryBuilder()
            ->select()
            ->from($this->getTable())
            ->where('descripcion LIKE ?');
        return $this->fetchOne($query, [$descripcion]);
    }
}
