Updated FileKey with setters

This commit is contained in:
Chris 2018-04-16 00:41:13 +02:00
parent 31634c5054
commit d6aacd841f

View File

@ -17,18 +17,8 @@ class FileKey implements KeyInterface
*/ */
public function __construct(string $pathname, $proj="\0", bool $create = false) public function __construct(string $pathname, $proj="\0", bool $create = false)
{ {
if (!file_exists($pathname)) { $this->setPathname($pathname, $create);
if (!$create) { $this->setProjectIdentifier($proj);
throw new \RuntimeException("Path does not exist: {$pathname}");
}
if (!is_dir(dirname($pathname))) {
throw new \RuntimeException("The directory ".dirname($pathname)." does not exist");
}
touch($pathname);
}
$this->pathname = $pathname;
$this->proj = substr($proj,0,1);
} }
/** /**
@ -41,6 +31,20 @@ class FileKey implements KeyInterface
return $this->pathname; return $this->pathname;
} }
public function setPathname(string $pathname, bool $create = false)
{
if (!file_exists($pathname)) {
if (!$create) {
throw new \RuntimeException("Path does not exist: {$pathname}");
}
if (!is_dir(dirname($pathname))) {
throw new \RuntimeException("The directory ".dirname($pathname)." does not exist");
}
touch($pathname);
}
$this->pathname = $pathname;
}
/** /**
* Get the project identifier. Not guaranteed to be printable * Get the project identifier. Not guaranteed to be printable
* *
@ -51,6 +55,17 @@ class FileKey implements KeyInterface
return $this->proj; return $this->proj;
} }
/**
* Set the project identifier
*
* @param string $proj
* @return void
*/
public function setProjectIdentifier(string $proj)
{
$this->proj = substr($proj, 0, 1);
}
/** /**
* Get the key value * Get the key value
* *