]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/ReversedFileReader.php
Fix image grid in shared Friendica posts
[friendica.git] / src / Util / ReversedFileReader.php
index 64d41b25d66115763006cd1d8d7828a6273c26fd..2ef062152c9e590203c23ad499b009f60c82335d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2021, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -51,16 +51,16 @@ class ReversedFileReader implements \Iterator
 
        /**
         * Open $filename for read and reset iterator
-        * 
+        *
         * @param string $filename      File to open
         * @return $this
         */
-       public function open(string $filename)
+       public function open(string $filename): ReversedFileReader
        {
                $this->fh = fopen($filename, 'r');
                if (!$this->fh) {
                        // this should use a custom exception.
-                       throw \Exception("Unable to open $filename");
+                       throw new \Exception("Unable to open $filename");
                }
                $this->filesize = filesize($filename);
                $this->pos      = -1;
@@ -70,13 +70,25 @@ class ReversedFileReader implements \Iterator
                return $this;
        }
 
-       private function _read($size)
+       /**
+        * Read $size bytes behind last position
+        *
+        * @param int $size
+        * @return string
+        */
+       private function _read(int $size): string
        {
                $this->pos -= $size;
                fseek($this->fh, $this->pos);
                return fread($this->fh, $size);
        }
 
+       /**
+        * Read next line from end of file
+        * Return null if no lines are left to read
+        *
+        * @return string|null Depending on data being buffered
+        */
        private function _readline()
        {
                $buffer = & $this->buffer;
@@ -84,6 +96,9 @@ class ReversedFileReader implements \Iterator
                        if ($this->pos == 0) {
                                return array_pop($buffer);
                        }
+                       if (is_null($buffer)) {
+                               return null;
+                       }
                        if (count($buffer) > 1) {
                                return array_pop($buffer);
                        }
@@ -91,12 +106,24 @@ class ReversedFileReader implements \Iterator
                }
        }
 
+       /**
+        * Fetch next line from end and set it as current iterator value.
+        *
+        * @see Iterator::next()
+        * @return void
+        */
        public function next()
        {
                ++$this->key;
                $this->value = $this->_readline();
        }
 
+       /**
+        * Rewind iterator to the first line at the end of file
+        *
+        * @see Iterator::rewind()
+        * @return void
+        */
        public function rewind()
        {
                if ($this->filesize > 0) {
@@ -108,17 +135,35 @@ class ReversedFileReader implements \Iterator
                }
        }
 
-       public function key()
+       /**
+        * Return current line number, starting from zero at the end of file
+        *
+        * @see Iterator::key()
+        * @return int
+        */
+       public function key(): int
        {
                return $this->key;
        }
 
-       public function current()
+       /**
+        * Return current line
+        *
+        * @see Iterator::current()
+        * @return string
+        */
+       public function current(): string
        {
                return $this->value;
        }
 
-       public function valid()
+       /**
+        * Checks if current iterator value is valid, that is, we readed all lines in files
+        *
+        * @see Iterator::valid()
+        * @return bool
+        */
+       public function valid(): bool
        {
                return ! is_null($this->value);
        }