<?php

namespace PPOM\Attach;

/**
 * Attach UI list item binding an ID to a {@see ContainerView} renderer.
 */
class ContainerItem {

	/**
	 * The ID of the component.
	 *
	 * @var string
	 */
	public $id = '';

	/**
	 * The class that render the component HTML.
	 *
	 * @var ContainerView
	 */
	public $renderer;

	/**
	 * Binds a container id to its view renderer.
	 *
	 * @param string        $id       Stable DOM / container id.
	 * @param ContainerView $renderer View responsible for `render()`.
	 */
	public function __construct( $id, $renderer ) {
		$this->id       = $id;
		$this->renderer = $renderer;
	}

	/**
	 * Get the used renderer.
	 *
	 * @return ContainerView
	 */
	public function get_renderer() {
		return $this->renderer;
	}

	/**
	 * Set the render.
	 *
	 * @param ContainerView $renderer Object implementing `render()`.
	 *
	 * @return bool If the operation was successful.
	 */
	public function set_renderer( $renderer ) {
		if ( ! method_exists( $renderer, 'render' ) ) {
			return false;
		}

		$this->renderer = $renderer;
		return true;
	}

	/**
	 * Gets the ID of the container item.
	 *
	 * @return string The ID of the container item.
	 */
	public function get_id(): string {
		return $this->id;
	}

	/**
	 * Sets the ID of the container item.
	 *
	 * @param string $id The new ID of the container item.
	 * @return void
	 */
	public function set_id( string $id ): void {
		$this->id = $id;
	}
}
