Skip to content

Commit 959ef13

Browse files
committed
Added phpdoc comments for CompressedStringList and some notes for my forked GzStreamGuzzle class
1 parent 0b68e40 commit 959ef13

File tree

3 files changed

+105
-18
lines changed

3 files changed

+105
-18
lines changed

src/CompressedString.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ public function getPath()
147147
* in these situations you may use the optional $options
148148
* and $depth parameters.
149149
*
150-
* Returns the current size (in bytes) of the uncompressed data that has
151-
* been written so far.
152-
*
153150
* @param mixed $string
154151
* @param int $options
155152
* @param int $depth
@@ -215,7 +212,7 @@ public function read($length = 65536)
215212
public function prepend($string, $compressionLevel = 6)
216213
{
217214
$this->prepareForRead();
218-
$gzStreamReadOnly = $this->getGzStream()->readOnlyStream();
215+
$gzStreamReadOnly = $this->getGzStream()->getReadOnlyCopy();
219216

220217
$this->replaceStream(false, $compressionLevel, 'php://memory');
221218

@@ -374,20 +371,23 @@ public function getCompressedReadOnlyStream()
374371
}
375372

376373
/**
377-
* put your comment there...
374+
* Returns a read-only stream that can be used
375+
* for reading the decompressed contents back out
376+
* as a stream.
378377
*
378+
* @return GzStreamGuzzle
379379
*/
380380
public function getDecompressedReadOnlyStream()
381381
{
382382
if ($this->isRealFile && $this->isReadOnly()) {
383383
return $this->getGzStream();
384384
} elseif ($this->isRealFile) {
385-
return $this->getGzStream()->readOnlyStream();
385+
return $this->getGzStream()->getReadOnlyCopy();
386386
}
387387

388388
// More specifically for the in-memory streams:
389389
$this->prepareForRead();
390-
$gzStreamReadOnly = $this->getGzStream()->readOnlyStream();
390+
$gzStreamReadOnly = $this->getGzStream()->getReadOnlyCopy();
391391

392392
return $gzStreamReadOnly;
393393
}

src/CompressedStringList.php

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,68 @@
11
<?php
22
namespace Orware\Compressed;
33

4+
/**
5+
* Compressed String List Class
6+
*
7+
* Allows you to queue up multiple instances of CompressedString
8+
* so that you can use later or with the merge method, which allows
9+
* a new compressed string to be created with the combined output.
10+
*
11+
* e.g. Merging one or more compressed JSON strings of database results
12+
* into a JSON object containing metadata.
13+
*
14+
*/
415
class CompressedStringList
516
{
17+
/**
18+
* The internal SplQueue instance
19+
*
20+
* @var \SplQueue
21+
*/
622
protected $queue = null;
723

8-
public function __construct()
24+
/**
25+
* Creates the CompressedStringList
26+
*
27+
* By default it will not keep copies of the
28+
* queued objects during iteration in order to
29+
* conserve memory usage.
30+
*
31+
*/
32+
public function __construct($keep = false)
933
{
1034
$this->queue = new \SplQueue();
11-
$this->queue->setIteratorMode(\SplDoublyLinkedList::IT_MODE_FIFO | \SplDoublyLinkedList::IT_MODE_DELETE);
35+
$mode = \SplDoublyLinkedList::IT_MODE_DELETE;
36+
if ($keep) {
37+
$mode = \SplDoublyLinkedList::IT_MODE_KEEP;
38+
}
39+
$this->queue->setIteratorMode(\SplDoublyLinkedList::IT_MODE_FIFO | $mode);
1240
}
1341

14-
public static function merge($subject, $delimiter, CompressedStringList $gzippedStrings, $compressionLevel = 6, $addQuotesToDelimiter = false)
42+
/**
43+
* Merges multiple CompressedStrings contained
44+
* in a CompressedStringList into the $subject
45+
* string based on the locations of the $delimiter.
46+
*
47+
* The subject will normally be a string, but you may also
48+
* provide an array or object and it will be JSON encoded and then
49+
* split on the provided delimiter.
50+
*
51+
* You may optionally provide a new compression level
52+
* and make use of the option to automatically add quotes
53+
* to the delimiter (useful for when the delimiter has been added
54+
* to text that was then JSON encoded, otherwise your inserted text might
55+
* included additional quotes around it that you won't want).
56+
*
57+
* @param mixed $subject
58+
* @param string $delimiter
59+
* @param CompressedStringList $compressedStringList
60+
* @param int $compressionLevel
61+
* @param bool $addQuotesToDelimiter
62+
*
63+
* @return CompressedString
64+
*/
65+
public static function merge($subject, $delimiter, CompressedStringList $compressedStringList, $compressionLevel = 6, $addQuotesToDelimiter = false)
1566
{
1667
if (!is_string($subject)) {
1768
$subject = json_encode($subject);
@@ -27,8 +78,8 @@ public static function merge($subject, $delimiter, CompressedStringList $gzipped
2778

2879
foreach ($subjectParts as $part) {
2980
$merged->write($part);
30-
if (!$gzippedStrings->isEmpty()) {
31-
$string = $gzippedStrings->dequeue();
81+
if (!$compressedStringList->isEmpty()) {
82+
$string = $compressedStringList->dequeue();
3283

3384
$readStream = $string->getDecompressedReadOnlyStream();
3485
while ($buffer = $readStream->read()) {
@@ -40,21 +91,43 @@ public static function merge($subject, $delimiter, CompressedStringList $gzipped
4091
return $merged;
4192
}
4293

94+
/**
95+
* Add a CompressedString to the Queue.
96+
*
97+
* @param CompressedString $string
98+
*
99+
* @return void
100+
*/
43101
public function enqueue(CompressedString $string)
44102
{
45103
return $this->queue->enqueue($string);
46104
}
47105

106+
/**
107+
* Returns a CompressedString from the Queue
108+
*
109+
* @return CompressedString
110+
*/
48111
public function dequeue()
49112
{
50113
return $this->queue->dequeue();
51114
}
52115

116+
/**
117+
* Checks whether the list is empty
118+
*
119+
* @return bool
120+
*/
53121
public function isEmpty()
54122
{
55123
return $this->queue->isEmpty();
56124
}
57125

126+
/**
127+
* Returns the number of entries in the Queue
128+
*
129+
* @return int
130+
*/
58131
public function count()
59132
{
60133
return $this->queue->count();

src/GzStreamGuzzle.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
66
use GuzzleHttp\Psr7\Stream;
77
use GuzzleHttp\Psr7\StreamWrapper;
88

9+
/**
10+
* Modified version of the original class created by Tom Westcott that can be found here:
11+
* https://github.com/cyberdummy/gzstream/blob/master/src/GzStreamGuzzle.php
12+
*
13+
* Mainly the issue for me was that the original class had its internal properties
14+
* and certain methods (writeFooter for example) set as private, which made
15+
* some of the things I wanted to use the class for, more difficult.
16+
*
17+
* I've integrated this modified version of the class into this package.
18+
*
19+
* Changes made:
20+
* - Convert properties to protected
21+
* - Changed private methods to protected
22+
* - Added ability to specify Gzip compression level
23+
* - Added getReadOnlyCopy() method
24+
* - Added writeFooterEarly() method
25+
* - Added default read() length
26+
* - Added getWriteSize() method
27+
*/
928
class GzStreamGuzzle implements StreamInterface
1029
{
1130
use StreamDecoratorTrait;
@@ -40,16 +59,11 @@ public function __construct(StreamInterface $stream, $readOnly = false, $level =
4059
}
4160
}
4261

43-
public function readOnlyStream()
62+
public function getReadOnlyCopy()
4463
{
4564
return new self($this->stream, true, $this->level);
4665
}
4766

48-
public function writeableStream()
49-
{
50-
return new self($this->stream, false, $this->level);
51-
}
52-
5367
public function read($length = 65536)
5468
{
5569
$ret = $this->stream->read($length);

0 commit comments

Comments
 (0)