123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace fphammerle\helpers;
- class Path
- {
- use PropertyAccessTrait;
- /**
- * @var Path|null
- */
- protected $_dir_path;
- /**
- * @var string|null
- */
- protected $_filename;
- /**
- * @var string|null
- */
- protected $_extension;
- /**
- * @param string|null $path
- */
- public function __construct($path = null)
- {
- $this->setPath($path);
- }
- /**
- * @return string
- */
- public function __toString()
- {
- return $this->path ?: '';
- }
- /**
- * @return string|null
- */
- public function getBasename()
- {
- return StringHelper::unite([
- $this->_filename,
- StringHelper::prepend('.', $this->_extension),
- ]);
- }
- /**
- * @return string|null
- */
- public function setBasename($basename)
- {
- $this->setExtension(pathinfo($basename, PATHINFO_EXTENSION));
- if(isset($this->_extension)) {
- $this->setFilename(substr(
- $basename,
- 0,
- strlen($basename) - strlen($this->_extension) - 1
- ));
- } else {
- $this->setFilename($basename);
- }
- }
- /**
- * @return Path|null
- */
- public function getDirPath()
- {
- return $this->_dir_path;
- }
- /**
- * @var Path|string|null $path
- */
- public function setDirPath($path)
- {
- if($path instanceof Path) {
- $this->_dir_path = $path;
- } elseif($path === null) {
- $this->_dir_path = null;
- } else {
- $this->_dir_path = new self($path);
- }
- }
- /**
- * @return string|null
- */
- public function getExtension()
- {
- return $this->_extension;
- }
- /**
- * @param string|null $extension
- */
- public function setExtension($extension)
- {
- $this->_extension = ((string)$extension) ?: null;
- }
- /**
- * @return string|null
- */
- public function getFilename()
- {
- return $this->_filename;
- }
- /**
- * @param string|null $filename
- */
- public function setFilename($filename)
- {
- $this->_filename = ((string)$filename) ?: null;
- }
- /**
- * @return string|null
- */
- public function getPath()
- {
- if($this->isRoot()) {
- return '/';
- } elseif(!isset($this->_dir_path)) {
- return $this->basename;
- } else {
- $dir_path = $this->_dir_path->path;
- if($dir_path == '/') {
- return '/' . $this->basename;
- } else {
- return $dir_path . '/' . $this->basename;
- }
- }
- }
- /**
- * @return string|null
- */
- public function setPath($path)
- {
- $basename = basename($path);
- $this->setBasename($basename);
- if(strlen($basename) < strlen($path)) {
- $dirname = dirname($path);
- if($dirname == $path) { // root?
- $this->setDirPath($this);
- } else {
- $this->setDirPath(dirname($path));
- }
- } else {
- $this->setDirPath(null);
- }
- }
- /**
- * @return bool
- */
- public function isRoot()
- {
- return $this->_dir_path === $this;
- }
- }
|