Skip to content

Commit

Permalink
1.36.0
Browse files Browse the repository at this point in the history
* [!] Path is ALWAYS immutable
* [+] added __toString(), isPresent(), isAccessible($access_rights = 0777) methods
  • Loading branch information
KarelWintersky committed Jun 10, 2021
1 parent 48f5822 commit cc14ea3
Showing 1 changed file with 48 additions and 24 deletions.
72 changes: 48 additions & 24 deletions sources/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,54 @@

namespace SteamBoat;

/**
* Class Path
*
* Data ALWAYS immutable
*
* @package SteamBoat
*/
class Path
{
public $atoms = [];

public $isImmutable = false;

public $isTrailingSlash = true;

public $isAbsolutePath = true;

/**
* Mutable create
* Create (immutable)
*
* @param $path
* @return Path
*/
public static function create($path)
{
return new self($path, false);
return new self($path);
}

/**
* Immutable create
* Create (alias)
*
* @param $path
* @return Path
*/
public static function createImmutable($path)
{
return new self($path, true);
return new self($path);
}

/**
* Path constructor
*
* @param $path
* @param bool $isImmutable
* @param bool $isTrailingSlash
* @param bool $isAbsolutePath
*/
public function __construct($path, $isImmutable = false, $isTrailingSlash = true)
public function __construct($path, $isTrailingSlash = true, $isAbsolutePath = true)
{
$this->isImmutable = $isImmutable;
$this->isTrailingSlash = $isTrailingSlash;
$this->isAbsolutePath = $isAbsolutePath;

if (is_string($path)) {
$path = trim($path, DIRECTORY_SEPARATOR);
Expand All @@ -69,6 +74,16 @@ public function toString()
($this->isTrailingSlash ? DIRECTORY_SEPARATOR : '');
}

/**
* Magic __toString method
*
* @return string
*/
public function __toString()
{
return $this->toString();
}

/**
* @param $data
* @return $this|Path
Expand All @@ -82,13 +97,7 @@ public function join($data)
}
$data = $this->trimEach($data);

if ($this->isImmutable) {
return new self(array_merge($this->atoms, $data), $this->isImmutable, $this->isTrailingSlash);
}

$this->atoms = array_merge($this->atoms, $data);

return $this;
return new self(array_merge($this->atoms, $data), $this->isTrailingSlash, $this->isAbsolutePath);
}

/**
Expand All @@ -100,14 +109,29 @@ public function joinName($data)
$this->isTrailingSlash = false;

$data = $this->trimEach($data);

if ($this->isImmutable) {
return new self(array_merge($this->atoms, [ $data ]), $this->isImmutable, $this->isTrailingSlash);
}

$this->atoms = array_merge($this->atoms, [ $data ] );

return $this;

return new self(array_merge($this->atoms, [ $data ]), $this->isTrailingSlash, $this->isAbsolutePath);
}

/**
*
* @return bool
*/
public function isPresent():bool
{
return is_dir($this->toString());
}

/**
*
* @param int $access_rights
* @return bool
*/
public function isAccessible($access_rights = 0777):bool
{
$path = $this->toString();

return is_dir( $path ) || ( mkdir( $path, 0777, true ) && is_dir( $path ) );
}

/**
Expand Down

0 comments on commit cc14ea3

Please sign in to comment.