Skip to content

Commit ac0764d

Browse files
committed
Init
1 parent e97e3b9 commit ac0764d

File tree

6 files changed

+323
-2
lines changed

6 files changed

+323
-2
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Dependencies
2+
composer.lock
3+
vendor/*
4+
5+
# Dev
6+
.DS_Store
7+
.nova/*

README.md

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,114 @@
1-
# file
2-
Files
1+
# Files
2+
3+
## Requirments
4+
5+
PHP >= 8.0
6+
7+
8+
## Install
9+
10+
`composer require phant/file`
11+
12+
## Usages
13+
14+
### File
15+
16+
```php
17+
use Phant\File\File;
18+
19+
$file = new File('path/filename.ext');
20+
```
21+
22+
23+
#### Get file path
24+
25+
```php
26+
$filePath = $file->getPath();
27+
```
28+
29+
30+
#### Verify if file exist file path
31+
32+
```php
33+
$fileExist = $file->exist();
34+
```
35+
36+
37+
#### Delete file
38+
39+
```php
40+
$file->delete();
41+
```
42+
43+
44+
#### Get temporary path
45+
46+
```php
47+
$temoraryDirectory = $file->getTemoraryDirectory();
48+
```
49+
50+
51+
#### Clean filename
52+
53+
```php
54+
$cleanFilename = File::cleanFilename($dirtyFilename);
55+
```
56+
57+
58+
#### Download file to temporary directory
59+
60+
```php
61+
$file = File::download($fileUrl);
62+
```
63+
64+
65+
66+
### Csv file
67+
68+
```php
69+
use Phant\File\Csv;
70+
71+
$file = new File('path/filename.csv');
72+
```
73+
74+
75+
#### Verify columns
76+
77+
```php
78+
$isConform = $file->verifyColumns($columns);
79+
```
80+
81+
82+
#### Get number of lines
83+
84+
```php
85+
$nbLines = $file->getNbLines();
86+
```
87+
88+
89+
#### Read file by line
90+
91+
```php
92+
foreach ($file->readFileByLine() as $line) {
93+
94+
}
95+
```
96+
97+
98+
### Zip file
99+
100+
```php
101+
use Phant\File\Zip;
102+
103+
$file = new File('path/filename.zip');
104+
```
105+
106+
107+
#### Unarchive
108+
109+
```php
110+
$files = $file->unarchive();
111+
foreach ($files as $file) {
112+
}
113+
```
114+

component/Csv.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Phant\File;
5+
use Phant\File\File;
6+
7+
class Csv extends File
8+
{
9+
protected bool $headerInFirstLine;
10+
protected array $columns;
11+
protected string $delimiter;
12+
protected string $textSeparator;
13+
14+
public function __construct(string $path, bool $headerInFirstLine = true, string $delimiter = ';', string $textSeparator = '"')
15+
{
16+
parent::__construct($path);
17+
18+
$this->headerInFirstLine = $headerInFirstLine;
19+
$this->columns = [];
20+
$this->delimiter = $delimiter;
21+
$this->textSeparator = $textSeparator;
22+
}
23+
24+
public function verifyColumns(array $columns): bool
25+
{
26+
$handle = fopen($this->path, 'r');
27+
$this->columns = fgetcsv($handle, 0, $this->delimiter, $this->textSeparator);
28+
fclose($handle);
29+
30+
if (count($this->columns) != count($columns)) {
31+
return false;
32+
}
33+
34+
foreach ($this->columns as $colonne) {
35+
if (!in_array($colonne, $columns)) {
36+
return false;
37+
}
38+
}
39+
40+
return true;
41+
}
42+
43+
public function getNbLines(): int
44+
{
45+
$nbLines = 0;
46+
47+
$handle = fopen($this->path, 'r');
48+
49+
while (!feof($handle)) {
50+
fgets($handle);
51+
$nbLines++;
52+
}
53+
54+
fclose($handle);
55+
56+
return $nbLines;
57+
}
58+
59+
public function readFileByLine()
60+
{
61+
$handle = fopen($this->path, 'r');
62+
63+
$lineNumber = 0;
64+
while (($cells = fgetcsv($handle, 0, $this->delimiter, $this->textSeparator)) !== false) {
65+
if ($this->headerInFirstLine && $lineNumber === 0) {
66+
++$lineNumber;
67+
continue;
68+
}
69+
70+
$values = $this->headerInFirstLine ? array_combine($this->columns, $cells) : $cells;
71+
72+
yield $values;
73+
74+
++$lineNumber;
75+
}
76+
77+
fclose($handle);
78+
}
79+
}

component/File.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Phant\File;
5+
6+
class File
7+
{
8+
protected string $path;
9+
10+
public function __construct(string $path)
11+
{
12+
$this->path = $path;
13+
}
14+
15+
public function getPath(): string
16+
{
17+
return $this->path;
18+
}
19+
20+
public function exist(): bool
21+
{
22+
return file_exists($this->path);
23+
}
24+
25+
public function delete()
26+
{
27+
unlink($this->path);
28+
}
29+
30+
public static function getTemoraryDirectory(): string
31+
{
32+
return realpath(sys_get_temp_dir()) . '/';
33+
}
34+
35+
public static function cleanFilename(string $fileName): string
36+
{
37+
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
38+
$fileName = pathinfo($fileName, PATHINFO_FILENAME);
39+
40+
// Clean local filename
41+
$fileName = pathinfo($fileName, PATHINFO_FILENAME);
42+
43+
$fileName = strtr(
44+
$fileName,
45+
['Š' => 'S','Ž' => 'Z','š' => 's','ž' => 'z','Ÿ' => 'Y','À' => 'A','Á' => 'A','Â' => 'A','Ã' => 'A','Ä' => 'A','Å' => 'A','Ç' => 'C','È' => 'E','É' => 'E','Ê' => 'E','Ë' => 'E','Ì' => 'I','Í' => 'I','Î' => 'I','Ï' => 'I','Ñ' => 'N','Ò' => 'O','Ó' => 'O','Ô' => 'O','Õ' => 'O','Ö' => 'O','Ø' => 'O','Ù' => 'U','Ú' => 'U','Û' => 'U','Ü' => 'U','Ý' => 'Y','à' => 'a','á' => 'a','â' => 'a','ã' => 'a','ä' => 'a','å' => 'a','ç' => 'c','è' => 'e','é' => 'e','ê' => 'e','ë' => 'e','ì' => 'i','í' => 'i','î' => 'i','ï' => 'i','ñ' => 'n','ò' => 'o','ó' => 'o','ô' => 'o','õ' => 'o','ö' => 'o','ø' => 'o','ù' => 'u','ú' => 'u','û' => 'u','ü' => 'u','ý' => 'y','ÿ' => 'y']
46+
);
47+
48+
$fileName = strtr(
49+
$fileName,
50+
['Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u']
51+
);
52+
53+
$fileName = preg_replace(['/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'], ['_', '.', ''], $fileName);
54+
55+
return $fileName . '.' . $extension;
56+
}
57+
58+
public static function download(string $distantPath, ?string $localPath = null): self
59+
{
60+
if (!$localPath) {
61+
$localPath = self::getTemoraryDirectory() . date('Y-m-d_H-i-s') . '-' . self::cleanFilename($distantPath);
62+
}
63+
64+
file_put_contents($localPath, fopen($distantPath, 'r'));
65+
66+
return new self($localPath);
67+
}
68+
}

component/Zip.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Phant\File;
5+
use Phant\File\File;
6+
use \ZipArchive;
7+
8+
class Zip extends File
9+
{
10+
public function unarchive(?string $unarchiveDirectory = null): array
11+
{
12+
if ($unarchiveDirectory) {
13+
$unarchiveDirectory = self::getTemoraryDirectory() . pathinfo($this->path, PATHINFO_FILENAME) . '/';
14+
}
15+
16+
$files = [];
17+
18+
$zip = new ZipArchive;
19+
$zip->open($this->path);
20+
if ($zip->extractTo($unarchiveDirectory) == true) {
21+
for ($i = 0; $i < $zip->numFiles; $i++) {
22+
$files[ $zip->getNameIndex($i) ] = new File($unarchiveDirectory . $zip->getNameIndex($i));
23+
}
24+
}
25+
$zip->close();
26+
27+
return $files;
28+
}
29+
}

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "phant/file",
3+
"description": "Manage file easily",
4+
"license": "MIT",
5+
"keywords": ["file manager", "file loader", "file reader", "csv file", "csv reader"],
6+
"authors": [
7+
{
8+
"name": "Lenny ROUANET",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": "^8.0"
14+
},
15+
"require-dev": {
16+
"phpstan/phpstan": "^1.4"
17+
},
18+
"scripts": {
19+
"analyse": "vendor/bin/phpstan analyse component --memory-limit=4G"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Phant\\File\\": "component/"
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)