Path.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace fphammerle\helpers;
  3. class Path
  4. {
  5. use PropertyAccessTrait;
  6. /**
  7. * @var Path|null
  8. */
  9. protected $_dir_path;
  10. /**
  11. * @var string|null
  12. */
  13. protected $_filename;
  14. /**
  15. * @var string|null
  16. */
  17. protected $_extension;
  18. public function __construct($path = null)
  19. {
  20. $this->setPath($path);
  21. }
  22. /**
  23. * @return string|null
  24. */
  25. public function getBasename()
  26. {
  27. return StringHelper::implode('', [
  28. $this->_filename,
  29. isset($this->_extension) ? ('.' . $this->_extension) : null,
  30. ]);
  31. }
  32. /**
  33. * @return string|null
  34. */
  35. public function setBasename($basename)
  36. {
  37. $this->setExtension(pathinfo($basename, PATHINFO_EXTENSION));
  38. if(isset($this->_extension)) {
  39. $this->setFilename(substr(
  40. $basename,
  41. 0,
  42. strlen($basename) - strlen($this->_extension) - 1
  43. ));
  44. } else {
  45. $this->setFilename($basename);
  46. }
  47. }
  48. /**
  49. * @return Path|null
  50. */
  51. public function getDirPath()
  52. {
  53. return $this->_dir_path;
  54. }
  55. /**
  56. * @var Path|string|null $path
  57. */
  58. public function setDirPath($path)
  59. {
  60. if($path instanceof Path) {
  61. $this->_dir_path = $path;
  62. } elseif($path === null) {
  63. $this->_dir_path = null;
  64. } else {
  65. $this->_dir_path = new self($path);
  66. }
  67. }
  68. /**
  69. * @return string|null
  70. */
  71. public function getExtension()
  72. {
  73. return $this->_extension;
  74. }
  75. /**
  76. * @param string|null $extension
  77. */
  78. public function setExtension($extension)
  79. {
  80. $this->_extension = ((string)$extension) ?: null;
  81. }
  82. /**
  83. * @return string|null
  84. */
  85. public function getFilename()
  86. {
  87. return $this->_filename;
  88. }
  89. /**
  90. * @param string|null $filename
  91. */
  92. public function setFilename($filename)
  93. {
  94. $this->_filename = ((string)$filename) ?: null;
  95. }
  96. /**
  97. * @return string|null
  98. */
  99. public function getPath()
  100. {
  101. if($this->isRoot()) {
  102. return '/';
  103. } elseif(!isset($this->_dir_path)) {
  104. return $this->basename;
  105. } else {
  106. $dir_path = $this->_dir_path->path;
  107. if($dir_path == '/') {
  108. return '/' . $this->basename;
  109. } else {
  110. return $dir_path . '/' . $this->basename;
  111. }
  112. }
  113. }
  114. /**
  115. * @return string|null
  116. */
  117. public function setPath($path)
  118. {
  119. $basename = basename($path);
  120. $this->setBasename($basename);
  121. if(strlen($basename) < strlen($path)) {
  122. $dirname = dirname($path);
  123. if($dirname == $path) { // root?
  124. $this->setDirPath($this);
  125. } else {
  126. $this->setDirPath(dirname($path));
  127. }
  128. } else {
  129. $this->setDirPath(null);
  130. }
  131. }
  132. /**
  133. * @return bool
  134. */
  135. public function isRoot()
  136. {
  137. return $this->_dir_path === $this;
  138. }
  139. }