<?php
namespace Bitrix\Main\IO;

abstract class FileSystemEntry
{
	protected $path = null;
	protected $originalPath = null;
	protected $documentRoot = null;
	protected $pathPhysical = null;

	public function __construct($path)
	{
		if (empty($path))
			throw new InvalidPathException($path);

		$this->originalPath = $path;
		$this->path = Path::Normalize($path);
		$this->documentRoot = $_SERVER["DOCUMENT_ROOT"];

		if (empty($this->path))
			throw new InvalidPathException($path);
	}

	public function GetName()
	{
		return Path::GetName($this->path);
	}

	public function GetDirectoryName()
	{
		return Path::GetDirectory($this->path);
	}

	public function GetPath()
	{
		return $this->path;
	}

	public function GetDirectory()
	{
		return new Directory($this->GetDirectoryName());
	}

	abstract public function GetCreationTime();
	abstract public function GetLastAccessTime();
	abstract public function GetModificationTime();

	abstract public function IsExists();

	public abstract function IsDirectory();
	public abstract function IsFile();
	public abstract function IsLink();

	public abstract function MarkWritable();
	public abstract function GetPermissions();
	public abstract function Delete();

	protected function getPhysicalPath()
	{
		if (is_null($this->pathPhysical))
			$this->pathPhysical = Path::ConvertLogicalToPhysical($this->path);

		return $this->pathPhysical;
	}

	public function rename($newPath)
	{
		$newPathNormalized = Path::Normalize($newPath);

		$success = true;
		if ($this->IsExists())
			$success = rename($this->getPhysicalPath(), Path::ConvertLogicalToPhysical($newPathNormalized));

		if ($success)
		{
			$this->originalPath = $newPath;
			$this->path = $newPathNormalized;
			$this->pathPhysical = null;
		}

		return $success;
	}
}
