Passed
Branch tests1.5 (0c23dc)
by Wanderson
01:17
created

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