Passed
Branch tests1.5 (2e49f7)
by Wanderson
01:30
created

File::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\File;
4
5
use Exception;
6
7
/**
8
 * Arquivos
9
 *
10
 */
11
class File extends DirectoryItem implements DirectoryItemInterface {
12
13
	/** @var string[] */
14
	public static $validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'csv', 'doc', 'docx', 'odt', 'pdf', 'txt', 'md', 'mp3', 'wav', 'mpeg'];
15
16
	/**
17
	 * Instância um novo arquivo
18
	 * @param string $path Caminho relativo
19
	 */
20
	public function __construct($path) {
21
		$this->setPath($path);
22
	}
23
24
	/** @return string */
25
	public function __toString() {
26
		return $this->getName() . $this->getExtensionDot();
27
	}
28
29
	/** @return string */
30
	public function getName() {
31
		return pathinfo($this->getAbsolutePath(), PATHINFO_FILENAME);
32
	}
33
34
	/** @return string */
35
	public function getExtension() {
36
		return pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION);
37
	}
38
39
	/** @return string */
40
	public function getExtensionDot() {
41
		return $this->getExtension() ? '.' . $this->getExtension() : '';
42
	}
43
44
	/** @return string */
45
	public function getType() {
46
		return mime_content_type($this->getAbsolutePath());
47
	}
48
49
	/** @return int|false */
50
	public function getSize() {
51
		$size = false;
52
		if ($this->exists()) {
53
			$size = filesize($this->getAbsolutePath());
54
		}
55
		return $size;
56
	}
57
58
	/** @return boolean */
59
	public function exists() {
60
		return is_file($this->getAbsolutePath());
61
	}
62
63
	/**
64
	 * @param string $path Caminho relativo
65
	 * @throws Exception
66
	 */
67
	protected function setPath($path) {
68
		if (!preg_match(static::REGEXP_PATH, $path)) {
69
			throw new Exception($path . ' is a invalid directory path.');
70
		}
71
		parent::setPath($path);
72
	}
73
74
	/**
75
	 * Exclui o arquivo
76
	 * @return boolean
77
	 */
78
	public function delete() {
79
		return unlink($this->getAbsolutePath());
80
	}
81
82
	/**
83
	 * Salva o conteúdo no arquivo
84
	 * @param string $content
85
	 * @param string $mode
86
	 * @return boolean
87
	 */
88
	public function write($content, $mode = 'w') {
89
		$return = false;
90
		if (strlen($this->getName()) > 0) {
91
			$this->getDirectory()->create();
92
			$fp = fopen($this->getAbsolutePath(), $mode);
93
			if ($fp !== false) {
94
				fwrite($fp, $content);
95
				$return = fclose($fp);
96
			}
97
		}
98
		return $return;
99
	}
100
101
	/**
102
	 * Retorna o conteúdo do arquivo
103
	 * @param string|false $content
104
	 */
105
	public function read() {
106
		$content = false;
107
		if ($this->exists()) {
108
			$content = file_get_contents($this->getAbsolutePath());
109
		}
110
		return $content;
111
	}
112
113
	/**
114
	 * Renomeia o arquivo
115
	 * @param string $newName [optional]
116
	 * @param string $newExtension [optional]
117
	 * @return boolean
118
	 */
119
	public function rename($newName = null, $newExtension = null) {
120
		$newName .= ($newExtension) ? '.' . $newExtension : $this->getExtensionDot();
121
		if (!preg_match(static::REGEXP_NAME, $newName)) {
122
			throw new Exception($newName . ' is a invalid file name.');
123
		}
124
		return parent::rename($newName);
125
	}
126
127
}
128