Skip to content

Commit f6940da

Browse files
authored
Merge pull request #4 from PhantPHP/version-2
Hello world !
2 parents 6811d2d + 54ae279 commit f6940da

File tree

12 files changed

+1373
-154
lines changed

12 files changed

+1373
-154
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
composer.lock
33
vendor/*
44

5+
# Test
6+
.phpunit*
7+
.public
8+
59
# Dev
610
.DS_Store
711
.nova/*
12+
.php-cs-fixer.cache

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Requirments
44

5-
PHP >= 8.0
5+
PHP >= 8.1
66

77

88
## Install

component/Csv.php

Lines changed: 71 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,80 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Phant\File;
6+
57
use Phant\File\File;
68

79
class Csv extends File
810
{
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-
}
11+
protected array $columns;
12+
13+
public function __construct(
14+
protected readonly string $path,
15+
protected readonly bool $headerInFirstLine = true,
16+
protected readonly string $delimiter = ';',
17+
protected readonly string $textSeparator = '"'
18+
) {
19+
$this->loadHeader();
20+
}
21+
22+
public function verifyColumns(array $columns): bool
23+
{
24+
if (count($this->columns) != count($columns)) {
25+
return false;
26+
}
27+
28+
foreach ($this->columns as $colonne) {
29+
if (!in_array($colonne, $columns)) {
30+
return false;
31+
}
32+
}
33+
34+
return true;
35+
}
36+
37+
public function getNbLines(): int
38+
{
39+
$nbLines = 0;
40+
41+
$handle = fopen($this->path, 'r');
42+
43+
while (!feof($handle)) {
44+
fgets($handle);
45+
$nbLines++;
46+
}
47+
48+
fclose($handle);
49+
50+
return $nbLines;
51+
}
52+
53+
public function readFileByLine(): \Generator
54+
{
55+
$handle = fopen($this->path, 'r');
56+
57+
$lineNumber = 0;
58+
while (($cells = fgetcsv($handle, 0, $this->delimiter, $this->textSeparator)) !== false) {
59+
if ($this->headerInFirstLine && $lineNumber === 0) {
60+
++$lineNumber;
61+
continue;
62+
}
63+
64+
$values = $this->headerInFirstLine ? array_combine($this->columns, $cells) : $cells;
65+
66+
yield $values;
67+
68+
++$lineNumber;
69+
}
70+
71+
fclose($handle);
72+
}
73+
74+
private function loadHeader(): void
75+
{
76+
$handle = fopen($this->path, 'r');
77+
$this->columns = fgetcsv($handle, 0, $this->delimiter, $this->textSeparator);
78+
fclose($handle);
79+
}
7980
}

component/File.php

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,65 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Phant\File;
56

67
class File
78
{
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-
}
9+
public function __construct(
10+
protected readonly string $path
11+
) {
12+
}
13+
14+
public function exist(): bool
15+
{
16+
return file_exists($this->path);
17+
}
18+
19+
public function delete()
20+
{
21+
unlink($this->path);
22+
}
23+
24+
public static function getTemoraryDirectory(): string
25+
{
26+
return realpath(sys_get_temp_dir()) . '/';
27+
}
28+
29+
public static function cleanFilename(string $fileName): string
30+
{
31+
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
32+
$fileName = pathinfo($fileName, PATHINFO_FILENAME);
33+
34+
// Clean local filename
35+
$fileName = pathinfo($fileName, PATHINFO_FILENAME);
36+
37+
$fileName = strtr(
38+
$fileName,
39+
['Š' => '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']
40+
);
41+
42+
$fileName = strtr(
43+
$fileName,
44+
['Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u']
45+
);
46+
47+
$fileName = preg_replace(['/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'], ['_', '.', ''], $fileName);
48+
49+
$extension = strtolower($extension);
50+
$extension = trim($extension);
51+
52+
return $fileName . ($extension ? '.' . $extension : '');
53+
}
54+
55+
public static function download(string $distantPath, ?string $localPath = null): self
56+
{
57+
if (!$localPath) {
58+
$localPath = self::getTemoraryDirectory() . date('Y-m-d_H-i-s') . '-' . self::cleanFilename($distantPath);
59+
}
60+
61+
file_put_contents($localPath, fopen($distantPath, 'r'));
62+
63+
return new self($localPath);
64+
}
6865
}

component/Zip.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Phant\File;
6+
57
use Phant\File\File;
6-
use \ZipArchive;
8+
use ZipArchive;
79

810
class Zip extends File
911
{
10-
public function unarchive(?string $unarchiveDirectory = null): array
11-
{
12-
if (is_null($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-
}
12+
public function unarchive(?string $unarchiveDirectory = null): ?array
13+
{
14+
if (! $this->exist()) {
15+
return null;
16+
}
17+
18+
if (is_null($unarchiveDirectory)) {
19+
$unarchiveDirectory = self::getTemoraryDirectory() . '/';
20+
}
21+
22+
$files = [];
23+
24+
$zip = new ZipArchive();
25+
$zip->open($this->path);
26+
if ($zip->extractTo($unarchiveDirectory) == true) {
27+
for ($i = 0; $i < $zip->numFiles; $i++) {
28+
$filepath = realpath($unarchiveDirectory . $zip->getNameIndex($i));
29+
if (is_dir($filepath)) {
30+
continue;
31+
}
32+
$files[ $zip->getNameIndex($i) ] = new File($filepath);
33+
}
34+
}
35+
$zip->close();
36+
37+
return $files;
38+
}
2939
}

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^8.0"
13+
"php": ">=8.1"
1414
},
1515
"require-dev": {
16-
"phpstan/phpstan": "^1.4"
16+
"friendsofphp/php-cs-fixer": "^3.0",
17+
"phpstan/phpstan": "^1.4",
18+
"phpunit/phpunit": "^9.5"
1719
},
1820
"scripts": {
19-
"analyse": "vendor/bin/phpstan analyse component --memory-limit=4G"
21+
"lint": "vendor/bin/php-cs-fixer fix ./ --rules=@PSR12",
22+
"analyse": "vendor/bin/phpstan analyse component --memory-limit=4G",
23+
"test": "vendor/bin/phpunit test --testdox",
24+
"coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html .public/code-coverage"
2025
},
2126
"autoload": {
2227
"psr-4": {

fixture/archive.zip

560 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)