<?php
	namespace Mocha\Core;

	class InstanceKey
	{
		public $ClassIndex;
		public $InstanceIndex;
		
		public function __construct(int $class_id, int $inst_id)
		{
			$this->ClassIndex = $class_id;
			$this->InstanceIndex = $inst_id;
		}

		public static function Parse(?string $instanceKeyStr) : ?InstanceKey
		{
			if ($instanceKeyStr !== null)
			{
				$split = explode("$", $instanceKeyStr);
				if (count($split) == 2)
				{
					if (is_numeric($split[0]) && is_numeric($split[1]))
					{
						return new InstanceKey(intval($split[0]), intval($split[1]));
					}
				}
			}
			return null;
		}
		public static function TryParse(?string $instanceKeyStr, ?InstanceKey &$out) : bool
		{
			$ik = InstanceKey::Parse($instanceKeyStr);
			if ($ik !== null)
			{
				$out = $ik;
				return true;
			}
			return false;
		}

		public function __toString()
		{
			return $this->ClassIndex . "$" . $this->InstanceIndex;
		}
	}