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

Directory::strToDirectoryName()   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 1
dl 0
loc 2
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
 * Diretório de Arquivos
10
 *
11
 */
12
class Directory {
13
14
	/** @var string */
15
	private $path;
16
17
	const VALIDATE_PATH = '@^(([a-z0-9._\-][\/]?))+$@';
18
19
	/**
20
	 * Instância um diretório
21
	 * @param string $path
22
	 */
23
	public function __construct($path) {
24
		$this->path = static::toAbsolutePath($path);
25
	}
26
27
	/** @return string */
28
	public function getPath() {
29
		return $this->path;
30
	}
31
32
	/** @return string */
33
	public function __toString() {
34
		return $this->toString();
35
	}
36
37
	/** @return string */
38
	public function toString() {
39
		return $this->getPath();
40
	}
41
42
	/** @return string */
43
	public function getRelativePath() {
44
		return str_replace(BASE_PATH . DIRECTORY_SEPARATOR, '', $this->path);
45
	}
46
47
	/**
48
	 * Converte o caminho relativo para absoluto
49
	 * @param $relativePath
50
	 * @return string
51
	 */
52
	public static function toAbsolutePath($relativePath) {
53
		if (!preg_match(static::VALIDATE_PATH, $relativePath . DIRECTORY_SEPARATOR)) {
54
			throw new Exception($relativePath . ' is a invalid directory path.');
55
		}
56
		return BASE_PATH . DIRECTORY_SEPARATOR . $relativePath;
57
	}
58
59
	/** @return boolean */
60
	public function exists() {
61
		return is_dir($this->path);
62
	}
63
64
	/**
65
	 * Renomeia o diretório
66
	 * @param string $newPath Caminho para o novo diretório
67
	 * @return boolean
68
	 */
69
	public function rename($newPath) {
70
		$newFullPath = static::toAbsolutePath($newPath);
71
		$oldFullPath = $this->path;
72
		$this->path = $newFullPath;
73
		return rename($oldFullPath, $newFullPath);
74
	}
75
76
	/**
77
	 * Exclui o diretório e o seu conteúdo
78
	 * @return boolean
79
	 */
80
	public function delete() {
81
		$return = false;
82
		if ($this->exists()) {
83
			$this->deleteContent();
84
			$return = rmdir($this->path);
85
		}
86
		return $return;
87
	}
88
89
	/**
90
	 * Exclui o conteúdo do diretório
91
	 */
92
	public function deleteContent() {
93
		foreach ($this->scan() as $content) {
94
			if (is_dir($this->path . DIRECTORY_SEPARATOR . $content)) {
95
				$subDirectory = new Directory($this->getRelativePath() . DIRECTORY_SEPARATOR . $content);
96
				$subDirectory->delete();
97
			} else {
98
				unlink($this->path . DIRECTORY_SEPARATOR . $content);
99
			}
100
		}
101
	}
102
103
	/**
104
	 * Cria o diretório
105
	 * @param int $chmod
106
	 * @return boolean
107
	 */
108
	public function create($chmod = 0755) {
109
		if (!$this->exists()) {
110
			if (@mkdir($this->path, $chmod, (boolean) STREAM_MKDIR_RECURSIVE) === false) {
111
				throw new Exception('The directory ' . $this->path . ' could not be created.');
112
			}
113
			$this->chmod($chmod);
114
		}
115
		return $this->exists();
116
	}
117
118
	/**
119
	 * Retorna o conteúdo do diretório
120
	 * @return string[]
121
	 */
122
	public function scan() {
123
		return array_diff(scandir($this->path), ['.', '..']);
124
	}
125
126
	/**
127
	 * Define a permissão ao diretório
128
	 * @param int $chmod
129
	 * @return boolean
130
	 */
131
	public function chmod($chmod = 0755) {
132
		return @chmod($this->path, $chmod);
133
	}
134
135
	/** @return string */
136
	public function getPermission() {
137
		clearstatcache();
138
		return substr(decoct(fileperms($this->path)), 2);
139
	}
140
141
	/**
142
	 * Converte uma string para um nome de diretório válido
143
	 * @param string $string
144
	 * @return string
145
	 */
146
	public static function strToDirectoryName($string) {
147
		return trim(strToURL($string), '-');
148
	}
149
150
}
151