/src/Izzle/IO/FileInfo.php

https://gitlab.com/Nenasith/IO · PHP · 373 lines · 186 code · 49 blank · 138 comment · 18 complexity · bc207fd922c6e06b8202526beb1788ef MD5 · raw file

  1. <?php
  2. namespace Izzle\IO;
  3. use Izzle\IO\Exception\FileNotFoundException;
  4. use Izzle\IO\Exception\ArgumentNullException;
  5. class FileInfo
  6. {
  7. protected $fullName;
  8. protected $name;
  9. protected $extension;
  10. protected $length;
  11. protected $changed;
  12. protected $accessed;
  13. protected $content;
  14. protected $directory;
  15. protected $exists;
  16. protected $isReadOnly;
  17. /**
  18. * @param $path
  19. * @param bool|true $directory
  20. * @throws ArgumentNullException
  21. */
  22. public function __construct($path, $directory = true)
  23. {
  24. if ($path === null) {
  25. throw new ArgumentNullException('path is null');
  26. }
  27. if (preg_match('/[\,\<\>\|]/', $path)) {
  28. throw new \InvalidArgumentException('invalid path characters');
  29. }
  30. $this->setFullName($path)
  31. ->setName(pathinfo($this->fullName, PATHINFO_FILENAME))
  32. ->setExists(file_exists($this->fullName))
  33. ->setExtension(pathinfo($this->fullName, PATHINFO_EXTENSION))
  34. ->setLength(0)
  35. ->setIsReadOnly(false);
  36. if ($directory) {
  37. $this->setDirectory(new DirectoryInfo(pathinfo($this->fullName, PATHINFO_DIRNAME), false));
  38. }
  39. $this->getInfos();
  40. }
  41. /**
  42. * @return bool
  43. */
  44. public function create()
  45. {
  46. if (!$this->exists) {
  47. if ($this->exists = touch($this->fullName)) {
  48. $this->getInfos();
  49. }
  50. return $this->exists;
  51. }
  52. return true;
  53. }
  54. /**
  55. * @return bool
  56. * @throws FileNotFoundException
  57. */
  58. public function delete()
  59. {
  60. if ($this->exists) {
  61. return unlink($this->fullName);
  62. } else {
  63. throw new FileNotFoundException("file '{$this->fullName}' not found");
  64. }
  65. }
  66. /**
  67. * @param $name
  68. * @return bool
  69. * @throws FileNotFoundException
  70. */
  71. public function rename($name)
  72. {
  73. if ($this->exists) {
  74. $newPath = Path::combine($this->getDirectory()->getFullName(), $name);
  75. if (rename($this->fullName, $newPath)) {
  76. $this->name = $name;
  77. $this->fullName = $newPath;
  78. return true;
  79. }
  80. return false;
  81. } else {
  82. throw new FileNotFoundException("file '{$this->fullName}' not found");
  83. }
  84. }
  85. /**
  86. * @param $name
  87. * @return bool
  88. * @throws FileNotFoundException
  89. */
  90. public function move($name)
  91. {
  92. if ($this->exists) {
  93. if (rename($this->fullName, $name)) {
  94. $this->name = basename($name);
  95. $this->fullName = $name;
  96. return true;
  97. }
  98. return false;
  99. } else {
  100. throw new FileNotFoundException("file '{$this->fullName}' not found");
  101. }
  102. }
  103. protected function getInfos()
  104. {
  105. if ($this->exists) {
  106. $this->setFullName($this->fullName)
  107. ->setIsReadOnly(!is_readable($this->fullName));
  108. $fileStats = stat($this->fullName);
  109. $this->setLength($fileStats[7]);
  110. $this->changed = new \DateTime('@' . $fileStats[9]);
  111. $this->accessed = new \DateTime('@' . $fileStats[8]);
  112. }
  113. }
  114. /**
  115. * Gets the filename.
  116. *
  117. * @return string
  118. */
  119. public function getFilename()
  120. {
  121. return sprintf('%s.%s', $this->name, $this->extension);
  122. }
  123. /**
  124. * Gets the value of fullName.
  125. *
  126. * @return string
  127. */
  128. public function getFullName()
  129. {
  130. return $this->fullName;
  131. }
  132. /**
  133. * Sets the value of fullName.
  134. *
  135. * @param string $fullName the full name
  136. *
  137. * @return self
  138. */
  139. public function setFullName($fullName)
  140. {
  141. $this->fullName = $fullName;
  142. return $this;
  143. }
  144. /**
  145. * Gets the value of name.
  146. *
  147. * @return string
  148. */
  149. public function getName()
  150. {
  151. return $this->name;
  152. }
  153. /**
  154. * Sets the value of name.
  155. *
  156. * @param string $name the name
  157. *
  158. * @return self
  159. */
  160. public function setName($name)
  161. {
  162. $this->name = $name;
  163. return $this;
  164. }
  165. /**
  166. * Gets the value of extension.
  167. *
  168. * @return string
  169. */
  170. public function getExtension()
  171. {
  172. return $this->extension;
  173. }
  174. /**
  175. * Sets the value of extension.
  176. *
  177. * @param string $extension the extension
  178. *
  179. * @return self
  180. */
  181. public function setExtension($extension)
  182. {
  183. $this->extension = $extension;
  184. return $this;
  185. }
  186. /**
  187. * Gets the value of length.
  188. *
  189. * @return int
  190. */
  191. public function getLength()
  192. {
  193. return $this->length;
  194. }
  195. /**
  196. * Sets the value of length.
  197. *
  198. * @param int $length the length
  199. *
  200. * @return self
  201. */
  202. public function setLength($length)
  203. {
  204. $this->length = (int) $length;
  205. return $this;
  206. }
  207. /**
  208. * Gets the value of changed.
  209. *
  210. * @return \DateTime
  211. */
  212. public function getChanged()
  213. {
  214. return $this->changed;
  215. }
  216. /**
  217. * Gets the value of accessed.
  218. *
  219. * @return \DateTime
  220. */
  221. public function getAccessed()
  222. {
  223. return $this->accessed;
  224. }
  225. /**
  226. * Gets the value of content.
  227. *
  228. * @return string
  229. */
  230. public function getContent()
  231. {
  232. if ($this->exists && $this->content === null) {
  233. $this->content = file_get_contents($this->fullName);
  234. }
  235. return $this->content;
  236. }
  237. /**
  238. * Sets the value of content.
  239. *
  240. * @param string $content the content
  241. *
  242. * @return self
  243. */
  244. public function setContent($content)
  245. {
  246. $this->content = $content;
  247. return $this;
  248. }
  249. /**
  250. * Writes content data to file
  251. *
  252. * @return bool / int
  253. */
  254. public function write()
  255. {
  256. if ($this->exists && $this->content !== null) {
  257. return file_put_contents($this->fullName, $this->content);
  258. }
  259. return false;
  260. }
  261. /**
  262. * Gets the value of directory.
  263. *
  264. * @return DirectoryInfo
  265. */
  266. public function getDirectory()
  267. {
  268. return $this->directory;
  269. }
  270. /**
  271. * Sets the value of directory.
  272. *
  273. * @param DirectoryInfo $directory the directory
  274. *
  275. * @return self
  276. */
  277. public function setDirectory(DirectoryInfo $directory)
  278. {
  279. $this->directory = $directory;
  280. return $this;
  281. }
  282. /**
  283. * Gets the value of exists.
  284. *
  285. * @return bool
  286. */
  287. public function getExists()
  288. {
  289. return $this->exists;
  290. }
  291. /**
  292. * Sets the value of exists.
  293. *
  294. * @param bool $exists the exists
  295. *
  296. * @return self
  297. */
  298. public function setExists($exists)
  299. {
  300. $this->exists = (bool) $exists;
  301. return $this;
  302. }
  303. /**
  304. * Gets the value of isReadOnly.
  305. *
  306. * @return bool
  307. */
  308. public function getIsReadOnly()
  309. {
  310. return $this->isReadOnly;
  311. }
  312. /**
  313. * Sets the value of isReadOnly.
  314. *
  315. * @param bool $isReadOnly the is read only
  316. *
  317. * @return self
  318. */
  319. public function setIsReadOnly($isReadOnly)
  320. {
  321. $this->isReadOnly = (bool)$isReadOnly;
  322. return $this;
  323. }
  324. }