Passed
Branch tests1.5 (81ba23)
by Wanderson
01:11
created

File::getExtensionDot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
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 {
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 getName() {
26
		return pathinfo($this->getAbsolutePath(), PATHINFO_FILENAME);
27
	}
28
29
	/** @return string */
30
	public function getExtension() {
31
		return pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION);
32
	}
33
34
	/** @return string */
35
	public function getExtensionDot() {
36
		return $this->getExtension() ? '.' . $this->getExtension() : '';
37
	}
38
39
	/** @return string */
40
	public function getType() {
41
		return mime_content_type($this->getAbsolutePath());
42
	}
43
44
	/** @return int|false */
45
	public function getSize() {
46
		if ($this->exists()) {
47
			$size = filesize($this->getAbsolutePath());
48
		} else {
49
			$size = false;
50
		}
51
		return $size;
52
	}
53
54
	/** @return string */
55
	public function toString() {
56
		return $this->getName() . $this->getExtensionDot();
57
	}
58
59
	/** @return boolean */
60
	public function exists() {
61
		return is_file($this->getAbsolutePath());
62
	}
63
64
	/**
65
	 * @param string $path Caminho relativo
66
	 * @throws Exception
67
	 */
68
	protected function setPath($path) {
69
		if (!preg_match(static::REGEXP_PATH, $path)) {
70
			throw new Exception($path . ' is a invalid directory path.');
71
		}
72
		parent::setPath($path);
73
	}
74
75
	/**
76
	 * Exclui o arquivo
77
	 * @return boolean
78
	 */
79
	public function delete() {
80
		return unlink($this->getAbsolutePath());
81
	}
82
83
	/**
84
	 * Salva o conteúdo no arquivo
85
	 * @param string $content
86
	 * @param string $mode
87
	 * @return boolean
88
	 */
89
	public function write($content, $mode = 'w') {
90
		$return = false;
91
		if (strlen($this->getName()) > 0) {
92
			$this->getDirectory()->create();
93
			$fp = fopen($this->getAbsolutePath(), $mode);
94
			if ($fp !== false) {
95
				fwrite($fp, $content);
96
				$return = fclose($fp);
97
			}
98
		}
99
		return $return;
100
	}
101
102
	/**
103
	 * Retorna o conteúdo do arquivo
104
	 * @param string|false $content
105
	 */
106
	public function read() {
107
		$content = false;
108
		if ($this->exists()) {
109
			$content = file_get_contents($this->getAbsolutePath());
110
		}
111
		return $content;
112
	}
113
114
	/**
115
	 * Renomeia o arquivo
116
	 * @param string $newName [optional]
117
	 * @param string $newExtension [optional]
118
	 * @return boolean
119
	 */
120
	public function rename($newName = null, $newExtension = null) {
121
		$newName .= ($newExtension) ? '.' . $newExtension : $this->getExtensionDot();
122
		if (!preg_match(static::REGEXP_NAME, $newName)) {
123
			throw new Exception($newName . ' is a invalid file name.');
124
		}
125
		return parent::rename($newName);
126
	}
127
128
}
129