1
0

Path.php 3.2 KB

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