Skip to content

Commit 08d6f91

Browse files
committed
Renaming my String class to CompressedString to avoid any PHP 7 issues.
1 parent d022c43 commit 08d6f91

File tree

7 files changed

+98
-109
lines changed

7 files changed

+98
-109
lines changed

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Usage
2222

2323
It's primarily intended to be used in a write forward way (primarily because going back to the beginning of a gzip string requires it to be decoded, so prepending should be discouraged from excessive use), but you do have the option to prepend text when needed:
2424
```php
25-
use Orware\Compressed\String;
25+
use Orware\Compressed\CompressedString;
2626

27-
$compressedString = new String();
27+
$compressedString = new CompressedString();
2828

2929
// You may write multiple times:
3030
$content = 'The quick brown fox jumps over the lazy dog';
@@ -33,7 +33,7 @@ $compressedString->write($content);
3333
$moreContent = 'The quick brown fox jumps over the lazy dog';
3434
$compressedString->write($moreContent);
3535

36-
// You can prepend text as well
36+
// You can prepend text as well
3737
// (currently this involves creating a new stream, adding the prepended text, then copying the existing stream into the new stream):
3838
$textToPrepend = 'PREPENDED';
3939
$compressedString->prepend($textToPrepend);
@@ -59,38 +59,37 @@ $compressedString->writeCompressedContents('tests/files/appended_test_compressed
5959

6060
There's also the ability to merge in one or more compressed strings into a "wrapper" string. In my case my wrapper contained some metadata about the JSON results.
6161
```php
62-
use Orware\Compressed\String;
63-
use Orware\Compressed\StringList;
64-
use Orware\Compressed\StringMerge;
62+
use Orware\Compressed\CompressedString;
63+
use Orware\Compressed\CompressedStringList;
6564

66-
$compressedString1 = new String();
65+
$compressedString1 = new CompressedString();
6766
$content = 'My first string';
6867
$compressedString1->write($content);
6968

70-
$compressedString2 = new String();
69+
$compressedString2 = new CompressedString();
7170
$content = 'My second string';
7271
$compressedString2->write($content);
7372

74-
$compressedString3 = new String();
73+
$compressedString3 = new CompressedString();
7574
$content = 'My third string';
7675
$compressedString3->write($content);
7776

7877
// You must use this StringList class (it's what the merge call below expects):
79-
$list = new StringList();
78+
$list = new CompressedStringList();
8079

8180
$list->enqueue($compressedString1);
8281
$list->enqueue($compressedString2);
8382
$list->enqueue($compressedString3);
8483

85-
// The default placeholder is #|_|#
84+
// The default placeholder is #|_|#
8685
// Each instance of that placeholder below will get replaced:
8786
$subject = '{"string1":"#|_|#","string2":"#|_|#","string3":"#|_|#"}';
8887

8988
// The end result is a new compressed string.
90-
// Depending on the size of your compressed strings execution
91-
// time may go up during a merge since each has to be decoded
89+
// Depending on the size of your compressed strings execution
90+
// time may go up during a merge since each has to be decoded
9291
// and compressed again when merged into the new string.
93-
$mergedString = StringMerge::merge($subject, '#|_|#', $list);
92+
$mergedString = CompressedStringList::merge($subject, '#|_|#', $list);
9493

9594
```
9695

src/String.php renamed to src/CompressedString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use GuzzleHttp\Psr7\Stream;
88
use GuzzleHttp\Psr7\StreamWrapper;
99

10-
class String
10+
class CompressedString
1111
{
1212
protected $gzStream = null;
1313
protected $stream = null;

src/CompressedStringList.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
namespace Orware\Compressed;
3+
4+
class CompressedStringList
5+
{
6+
protected $queue = null;
7+
8+
public function __construct()
9+
{
10+
$this->queue = new \SplQueue();
11+
$this->queue->setIteratorMode(\SplDoublyLinkedList::IT_MODE_FIFO | \SplDoublyLinkedList::IT_MODE_DELETE);
12+
}
13+
14+
public static function merge($subject, $delimiter, CompressedStringList $gzippedStrings)
15+
{
16+
if (!is_string($subject)) {
17+
$subject = json_encode($subject);
18+
}
19+
20+
$subjectParts = explode($delimiter, $subject);
21+
22+
$merged = new CompressedString();
23+
24+
foreach ($subjectParts as $part) {
25+
$merged->write($part);
26+
if (!$gzippedStrings->isEmpty()) {
27+
$string = $gzippedStrings->dequeue();
28+
29+
$readStream = $string->getReadOnlyStream();
30+
while ($buffer = $readStream->read(4096)) {
31+
$merged->write($buffer);
32+
}
33+
}
34+
}
35+
36+
return $merged;
37+
}
38+
39+
public function enqueue(CompressedString $string)
40+
{
41+
return $this->queue->enqueue($string);
42+
}
43+
44+
public function dequeue()
45+
{
46+
return $this->queue->dequeue();
47+
}
48+
49+
public function isEmpty()
50+
{
51+
return $this->queue->isEmpty();
52+
}
53+
54+
public function count()
55+
{
56+
return $this->queue->count();
57+
}
58+
}

src/StringList.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/StringMerge.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/StringMergeTest.php renamed to tests/CompressedStringMergeTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2-
use Orware\Compressed\String;
3-
use Orware\Compressed\StringList;
2+
use Orware\Compressed\CompressedString;
3+
use Orware\Compressed\CompressedStringList;
44
use Orware\Compressed\StringMerge;
55

6-
class StringMergeTest extends \PHPUnit_Framework_TestCase
6+
class CompressedStringMergeTest extends \PHPUnit_Framework_TestCase
77
{
88
public function memoryUsage($method, $stage = '')
99
{
@@ -26,27 +26,27 @@ public function testMerge()
2626
{
2727
$this->memoryUsage(__METHOD__, '');
2828

29-
$compressedString1 = new String();
29+
$compressedString1 = new CompressedString();
3030
$content = 'My first string';
3131
$compressedString1->write($content);
3232

33-
$compressedString2 = new String();
33+
$compressedString2 = new CompressedString();
3434
$content = 'My second string';
3535
$compressedString2->write($content);
3636

37-
$compressedString3 = new String();
37+
$compressedString3 = new CompressedString();
3838
$content = 'My third string';
3939
$compressedString3->write($content);
4040

41-
$list = new StringList();
41+
$list = new CompressedStringList();
4242

4343
$list->enqueue($compressedString1);
4444
$list->enqueue($compressedString2);
4545
$list->enqueue($compressedString3);
4646

4747
$subject = '{"string1":"#|_|#","string2":"#|_|#","string3":"#|_|#"}';
4848

49-
$mergedString = StringMerge::merge($subject, '#|_|#', $list);
49+
$mergedString = CompressedStringList::merge($subject, '#|_|#', $list);
5050

5151
$expected = '{"string1":"My first string","string2":"My second string","string3":"My third string"}';
5252
$this->assertEquals($expected, $mergedString->getDecompressedContents());
@@ -56,7 +56,7 @@ public function testLargeMergeIntoString()
5656
{
5757
$this->memoryUsage(__METHOD__, 'Start');
5858

59-
$compressedString1 = new String();
59+
$compressedString1 = new CompressedString();
6060
$handle = fopen('tests/files/companies_first_10.json', "r");
6161
if ($handle) {
6262
while (($buffer = fgets($handle, 4096)) !== false) {
@@ -66,7 +66,7 @@ public function testLargeMergeIntoString()
6666
fclose($handle);
6767
}
6868

69-
$compressedString2 = new String();
69+
$compressedString2 = new CompressedString();
7070
$handle = fopen('tests/files/companies_first_10.json', "r");
7171
if ($handle) {
7272
while (($buffer = fgets($handle, 4096)) !== false) {
@@ -78,12 +78,12 @@ public function testLargeMergeIntoString()
7878

7979
$subject = '[{"queryType":"SELECT","rowCount":4178,"executionTimeMilliseconds":4764.52,"executionTimeSeconds":4.76,"memoryUsageBytes":323344,"memoryUsageMegabytes":0.31,"cachedResponse":false,"data":#|_|#,"error":false},{"queryType":"SELECT","rowCount":4178,"executionTimeMilliseconds":4764.52,"executionTimeSeconds":4.76,"memoryUsageBytes":323344,"memoryUsageMegabytes":0.31,"cachedResponse":false,"data":#|_|#,"error":false}]';
8080

81-
$list = new StringList();
81+
$list = new CompressedStringList();
8282

8383
$list->enqueue($compressedString1);
8484
$list->enqueue($compressedString2);
8585

86-
$mergedString = StringMerge::merge($subject, '#|_|#', $list);
86+
$mergedString = CompressedStringList::merge($subject, '#|_|#', $list);
8787

8888
$this->log("Merged String Size is: " . $mergedString->getCompressedSize());
8989

@@ -99,7 +99,7 @@ public function testLargeMergeIntoObject()
9999
{
100100
$this->memoryUsage(__METHOD__, 'Start');
101101

102-
$compressedString1 = new String();
102+
$compressedString1 = new CompressedString();
103103
$handle = fopen('tests/files/companies_first_10.json', "r");
104104
if ($handle) {
105105
while (($buffer = fgets($handle, 4096)) !== false) {
@@ -109,7 +109,7 @@ public function testLargeMergeIntoObject()
109109
fclose($handle);
110110
}
111111

112-
$compressedString2 = new String();
112+
$compressedString2 = new CompressedString();
113113
$handle = fopen('tests/files/companies_first_10.json', "r");
114114
if ($handle) {
115115
while (($buffer = fgets($handle, 4096)) !== false) {
@@ -146,12 +146,12 @@ public function testLargeMergeIntoObject()
146146

147147
$subject[] = $queryObject2;
148148

149-
$list = new StringList();
149+
$list = new CompressedStringList();
150150

151151
$list->enqueue($compressedString1);
152152
$list->enqueue($compressedString2);
153153

154-
$mergedString = StringMerge::merge($subject, '#|_|#', $list);
154+
$mergedString = CompressedStringList::merge($subject, '#|_|#', $list);
155155

156156
// Actual size should be less than 80,000 bytes:
157157
$this->assertLessThanOrEqual(80000, $mergedString->getCompressedSize());

tests/StringTest.php renamed to tests/CompressedStringTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2-
use Orware\Compressed\String;
2+
use Orware\Compressed\CompressedString;
33

4-
class StringTest extends \PHPUnit_Framework_TestCase
4+
class CompressedStringTest extends \PHPUnit_Framework_TestCase
55
{
66
/*public function testReadStream()
77
{
@@ -30,7 +30,7 @@ public function log($string, $newline = true)
3030

3131
public function testWriteStream()
3232
{
33-
$compressedString = new String();
33+
$compressedString = new CompressedString();
3434

3535
$content = 'The quick brown fox jumps over the lazy dog';
3636
$compressedString->write($content);
@@ -40,7 +40,7 @@ public function testWriteStream()
4040

4141
public function testMixedWriteStream()
4242
{
43-
$compressedString = new String();
43+
$compressedString = new CompressedString();
4444

4545
$content = 'The quick brown fox jumps over the lazy dog';
4646
$compressedString->write($content);
@@ -60,7 +60,7 @@ public function testPrependStream()
6060
{
6161
$this->memoryUsage(__METHOD__, 'Start');
6262

63-
$compressedString = new String();
63+
$compressedString = new CompressedString();
6464

6565
$content = 'The quick brown fox jumps over the lazy dog';
6666
$compressedString->write($content);
@@ -78,7 +78,7 @@ public function testPrependStream()
7878

7979
public function testPrependAndWriteStream()
8080
{
81-
$compressedString = new String();
81+
$compressedString = new CompressedString();
8282

8383
$content = 'The quick brown fox jumps over the lazy dog';
8484
$compressedString->write($content);
@@ -98,10 +98,10 @@ public function testDifferentCompressionModes()
9898
{
9999
$content = file_get_contents('README.md');
100100

101-
$compressedString1 = new String(false, 1);
101+
$compressedString1 = new CompressedString(false, 1);
102102
$compressedString1->write($content);
103103

104-
$compressedString2 = new String(false, 6);
104+
$compressedString2 = new CompressedString(false, 6);
105105
$compressedString2->write($content);
106106

107107
$size1 = $compressedString1->getCompressedSize();
@@ -118,7 +118,7 @@ public function testDifferentCompressionModes()
118118
public function testReadGzipFile()
119119
{
120120
$this->memoryUsage(__METHOD__, 'Start');
121-
$compressedStringFile = new String(true, 6, __DIR__.'/files/companies_first_10.gz');
121+
$compressedStringFile = new CompressedString(true, 6, __DIR__.'/files/companies_first_10.gz');
122122

123123
$readOnlyStream = $compressedStringFile->getReadOnlyStream();
124124
$i = 0;
@@ -141,7 +141,7 @@ public function testWriteFileAndReadStream()
141141
{
142142
$this->memoryUsage(__METHOD__, 'Start');
143143

144-
$compressedString = new String();
144+
$compressedString = new CompressedString();
145145

146146
$content = file_get_contents(__DIR__.'/files/companies_first_10.json');
147147
$compressedString->write($content);

0 commit comments

Comments
 (0)