Skip to content

Commit 9baeecc

Browse files
committed
Namespaces, phpdoc and README
- [Change] Changed namespace to the composer format - [New] Added PHPDoc to src classes - [New] New readme with docs
1 parent 140ac02 commit 9baeecc

File tree

6 files changed

+204
-82
lines changed

6 files changed

+204
-82
lines changed

.gitignore

-25
This file was deleted.

README.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Namespace: Trihydera\File
2+
3+
## Class: FileInfo
4+
5+
Retrieves information about files in a directory, including file details like size, hash, and modification time.
6+
7+
### Constructor
8+
9+
#### `__construct($dir)`
10+
11+
- **Description:** FileInfo constructor.
12+
- **Parameters:**
13+
- `$dir` (string): The directory path to retrieve file information from.
14+
15+
### Methods
16+
17+
#### `getInfo()`
18+
19+
- **Description:** Get information about files in the directory.
20+
- **Returns:** An array containing details of files in the directory.
21+
22+
#### `listFilesRecursively($dir, &$details)`
23+
24+
- **Description:** Recursively list files in a directory and gather details.
25+
- **Parameters:**
26+
- `$dir` (string): The directory path to list files from.
27+
- `$details` (array): Reference to an array to store file details.
28+
29+
#### `formatSize($bytes)`
30+
31+
- **Description:** Format file size in human-readable format.
32+
- **Parameters:**
33+
- `$bytes` (int): The size of the file in bytes.
34+
- **Returns:** The formatted file size with appropriate units.
35+
36+
---
37+
38+
## Class: FileTypes
39+
40+
Defines different file types along with their categories and purposes.
41+
42+
### Properties
43+
44+
#### `$types`
45+
46+
- **Description:** Array containing file types with their categories and purposes.
47+
- **Type:** array
48+
- **Values:**
49+
- `exe`: Windows - Executable file
50+
- `msi`: Windows - Windows Installer Package
51+
- `jpg`: Image - JPEG Image
52+
- `png`: Image - PNG Image
53+
- `txt`: Text - Plain Text File
54+
- `pdf`: Document - PDF Document
55+
- `doc`: Document - Microsoft Word Document
56+
- `xlsx`: Document - Microsoft Excel Spreadsheet
57+
- `zip`: Archive - Compressed Archive
58+
- `mp3`: Audio - MP3 Audio File
59+
60+
---
61+
62+
## Class: JsonFile
63+
64+
Handles reading and writing JSON data to files.
65+
66+
### Methods
67+
68+
#### `read($file)`
69+
70+
- **Description:** Read JSON data from a file.
71+
- **Parameters:**
72+
- `$file` (string): The path to the JSON file.
73+
- **Returns:** The decoded JSON data.
74+
- **Throws:** `\Exception` If the file path is invalid.
75+
76+
#### `write($file, $data)`
77+
78+
- **Description:** Write JSON data to a file.
79+
- **Parameters:**
80+
- `$file` (string): The path to the JSON file.
81+
- `$data` (array): The data to be encoded as JSON and written to the file.
82+
- **Throws:** `\Exception` If the file path is invalid.

message.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Namespaces, phpdoc and README
2+
3+
- [Change] Changed namespace to the composer format
4+
- [New] Added PHPDoc to src classes
5+
- [New] New readme with docs

src/FileInfo.php

+69-42
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,82 @@
11
<?php
2-
namespace TriHydera\File;
2+
namespace Trihydera\File;
33

4+
/**
5+
* Class FileInfo
6+
*
7+
* Retrieves information about files in a directory, including file details like size, hash, and modification time.
8+
*/
49
class FileInfo {
5-
private $dir;
10+
private $dir;
611

7-
public function __construct($dir) {
8-
$this->dir = $dir;
9-
}
12+
/**
13+
* FileInfo constructor.
14+
*
15+
* @param string $dir The directory path to retrieve file information from.
16+
*/
17+
public function __construct($dir) {
18+
$this->dir = $dir;
19+
}
1020

11-
public function getInfo() {
12-
$details = [];
13-
$this->listFilesRecursively($this->dir, $details);
14-
return $details;
15-
}
21+
/**
22+
* Get information about files in the directory.
23+
*
24+
* @return array An array containing details of files in the directory.
25+
*/
26+
public function getInfo() {
27+
$details = [];
28+
$this->listFilesRecursively($this->dir, $details);
29+
return $details;
30+
}
1631

17-
private function listFilesRecursively($dir, &$details) {
18-
$files = scandir($dir);
32+
/**
33+
* Recursively list files in a directory and gather details.
34+
*
35+
* @param string $dir The directory path to list files from.
36+
* @param array $details Reference to an array to store file details.
37+
*/
38+
private function listFilesRecursively($dir, &$details) {
39+
$files = scandir($dir);
1940

20-
foreach ($files as $file) {
21-
if ($file === '.' || $file === '..') {
22-
continue;
23-
}
41+
foreach ($files as $file) {
42+
if ($file === '.' || $file === '..') {
43+
continue;
44+
}
2445

25-
$path = $dir . '/' . $file;
26-
27-
if (is_dir($path)) {
28-
$this->listFilesRecursively($path, $details);
29-
} else {
30-
$hash = md5_file($path);
31-
$size = filesize($path);
32-
$modified = filemtime($path);
33-
$formattedSize = $this->formatSize($size);
34-
$shortCode = substr($hash, 2, 4).substr($hash, 10, 4);
35-
36-
$details[$file] = [
37-
'id' => $shortCode,
38-
'name' => $file,
39-
'modified' => date('Y-m-d H:i', $modified),
40-
'size_raw' => $size,
41-
'size' => $formattedSize,
42-
'hash' => $hash,
43-
'path' => $path
44-
];
46+
$path = $dir . '/' . $file;
47+
48+
if (is_dir($path)) {
49+
$this->listFilesRecursively($path, $details);
50+
} else {
51+
$hash = md5_file($path);
52+
$size = filesize($path);
53+
$modified = filemtime($path);
54+
$formattedSize = $this->formatSize($size);
55+
$shortCode = substr($hash, 2, 4) . substr($hash, 10, 4);
56+
57+
$details[$file] = [
58+
'id' => $shortCode,
59+
'name' => $file,
60+
'modified' => date('Y-m-d H:i', $modified),
61+
'size_raw' => $size,
62+
'size' => $formattedSize,
63+
'hash' => $hash,
64+
'path' => $path
65+
];
66+
}
4567
}
4668
}
47-
}
4869

49-
private function formatSize($bytes) {
50-
$units = array('B', 'KB', 'MB', 'GB', 'TB');
51-
$i = floor(log($bytes, 1024));
52-
return @round($bytes / pow(1024, $i), 2) . ' ' . $units[$i];
53-
}
70+
/**
71+
* Format file size in human-readable format.
72+
*
73+
* @param int $bytes The size of the file in bytes.
74+
* @return string The formatted file size with appropriate units.
75+
*/
76+
private function formatSize($bytes) {
77+
$units = array('B', 'KB', 'MB', 'GB', 'TB');
78+
$i = floor(log($bytes, 1024));
79+
return @round($bytes / pow(1024, $i), 2) . ' ' . $units[$i];
80+
}
5481
}
5582
?>

src/FileTypes.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
<?php
2-
namespace TriHydera\File;
2+
namespace Trihydera\File;
33

4+
/**
5+
* Class FileTypes
6+
*
7+
* Defines different file types along with their categories and purposes.
8+
*/
49
class FileTypes {
10+
/**
11+
* Array containing file types with their categories and purposes.
12+
*
13+
* @var array
14+
*/
515
public $types = [
616
'exe' => ['category' => 'Windows', 'purpose' => 'Executable file'],
717
'msi' => ['category' => 'Windows', 'purpose' => 'Windows Installer Package'],

src/JsonFile.php

+37-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
11
<?php
2-
namespace TriHydera\File;
2+
namespace Trihydera\File;
33

4-
class JsonFile {
5-
private $log;
6-
7-
public function __construct() {
8-
$this->log = $log;
9-
}
10-
11-
public function read(string $file)
4+
/**
5+
* Class JsonFile
6+
*
7+
* Handles reading and writing JSON data to files.
8+
*/
9+
class JsonFile
10+
{
11+
/**
12+
* Read JSON data from a file.
13+
*
14+
* @param string $file The path to the JSON file.
15+
* @return array The decoded JSON data.
16+
* @throws \Exception If the file path is invalid.
17+
*/
18+
public function read($file)
1219
{
13-
$split = explode('/..', $file);
20+
// Sanitize the file path
21+
$file = realpath($file);
22+
23+
if ($file === false || strpos($file, __DIR__) !== 0) {
24+
throw new \Exception('Invalid file path');
25+
}
1426

1527
$contents = file_exists($file) ? file_get_contents($file) : '[]';
16-
return json_decode($contents, true);
28+
return json_decode($contents, true);
1729
}
1830

19-
public function write(string $file, array $data)
31+
/**
32+
* Write JSON data to a file.
33+
*
34+
* @param string $file The path to the JSON file.
35+
* @param array $data The data to be encoded as JSON and written to the file.
36+
* @throws \Exception If the file path is invalid.
37+
*/
38+
public function write($file, $data)
2039
{
21-
$split = explode('/..', $file);
40+
// Sanitize the file path
41+
$file = realpath($file);
42+
43+
if ($file === false || strpos($file, __DIR__) !== 0) {
44+
throw new \Exception('Invalid file path');
45+
}
2246

2347
file_put_contents($file, json_encode($data));
2448
}
2549
}
26-
2750
?>

0 commit comments

Comments
 (0)