<?php
declare(strict_types=1);

namespace ProudNerds\Block;

use Doctrine\Common\Collections\ArrayCollection;
use ProudNerds\Block\Block;
use Timber\Post;

final class Context
{
    /**
     * @var Block
     */
    private $block;

    /**
     * @var Post
     */
    private $post;

    /**
     * @var mixed[]
     */
    private $global;

    /**
     * @var ArrayCollection
     */
    private $variables;

    public function __construct(Block $block, Post $post, array $global)
    {
        $this->block = $block;
        $this->post = $post;
        $this->global = $global;
        $this->variables = new ArrayCollection();
    }

    public function getBlock(): Block
    {
        return $this->block;
    }

    public function getPost(): Post
    {
        return $this->post;
    }

    public function getGlobal(): array
    {
        return $this->global;
    }

    public function set(string $key, $variable): self
    {
        $this->variables->set($key, $variable);

        return $this;
    }

    public function remove(string $key): self
    {
        $this->variables->remove($key);

        return $this;
    }

    public function get(string $key)
    {
        return $this->variables->get($key);
    }

    /**
     * return mixed[]
     */
    public function toArray(): array
    {
        return [
            'block' => $this->variables->toArray(),
            'post' => $this->post,
            'global' => $this->global,
        ];
    }
}
