Passed
Branch tests1.5 (70b00b)
by Wanderson
01:40
created

File::getExtensionByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\File;
4
5
use Exception;
6
use const BASE_PATH;
7
8
/**
9
 * Arquivos
10
 *
11
 */
12
class File {
13
14
	/** @var Directory */
15
	private $directory;
16
17
	/** @var string */
18
	private $name;
19
20
	/** @var string */
21
	private $extension;
22
23
	/** @var string[] */
24
	public static $validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'csv', 'doc', 'docx', 'odt', 'pdf', 'txt', 'md', 'mp3', 'wav', 'mpeg'];
25
26
	const VALIDATE_PATH = '@^(([a-z0-9._\-][\/]?))+$@';
27
	const VALIDATE_NAME = '@^(([a-z0-9._\-]?))+$@';
28
29
	/**
30
	 * Instância um novo arquivo
31
	 * @param string $path
32
	 */
33
	public function __construct($path) {
34
		if (!preg_match(static::VALIDATE_PATH, $path)) {
35
			throw new Exception($path . ' is a invalid file path.');
36
		}
37
		$this->directory = new Directory(dirname($path));
38
		$this->extension = pathinfo($path, PATHINFO_EXTENSION);
39
		$this->name = basename(BASE_PATH . DIRECTORY_SEPARATOR . $path, '.' . $this->extension);
40
	}
41
42
	/** @return string */
43
	public function __toString() {
44
		return $this->toString();
45
	}
46
47
	/** @return string */
48
	public function toString() {
49
		if ($this->extension) {
50
			return $this->name . '.' . $this->extension;
51
		}
52
		return $this->name;
53
	}
54
55
	/** @return string */
56
	public function getName() {
57
		return $this->name;
58
	}
59
60
	/** @return string */
61
	public function getExtension() {
62
		return $this->extension;
63
	}
64
65
	/** @return int|false */
66
	public function getSize() {
67
		if ($this->exists()) {
68
			$size = filesize($this->getPath());
69
		} else {
70
			$size = false;
71
		}
72
		return $size;
73
	}
74
75
	/** @return Directory */
76
	public function getDirectory() {
77
		return $this->directory;
78
	}
79
80
	/** @return string */
81
	public function getPath() {
82
		return (BASE_PATH . DIRECTORY_SEPARATOR . $this->getDirectory()->getRelativePath() .
83
				DIRECTORY_SEPARATOR . $this->toString());
84
	}
85
86
	/** @return string */
87
	public function getRelativePath() {
88
		return $this->getDirectory()->getRelativePath() . DIRECTORY_SEPARATOR . $this->toString();
89
	}
90
91
	/** @return boolean */
92
	public function exists() {
93
		return is_file($this->getPath());
94
	}
95
96
	/**
97
	 * Exclui o arquivo
98
	 * @return boolean
99
	 */
100
	public function delete() {
101
		return unlink($this->getPath());
102
	}
103
104
	/**
105
	 * Salva o conteúdo no arquivo
106
	 * @param string $content
107
	 * @param string $mode
108
	 * @return boolean
109
	 */
110
	public function write($content, $mode = 'a') {
111
		$return = false;
112
		if (strlen($this->getName()) > 0) {
113
			$this->directory->create();
114
			$fp = fopen($this->getPath(), $mode);
115
			if ($fp !== false) {
116
				fwrite($fp, $content);
117
				$return = fclose($fp);
118
			}
119
		}
120
		return $return;
121
	}
122
123
	/**
124
	 * Retorna o conteúdo do arquivo
125
	 * @param string|false $content
126
	 */
127
	public function read() {
128
		$content = false;
129
		if ($this->exists()) {
130
			$content = file_get_contents($this->getPath());
131
		}
132
		return $content;
133
	}
134
135
	/**
136
	 * Move o arquivo para um novo diretório
137
	 * @param Directory $newDirectory
138
	 * @return boolean
139
	 */
140
	public function move(Directory $newDirectory) {
141
		$oldPath = $this->getPath();
142
		$this->directory = $newDirectory;
143
		return rename($oldPath, $this->getPath());
144
	}
145
146
	/**
147
	 * Renomeia o arquivo
148
	 * @param string $newName
149
	 * @return boolean
150
	 */
151
	public function rename($newName) {
152
		if (!preg_match(static::VALIDATE_NAME, $newName)) {
153
			throw new Exception($newName . ' is a invalid file name.');
154
		}
155
		$oldPath = $this->getPath();
156
		$this->name = $newName;
157
		return rename($oldPath, $this->getPath());
158
	}
159
160
	/**
161
	 * Converte uma string para um nome de arquivo válido
162
	 * @param string $string
163
	 * @return string
164
	 */
165
	public static function strToFileName($string) {
166
		return trim(strToURL($string), '-');
167
	}
168
169
}
170