69 lines
1.4 KiB
PHP
69 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace core;
|
|
|
|
/**
|
|
* Generic class to specify string type
|
|
*/
|
|
class Path
|
|
{
|
|
/**
|
|
* Path value
|
|
*
|
|
* @var \string
|
|
*/
|
|
public \array $parts;
|
|
|
|
/**
|
|
* Constructor of Path
|
|
*
|
|
* @param \string $value Path string
|
|
*/
|
|
public function __construct(string $value) : void
|
|
{
|
|
$parts = \mb_split(\DIRECTORY_SEPARATOR, $value);
|
|
if ($parts === False)
|
|
{
|
|
\throw \core\PathException(_('cannot extract path components for the given value'), 1);
|
|
}
|
|
$this->parts = $parts;
|
|
}
|
|
|
|
/**
|
|
* Append a child path to this parent path without caring that the child is inside the parent
|
|
*
|
|
* @param \core\Path | \string $path Child path
|
|
*/
|
|
public function append(\core\Path | string $path) : self
|
|
{
|
|
if (\is_string($path))
|
|
{
|
|
$path = \core\Path($path);
|
|
}
|
|
$this->parts = \array_merge($this->parts, $path->parts);
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Append a child path to this parent path, checking if the result path is inside the parent path as it should
|
|
*
|
|
* @param \core\Path | \string $path Child path
|
|
*/
|
|
public function safe_append(\core\Path | string $path) : self
|
|
{
|
|
$old_realpath = \realpath($this->__tostring());
|
|
$new_realpath = \realpath($this->append($path)->__tostring());
|
|
if (\strpos($new_realpath, $old_realpath) !== 0)
|
|
{
|
|
$this = \core\Path($old_realpath);
|
|
}
|
|
return $this;
|
|
}
|
|
|
|
public function __toString() : string
|
|
{
|
|
return \DIRECTORY_SEPARATOR . \implode(\DIRECTORY_SEPARATOR, $this->parts);
|
|
}
|
|
}
|
|
|
|
?>
|