<?php
namespace Incoviba\Model\Inmobiliaria;

use Incoviba\Common\Ideal;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Model;
use Incoviba\Model\Inmobiliaria\Proveedor\Datos;

class Proveedor extends Ideal\Model
{
    public int $rut;
    public string $digito;
    public string $nombre;
    public ?string $razon;
    public ?Model\Persona $contacto;

    public ?Datos $datos;
    public function datos(): ?Datos
    {
        if (!isset($this->datos)) {
            $this->datos = $this->runFactory('datos');
        }
        return $this->datos;
    }

    public function jsonSerialize(): mixed
    {
        $json = [
            'rut' => $this->rut,
            'digito' => $this->digito,
            'nombre' => $this->nombre,
            'razon' => $this->razon,
            'datos' => null,
            'contacto' => $this->contacto
        ];
        try {
            $json['datos'] = $this->datos();
        } catch (EmptyResult) {}
        return $json;
    }
}
