|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Brainbits\FunctionalTestHelpers\SevenZipContents; |
| 6 | + |
| 7 | +use DateTimeImmutable; |
| 8 | + |
| 9 | +use function array_pop; |
| 10 | +use function array_push; |
| 11 | +use function explode; |
| 12 | +use function implode; |
| 13 | +use function str_replace; |
| 14 | +use function substr; |
| 15 | +use function trim; |
| 16 | + |
| 17 | +final readonly class SevenZipFileInfo |
| 18 | +{ |
| 19 | + private string $path; |
| 20 | + private DateTimeImmutable|null $lastModified; |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + string $path, |
| 24 | + private int $size, |
| 25 | + private int $compressedSize, |
| 26 | + private int $compression, |
| 27 | + string|null $lastModified, |
| 28 | + private string|null $crc, |
| 29 | + private bool $isDir, |
| 30 | + ) { |
| 31 | + $this->path = $this->cleanPath($path); |
| 32 | + |
| 33 | + $this->lastModified = $lastModified |
| 34 | + ? DateTimeImmutable::createFromFormat('Y-m-d H:i:s', substr($lastModified, 0, 19)) |
| 35 | + : null; |
| 36 | + } |
| 37 | + |
| 38 | + public function getPath(): string |
| 39 | + { |
| 40 | + return $this->path; |
| 41 | + } |
| 42 | + |
| 43 | + public function getSize(): int |
| 44 | + { |
| 45 | + if ($this->isDir) { |
| 46 | + return 0; |
| 47 | + } |
| 48 | + |
| 49 | + return $this->size; |
| 50 | + } |
| 51 | + |
| 52 | + public function getCompressedSize(): int |
| 53 | + { |
| 54 | + return $this->compressedSize; |
| 55 | + } |
| 56 | + |
| 57 | + public function getCompression(): int |
| 58 | + { |
| 59 | + return $this->compression; |
| 60 | + } |
| 61 | + |
| 62 | + public function getCrc(): string|null |
| 63 | + { |
| 64 | + return $this->crc; |
| 65 | + } |
| 66 | + |
| 67 | + public function isDir(): bool |
| 68 | + { |
| 69 | + return $this->isDir; |
| 70 | + } |
| 71 | + |
| 72 | + public function getLastModified(): DateTimeImmutable|null |
| 73 | + { |
| 74 | + return $this->lastModified; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Cleans up a path and removes relative parts, also strips leading slashes |
| 79 | + */ |
| 80 | + private function cleanPath(string $path): string |
| 81 | + { |
| 82 | + $path = str_replace('\\', '/', $path); |
| 83 | + $path = explode('/', $path); |
| 84 | + $newpath = []; |
| 85 | + foreach ($path as $p) { |
| 86 | + if ($p === '' || $p === '.') { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if ($p === '..') { |
| 91 | + array_pop($newpath); |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + array_push($newpath, $p); |
| 96 | + } |
| 97 | + |
| 98 | + return trim(implode('/', $newpath), '/'); |
| 99 | + } |
| 100 | +} |
0 commit comments