Completed
Push — master ( 767223...0c24fd )
by WEBEWEB
01:31
created

FileUtility::getUnits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
c 1
b 0
f 0
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
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
	 * Format a size.
30
	 *
31
	 * @param  $size The size.
32
	 * @param string $unit The unit.
33
	 * @return string Returns the formated size.
34
	 */
35
	public static function formatSize($size, $unit = null, $decimals = 2) {
36
37
38
		// Initialize the units.
39
		$units = self::getUnits();
40
41
		// Find the unit.
42
		$index = array_search($unit, $units);
43
		if (is_null($unit) === false && $index === false) {
44
			throw new IllegalArgumentException("The unit \"" . $unit . "\" does not exists");
45
		}
46
47
		// Initialize the output.
48
		$output = $size;
49
50
		$iteration = 0;
51
		while (self::FILE_SIZE_DIVIDER <= $output || $iteration < $index) {
52
			$output /= self::FILE_SIZE_DIVIDER;
53
			++$iteration;
54
		}
55
56
		// Return the output.
57
		return implode(" ", [sprintf("%." . $decimals . "f", $output), $units[$iteration]]);
58
	}
59
60
	/**
61
	 * Get a file contents.
62
	 *
63
	 * @param string $filename The filename.
64
	 * @return string Returns the file contents.
65
	 * @throws FileNotFoundException Throws a file not found exception if the file does not exists.
66
	 */
67
	public static function getContents($filename) {
68
		if (!file_exists($filename)) {
69
			throw new FileNotFoundException($filename);
70
		}
71
		return file_get_contents($filename);
72
	}
73
74
	/**
75
	 * Get the filenames.
76
	 *
77
	 * @param string $pathname The pathname.
78
	 * @param string $extension The file extension.
79
	 * @return array Returns the filenames.
80
	 * @throws DirectoryNotFoundException Throws a directory not found exception if the directory does not exists.
81
	 */
82
	public static function getFilenames($pathname, $extension = null) {
83
84
		// Check if the directory exists.
85
		if (!file_exists($pathname)) {
86
			throw new DirectoryNotFoundException($pathname);
87
		}
88
89
		// Initialize the filenames.
90
		$filenames = [];
91
92
		// Open the directory.
93
		if (($directory = opendir($pathname)) !== false) {
94
95
			// Initialize the offset.
96
			$offset = strlen($extension);
97
98
			// Read the directory.
99
			while (($file = readdir($directory)) !== false) {
100
101
				// Determines if the file should be added.
102
				if ($file !== "." && $file !== ".." && ((is_null($extension)) || substr_compare($file, $extension, -$offset) === 0)) {
103
					$filenames[] = $file;
104
				}
105
			}
106
107
			// Close the directory.
108
			closedir($directory);
109
		}
110
111
		// Return the filenames.
112
		return $filenames;
113
	}
114
115
	/**
116
	 * Get a file size.
117
	 *
118
	 * @param string $filename The filename.
119
	 * @return integer Returns the file size.
120
	 * @throws FileNotFoundException Throws a File not found exception if the file does not exists.
121
	 */
122
	public static function getSize($filename) {
123
		if (file_exists($filename) === false) {
124
			throw new FileNotFoundException($filename);
125
		}
126
		clearstatcache();
127
		return filesize($filename);
128
	}
129
130
	/**
131
	 * Get the units.
132
	 *
133
	 * @return array Returns the units.
134
	 */
135
	public static function getUnits() {
136
		return [
137
			self::FILE_SIZE_UNIT_B,
138
			self::FILE_SIZE_UNIT_KB,
139
			self::FILE_SIZE_UNIT_MB,
140
			self::FILE_SIZE_UNIT_GB,
141
			self::FILE_SIZE_UNIT_TB,
142
			self::FILE_SIZE_UNIT_PB,
143
			self::FILE_SIZE_UNIT_EB,
144
			self::FILE_SIZE_UNIT_ZB,
145
			self::FILE_SIZE_UNIT_YB,
146
		];
147
	}
148
149
}
150