11<?php
22namespace 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+ */
415class 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 ();
0 commit comments