Completed
Push — master ( a958fd...f4436f )
by WEBEWEB
02:43
created

FileUtility::rename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 NdC/WBW
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Utility;
13
14
use WBW\Library\Core\Exception\Argument\IllegalArgumentException;
15
use WBW\Library\Core\Exception\Directory\DirectoryNotFoundException;
16
use WBW\Library\Core\Exception\File\FileNotFoundException;
17
use WBW\Library\Core\File\FileSizeInterface;
18
19
/**
20
 * File utility.
21
 *
22
 * @author NdC/WBW <https://github.com/webeweb/>
23
 * @package WBW\Library\Core\Utility
24
 * @final
25
 */
26
final class FileUtility implements FileSizeInterface {
27
28
	/**
29
	 * Delete a file.
30
	 *
31
	 * @param string $filename The filename.
32
	 * @return boolean Returns true in case of success, false otherwise.
33
	 * @throws FileNotFoundException Throws a file not found exception if the file does not exists.
34
	 */
35
	public static function delete($filename) {
36
		if (false === file_exists($filename)) {
37
			throw new FileNotFoundException($filename);
38
		}
39
		return unlink($filename);
40
	}
41
42
	/**
43
	 * Format a size.
44
	 *
45
	 * @param  $size The size.
46
	 * @param string $unit The unit.
47
	 * @return string Returns the formated size.
48
	 */
49
	public static function formatSize($size, $unit = null, $decimals = 2) {
50
51
52
		// Initialize the units.
53
		$units = self::getUnits();
54
55
		// Find the unit.
56
		$index = array_search($unit, $units);
57
		if (null !== $unit && false === $index) {
58
			throw new IllegalArgumentException("The unit \"" . $unit . "\" does not exists");
59
		}
60
61
		// Initialize the output.
62
		$output = $size;
63
64
		$iteration = 0;
65
		while (self::FILE_SIZE_DIVIDER <= $output || $iteration < $index) {
66
			$output /= self::FILE_SIZE_DIVIDER;
67
			++$iteration;
68
		}
69
70
		// Return the output.
71
		return implode(" ", [sprintf("%." . $decimals . "f", $output), $units[$iteration]]);
72
	}
73
74
	/**
75
	 * Get a file contents.
76
	 *
77
	 * @param string $filename The filename.
78
	 * @return string Returns the file contents.
79
	 * @throws FileNotFoundException Throws a file not found exception if the file does not exists.
80
	 */
81
	public static function getContents($filename) {
82
		if (false === file_exists($filename)) {
83
			throw new FileNotFoundException($filename);
84
		}
85
		return file_get_contents($filename);
86
	}
87
88
	/**
89
	 * Get the filenames.
90
	 *
91
	 * @param string $pathname The pathname.
92
	 * @param string $extension The file extension.
93
	 * @return array Returns the filenames.
94
	 * @throws FileNotFoundException Throws a file not found exception if the directory does not exists.
95
	 */
96
	public static function getFilenames($pathname, $extension = null) {
97
98
		// Check if the directory exists.
99
		if (false === file_exists($pathname)) {
100
			throw new DirectoryNotFoundException($pathname);
101
		}
102
103
		// Initialize the filenames.
104
		$filenames = [];
105
106
		// Open the directory.
107
		if (false !== ($directory = opendir($pathname))) {
108
109
			// Initialize the offset.
110
			$offset = strlen($extension);
111
112
			// Read the directory.
113
			while (($file = readdir($directory)) !== false) {
114
115
				// Determines if the file should be added.
116
				if ("." !== $file && ".." !== $file && ((null === $extension) || 0 === substr_compare($file, $extension, -$offset))) {
117
					$filenames[] = $file;
118
				}
119
			}
120
121
			// Close the directory.
122
			closedir($directory);
123
		}
124
125
		// Return the filenames.
126
		return $filenames;
127
	}
128
129
	/**
130
	 * Get a file size.
131
	 *
132
	 * @param string $filename The filename.
133
	 * @return integer Returns the file size.
134
	 * @throws FileNotFoundException Throws a File not found exception if the file does not exists.
135
	 */
136
	public static function getSize($filename) {
137
		if (false === file_exists($filename)) {
138
			throw new FileNotFoundException($filename);
139
		}
140
		clearstatcache();
141
		return filesize($filename);
142
	}
143
144
	/**
145
	 * Get the units.
146
	 *
147
	 * @return array Returns the units.
148
	 */
149
	public static function getUnits() {
150
		return [
151
			self::FILE_SIZE_UNIT_B,
152
			self::FILE_SIZE_UNIT_KB,
153
			self::FILE_SIZE_UNIT_MB,
154
			self::FILE_SIZE_UNIT_GB,
155
			self::FILE_SIZE_UNIT_TB,
156
			self::FILE_SIZE_UNIT_PB,
157
			self::FILE_SIZE_UNIT_EB,
158
			self::FILE_SIZE_UNIT_ZB,
159
			self::FILE_SIZE_UNIT_YB,
160
		];
161
	}
162
163
	/**
164
	 * Rename a file.
165
	 *
166
	 * @param string $oldname The old directory name.
167
	 * @param string $newname The new directory name.
168
	 * @return boolean Returns true in case of success, false otherwise or null if the file can't be renamed.
169
	 * @see DirectoryUtility::rename()
170
	 */
171
	public static function rename($oldname, $newname) {
172
		return DirectoryUtility::rename($oldname, $newname);
173
	}
174
175
}
176