<?php
/**
 * Created by PhpStorm.
 * User: jirislav
 * Date: 2.5.17
 * Time: 17:15
 */

namespace App\Builders;

/**
 * Class EntityDefinitionBuilder
 * @package App\Builders
 */
class EntityDefinitionBuilder
{
    protected $id;
    protected $name;
    protected $isType = false;
    protected $requireParent = false;
    protected $entityTypeId = null;
    protected $hasManufacturer = false;
    protected $hasSupplier = false;
    protected $hasLocation = false;

    protected $parameters = [];
    protected $parentClassIds = [];

    /**
     * EntityDefinitionBuilder constructor.
     * @param $id
     * @param $name
     * @throws \Exception
     */
    public function __construct($id, $name)
    {
        if (empty($id) || empty($name))
            throw new \Exception('EntityDefinitionBuilder cannot be called with empty id nor empty name!');

        $this->id = $id;
        $this->name = $name;
    }

    /**
     * @param bool $isType
     * @return $this
     */
    public function isType($isType = true)
    {
        $this->isType = $isType;
        return $this;
    }

    /**
     * @param bool $requireParent
     * @return $this
     */
    public function requireParent($requireParent = true)
    {
        $this->requireParent = $requireParent;
        return $this;
    }

    /**
     * @param null $entityTypeId
     * @return $this
     */
    public function entityTypeId($entityTypeId = null)
    {
        $this->entityTypeId = $entityTypeId;
        return $this;
    }

    /**
     * @param bool $hasManufacturer
     * @return $this
     */
    public function hasManufacturer($hasManufacturer = true)
    {
        $this->hasManufacturer = $hasManufacturer;
        return $this;
    }

    /**
     * @param bool $hasSupplier
     * @return $this
     */
    public function hasSupplier($hasSupplier = true)
    {
        $this->hasSupplier = $hasSupplier;
        return $this;
    }

    /**
     * @param bool $hasLocation
     * @return $this
     */
    public function hasLocation($hasLocation = true)
    {
        $this->hasLocation = $hasLocation;
        return $this;
    }

    /**
     * @param array $parameters
     * @return $this
     */
    public function parameters($parameters = [])
    {
        $this->parameters = $parameters;
        return $this;
    }

    /**
     * @param array $parentClassIds
     * @param bool $required
     * @return $this
     */
    public function parentClassIds($parentClassIds = [], $required = null)
    {
        $this->parentClassIds = $parentClassIds;

        if ($required !== null)
            $this->requireParent = $required;

        return $this;
    }

    /**
     * @return array
     */
    public function finish()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'is_type' => $this->isType,
            'require_parent' => $this->requireParent,
            'entity_type_class_id' => $this->entityTypeId,
            'has_manufacturer' => $this->hasManufacturer,
            'has_supplier' => $this->hasSupplier,
            'has_location' => $this->hasLocation,
            'parameters' => $this->parameters,
            'parent_class_ids' => $this->parentClassIds
        ];
    }
}