<?php

namespace Src\Modules\JsonObjects\Dto\Dir;

use Src\Modules\JsonObjects\Interfaces\Dto\Dir\IFactory;
use Src\Modules\JsonObjects\Interfaces\Dto\IFactory as IDtoFactory;
use Src\Modules\JsonObjects\Interfaces\Dto\Dir\IPersist;
use Src\Modules\JsonObjects\Interfaces\Dto\Dir\IResource;

class Factory implements IFactory
{

    protected IDtoFactory $dtoFactory;

    protected array $conf;

    public function init(array $conf = []): void
    {
        $this->conf[IPersist::class] = [
            'class' => Persist::class,
        ];
        $this->conf[IResource::class] = [
            'class' => Resource::class,
        ];
        array_replace($this->conf, $conf);
    }

    public function setDtoFactory(IDtoFactory $factory): void
    {
        $this->dtoFactory = $factory;
    }

    public function createPersist(): IPersist
    {
        $persist = new $this->conf[IPersist::class]['class'];
        $persist->setDtoFactory($this->dtoFactory);
        $persist->init();
        return $persist;
    }

    public function createResource(): IResource
    {
        $resource = new $this->conf[IResource::class]['class'];
        $resource->setDtoFactory($this->dtoFactory);
        return $resource;
    }
}
