<?php

namespace Cristal\ApiWrapper\Relations;

use Cristal\ApiWrapper\Model;

class BelongsTo extends Relation
{
    public function __construct(Model $parent, Model $related, protected $foreignKey, protected $localKey)
    {
        parent::__construct($parent);
        $this->parent = $parent;
        $this->related = $related;

        $this->addConstraints();
    }

    /**
     * Get the results of the relationship.
     *
     * @return mixed
     */
    public function getResults()
    {
        if (!($queryValue = $this->parent->{$this->foreignKey})) {
            return null;
        }

        return $this->builder->find($queryValue);
    }

    /**
     * Set the base constraints on the relation query.
     *
     * @return void
     */
    public function addConstraints()
    {
        $this->builder = $this->related->newQuery();
    }

    /**
     * Get the models corresponding to data passed by array.
     *
     * @param $data
     *
     * @return mixed
     */
    public function getRelationsFromArray($data)
    {
        $class = $this->related::class;

        return new $class($data, isset($data[$this->localKey]));
    }
}
