Skip to content

Commit

Permalink
Convert newinstance to anonymous classes
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Apr 10, 2020
1 parent f85fea8 commit 221f7c2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
87 changes: 47 additions & 40 deletions src/main/php/io/archive/zip/Compression.class.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
<?php namespace io\archive\zip;

use io\streams\{Bz2CompressingOutputStream, Bz2DecompressingInputStream, DeflatingOutputStream, InflatingInputStream, InputStream};
use lang\Enum;
use io\streams\{
InputStream,
OutputStream,
Bz2CompressingOutputStream,
Bz2DecompressingInputStream,
DeflatingOutputStream,
InflatingInputStream
};
use lang\{Enum, IllegalArgumentException};

/**
* Compression algorithm enumeration.
*
* The following compression algorithms are defined by the standard:
* <pre>
* 0 - The file is stored (no compression)
* 1 - The file is Shrunk
* 2 - The file is Reduced with compression factor 1
* 3 - The file is Reduced with compression factor 2
* 4 - The file is Reduced with compression factor 3
* 5 - The file is Reduced with compression factor 4
* 6 - The file is Imploded
* 7 - Reserved for Tokenizing compression algorithm
* 8 - The file is Deflated
* 9 - Enhanced Deflating using Deflate64(tm)
* 10 - PKWARE Data Compression Library Imploding (old IBM TERSE)
* 12 - File is compressed using BZIP2 algorithm
* 14 - LZMA (EFS)
* 18 - File is compressed using IBM TERSE (new)
* 19 - IBM LZ77 z Architecture (PFS)
* 97 - WavPack compressed data
* 98 - PPMd version I, Rev 1
* </pre>
* ```
* 0 - The file is stored (no compression)
* 1 - The file is Shrunk
* 2 - The file is Reduced with compression factor 1
* 3 - The file is Reduced with compression factor 2
* 4 - The file is Reduced with compression factor 3
* 5 - The file is Reduced with compression factor 4
* 6 - The file is Imploded
* 7 - Reserved for Tokenizing compression algorithm
* 8 - The file is Deflated
* 9 - Enhanced Deflating using Deflate64(tm)
* 10 - PKWARE Data Compression Library Imploding (old IBM TERSE)
* 12 - File is compressed using BZIP2 algorithm
* 14 - LZMA (EFS)
* 18 - File is compressed using IBM TERSE (new)
* 19 - IBM LZ77 z Architecture (PFS)
* 97 - WavPack compressed data
* 98 - PPMd version I, Rev 1
* ```
*
* This implementation supports 0 (NONE), 8 (GZ) and 12 (BZIP2).
*
Expand All @@ -39,39 +46,39 @@ abstract class Compression extends Enum {
public static $NONE, $GZ, $BZ;

static function __static() {
self::$NONE= newinstance(__CLASS__, [0, 'NONE'], '{
self::$NONE= new class(0, 'NONE') extends Compression {
static function __static() { }

public function getCompressionStream(\io\streams\OutputStream $out, $level= 6) {
public function getCompressionStream(OutputStream $out, $level= 6) {
return $out;
}

public function getDecompressionStream(\io\streams\InputStream $in) {
public function getDecompressionStream(InputStream $in) {
return $in;
}
}');
self::$GZ= newinstance(__CLASS__, [8, 'GZ'], '{
};
self::$GZ= new class(8, 'GZ') extends Compression {
static function __static() { }

public function getCompressionStream(\io\streams\OutputStream $out, $level= 6) {
return new \io\streams\DeflatingOutputStream($out, $level);
public function getCompressionStream(OutputStream $out, $level= 6) {
return new DeflatingOutputStream($out, $level);
}

public function getDecompressionStream(\io\streams\InputStream $in) {
return new \io\streams\InflatingInputStream($in);
public function getDecompressionStream(InputStream $in) {
return new InflatingInputStream($in);
}
}');
self::$BZ= newinstance(__CLASS__, [12, 'BZ'], '{
};
self::$BZ= new class(12, 'BZ') extends Compression {
static function __static() { }

public function getCompressionStream(\io\streams\OutputStream $out, $level= 6) {
return new \io\streams\Bz2CompressingOutputStream($out, $level);
public function getCompressionStream(OutputStream $out, $level= 6) {
return new Bz2CompressingOutputStream($out, $level);
}

public function getDecompressionStream(\io\streams\InputStream $in) {
return new \io\streams\Bz2DecompressingInputStream($in);
public function getDecompressionStream(InputStream $in) {
return new Bz2DecompressingInputStream($in);
}
}');
};
}

/**
Expand All @@ -82,15 +89,15 @@ public function getDecompressionStream(\io\streams\InputStream $in) {
* @return io.streams.OutputStream
* @throws lang.IllegalArgumentException if the level is not between 0 and 9
*/
public abstract function getCompressionStream(\io\streams\OutputStream $out, $level= 6);
public abstract function getCompressionStream(OutputStream $out, $level= 6);

/**
* Gets decompression stream. Implemented in members.
*
* @param io.streams.InputStream in
* @return io.streams.InputStream
*/
public abstract function getDecompressionStream(\io\streams\InputStream $in);
public abstract function getDecompressionStream(InputStream $in);

/**
* Get a compression instance by a given id
Expand All @@ -104,7 +111,7 @@ public static function getInstance($n) {
case 0: return self::$NONE;
case 8: return self::$GZ;
case 12: return self::$BZ;
default: throw new \lang\IllegalArgumentException('Unknown compression algorithm #'.$n);
default: throw new IllegalArgumentException('Unknown compression algorithm #'.$n);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class ZipArchiveReaderTest extends AbstractZipFileTest {

#[@test]
public function close() {
$stream= newinstance(InputStream::class, [], '{
public $closed= FALSE;
public function read($limit= 8192) { return ""; }
$stream= new class() implements InputStream {
public $closed= false;
public function read($limit= 8192) { return ''; }
public function available() { return 0; }
public function close() { $this->closed= TRUE; }
}');
public function close() { $this->closed= true; }
};
$reader= new \io\archive\zip\ZipArchiveReader($stream);
$reader->close();
$this->assertTrue($stream->closed);
Expand Down

0 comments on commit 221f7c2

Please sign in to comment.