Skip to content
This repository was archived by the owner on Jul 28, 2024. It is now read-only.

Commit e7489d3

Browse files
authored
Merge pull request #5 from wavevision/feature/zip
Feature/zip
2 parents 23a6e68 + 24e8472 commit e7489d3

File tree

13 files changed

+202
-3
lines changed

13 files changed

+202
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/.phpunit.result.cache
66
/temp/
77
/vendor/
8+
test.zip

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The package contains useful classes for:
2323
- [ContentTypes](./src/Utils/ContentTypes.php) – format extensions and filenames for content types
2424
- [DOM](./src/Utils/DOM) – create and format data attributes for HTML elements
2525
- [ExternalProgram](./src/Utils/ExternalProgram/Executor.php) – simple external command runner
26+
- [FileInfo](./src/Utils/FileInfo.php) – get file info (basename, dirname, extension etc.)
2627
- [Finder](./src/Utils/Finder.php) – adds sorting to [nette/finder](https://github.com/nette/finder)
2728
- [ImageInfo](./src/Utils/ImageInfo.php) – get image content type and size
2829
- [Json](./src/Utils/Json.php) – JSON pretty encoder with PHP and JavaScript indents
@@ -33,3 +34,4 @@ The package contains useful classes for:
3334
- [Strings](./src/Utils/Strings.php) – string helpers (encode, transform etc.)
3435
- [Tokenizer](./src/Utils/Tokenizer/Tokenizer.php) – get structure from file (e.g. a class)
3536
- [Validators](./src/Utils/Validators.php) – validate Czech and Slovak numbers (phone, personal, business)
37+
- [Zip](./src/Utils/Zip) – simple ZIP archive helper (compress, extract)

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
"sort-packages": true
3535
},
3636
"require": {
37+
"ext-fileinfo": "*",
3738
"ext-iconv": "*",
39+
"ext-json": "*",
40+
"ext-zip": "*",
3841
"flow/jsonpath": "^0.4.0",
3942
"nette/finder": "^2.5",
4043
"nette/utils": "^3.1",

src/Utils/DOM/DataAttribute.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ public function asArray($value = null): array
4444
return [$this->currentName => $this->value($value)];
4545
}
4646

47+
/**
48+
* @param mixed $value
49+
* @return array<string>
50+
*/
51+
public function asTuple($value = null): array
52+
{
53+
return [$this->currentName, $this->value($value)];
54+
}
55+
4756
/**
4857
* @param Html<mixed> $element
4958
* @param mixed $value

src/Utils/ExternalProgram/Executor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace Wavevision\Utils\ExternalProgram;
44

5+
use Nette\StaticClass;
6+
57
class Executor
68
{
79

10+
use StaticClass;
11+
812
public static function executeUnchecked(string $command): Result
913
{
1014
exec($command . ' 2>&1', $output, $returnValue);

src/Utils/ExternalProgram/Result.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace Wavevision\Utils\ExternalProgram;
44

5+
use Nette\SmartObject;
6+
57
class Result
68
{
79

10+
use SmartObject;
11+
812
private string $command;
913

1014
/**

src/Utils/FileInfo.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class FileInfo
1616

1717
private string $baseName;
1818

19+
private string $dirName;
20+
1921
private string $extension;
2022

2123
private string $contentType;
@@ -31,6 +33,7 @@ public function __construct(string $pathName)
3133
}
3234
$this->pathName = $pathName;
3335
$this->baseName = basename($pathName);
36+
$this->dirName = dirname($pathName);
3437
$this->extension = pathinfo($pathName, PATHINFO_EXTENSION);
3538
$this->contentType = (string)(new finfo(FILEINFO_MIME_TYPE))->file($pathName);
3639
$this->size = (int)filesize($pathName);
@@ -42,14 +45,22 @@ public function getPathName(): string
4245
return $this->pathName;
4346
}
4447

45-
public function getBaseName(): string
48+
public function getBaseName(bool $withoutExtension = false): string
4649
{
50+
if ($withoutExtension) {
51+
return str_replace($this->getExtension(true), '', $this->baseName);
52+
}
4753
return $this->baseName;
4854
}
4955

50-
public function getExtension(): string
56+
public function getDirName(): string
57+
{
58+
return $this->dirName;
59+
}
60+
61+
public function getExtension(bool $withDot = false): string
5162
{
52-
return $this->extension;
63+
return $withDot ? '.' . $this->extension : $this->extension;
5364
}
5465

5566
public function getContentType(): string

src/Utils/Zip/ZipArchive.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php declare (strict_types = 1);
2+
3+
namespace Wavevision\Utils\Zip;
4+
5+
use Nette\FileNotFoundException;
6+
use Nette\SmartObject;
7+
use Wavevision\Utils\FileInfo;
8+
use Wavevision\Utils\Path;
9+
use ZipArchive as Zip;
10+
11+
class ZipArchive
12+
{
13+
14+
use SmartObject;
15+
16+
/**
17+
* @var ZipArchiveFile[]
18+
*/
19+
private array $files;
20+
21+
private string $path;
22+
23+
private Zip $zip;
24+
25+
public function __construct(string $path, ZipArchiveFile ...$files)
26+
{
27+
$this->files = $files;
28+
$this->path = $path;
29+
$this->zip = new Zip();
30+
}
31+
32+
public function addFile(ZipArchiveFile $file): self
33+
{
34+
$this->files[] = $file;
35+
return $this;
36+
}
37+
38+
public function close(): self
39+
{
40+
$this->zip->close();
41+
return $this;
42+
}
43+
44+
public function compress(): self
45+
{
46+
foreach ($this->files as $file) {
47+
$this->zip->addFile($this->getFilePath($file), $file->getName());
48+
}
49+
return $this->close();
50+
}
51+
52+
public function extract(?string $dir = null): self
53+
{
54+
$this->zip->extractTo($dir ?? $this->getExtractDir());
55+
return $this;
56+
}
57+
58+
public function getName(): string
59+
{
60+
return basename($this->getPath());
61+
}
62+
63+
public function getPath(): string
64+
{
65+
return $this->path;
66+
}
67+
68+
public function read(): self
69+
{
70+
$this->zip->open($this->getPath());
71+
return $this;
72+
}
73+
74+
public function write(): self
75+
{
76+
$flag = is_file($this->getPath()) ? Zip::OVERWRITE : Zip::CREATE;
77+
$this->zip->open($this->getPath(), $flag);
78+
return $this;
79+
}
80+
81+
private function getExtractDir(): string
82+
{
83+
$fileInfo = new FileInfo($this->getPath());
84+
return Path::join($fileInfo->getDirName(), $fileInfo->getBaseName(true));
85+
}
86+
87+
private function getFilePath(ZipArchiveFile $file): string
88+
{
89+
$path = $file->getPath();
90+
if (!file_exists($path)) {
91+
throw new FileNotFoundException("Zip archive file '$path' not found.");
92+
}
93+
return $path;
94+
}
95+
96+
}

src/Utils/Zip/ZipArchiveFile.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare (strict_types = 1);
2+
3+
namespace Wavevision\Utils\Zip;
4+
5+
use Nette\SmartObject;
6+
7+
final class ZipArchiveFile
8+
{
9+
10+
use SmartObject;
11+
12+
private string $name;
13+
14+
private string $path;
15+
16+
public function __construct(string $path, ?string $name = null)
17+
{
18+
$this->name = $name ?? basename($path);
19+
$this->path = $path;
20+
}
21+
22+
public function getName(): string
23+
{
24+
return $this->name;
25+
}
26+
27+
public function getPath(): string
28+
{
29+
return $this->path;
30+
}
31+
32+
}

tests/UtilsTests/DOM/DataAttributeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public function testAsString(): void
2626
$this->assertEquals('data-prefix-test=""', (string)$this->createDataAttribute('prefix'));
2727
}
2828

29+
public function testAsTuple(): void
30+
{
31+
$this->assertEquals(['data-test', ''], $this->createDataAttribute()->asTuple());
32+
}
33+
2934
public function testAssign(): void
3035
{
3136
$attribute = $this->createDataAttribute();

0 commit comments

Comments
 (0)