Passed
Branch master (989b89)
by Wanderson
01:21
created

File::validateUpload()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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