<?php

namespace Engine\RemoteData\InfoBox;

abstract class FieldAbstract
{
	protected $label = null;

	protected $value = null;

	protected $style = null;

	protected $forceDisplay = null;

	/**
	 * This is just to clear new static (phpstan) issues
	 */
	final public function __construct()
	{ }

	/**
	 * Creates a new field (faster instantiation).
	 * @param string $label
	 * @param string $value
	 */
	static public function create($label=null, $value=null)
	{
		$field = new static();
		$field->setLabel($label);
		if ($value!==null) {
			$field->setValue($value);
		}
		return $field;
	}

	public function setLabel($label)
	{
		$this->label = $label;
		return $this;
	}

	public function setValue($value)
	{
		$this->value = $value;
		return $this;
	}

	public function setForceDisplay($forceDisplay=true)
	{
		$this->forceDisplay = $forceDisplay;
		return $this;
	}

	public function getForceDisplay()
	{
		return $this->forceDisplay;
	}

	public function setStyle($style='highlight')
	{
		$this->style = $style;
		return $this;
	}

	public function getStyle()
	{
		return $this->style;
	}

	protected function getFieldType()
	{
		return lcfirst( substr( stripClassNamespace(get_called_class()), 6) );
	}

	public function exportAsArray()
	{
		return [
			'label'=>$this->label,
			'value'=>$this->exportValue(),
			'type'=>$this->getFieldType(),
			'style'=>$this->getStyle(),
			'forceDisplay'=>$this->getForceDisplay(),
		];
	}

	public function exportAsStdObj()
	{
		$return = new \stdClass();
		$return->label = $this->label;
		$return->value = $this->exportValue();
		$return->type = $this->getFieldType();
		$return->style = $this->getStyle();
		$return->forceDisplay = $this->getForceDisplay();
		return $return;
	}

	protected function exportValue()
	{
		return $this->value;
	}

}