X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FLog%2FParsedLogIterator.php;h=a45b67176961b47f74ded3fb5f5fc3d54dcb6b5d;hb=360614d2cf3aceeb763ef1281ad5236878f5d735;hp=3e1832d969998cc46e10c200fd0e6cbd7119da55;hpb=84fa6688459ca9ff22771ffccca1c8097aa54b27;p=friendica.git diff --git a/src/Model/Log/ParsedLogIterator.php b/src/Model/Log/ParsedLogIterator.php index 3e1832d969..a45b671769 100644 --- a/src/Model/Log/ParsedLogIterator.php +++ b/src/Model/Log/ParsedLogIterator.php @@ -1,6 +1,6 @@ reader = $reader; + } + /** * @param string $filename File to open + * @return $this + */ + public function open(string $filename): ParsedLogIterator + { + $this->reader->open($filename); + return $this; + } + + /** * @param int $limit Max num of lines to read - * @param array $filter filters per column - * @param string $search string to search to filter lines + * @return $this */ - public function __construct(string $filename, int $limit = 0, array $filters = [], string $search = "") + public function withLimit(int $limit): ParsedLogIterator + { + $this->limit = $limit; + return $this; + } + + /** + * @param array $filters filters per column + * @return $this + */ + public function withFilters(array $filters): ParsedLogIterator { - $this->reader = new ReversedFileReader($filename); - $this->value = null; - $this->limit = $limit; $this->filters = $filters; - $this->search = $search; + return $this; + } + + /** + * @param string $search string to search to filter lines + * @return $this + */ + public function withSearch(string $search): ParsedLogIterator + { + $this->search = $search; + return $this; } /** * Check if parsed log line match filters. * Always match if no filters are set. * - * @param ParsedLog $parsedlog - * @return bool + * @param ParsedLogLine $parsedlogline ParsedLogLine instance + * @return bool Wether the parse log line matches */ - private function filter($parsedlog) + private function filter(ParsedLogLine $parsedlogline): bool { $match = true; foreach ($this->filters as $filter => $filtervalue) { switch ($filter) { - case "level": - $match = $match && ($parsedlog->level == strtoupper($filtervalue)); + case 'level': + $match = $match && ($parsedlogline->level == strtoupper($filtervalue)); break; - case "context": - $match = $match && ($parsedlog->context == $filtervalue); + + case 'context': + $match = $match && ($parsedlogline->context == $filtervalue); break; } } @@ -90,13 +124,13 @@ class ParsedLogIterator implements \Iterator * Check if parsed log line match search. * Always match if no search query is set. * - * @param ParsedLog $parsedlog + * @param ParsedLogLine $parsedlogline * @return bool */ - private function search($parsedlog) + private function search(ParsedLogLine $parsedlogline): bool { - if ($this->search != "") { - return strstr($parsedlog->logline, $this->search) !== false; + if ($this->search != '') { + return strstr($parsedlogline->logline, $this->search) !== false; } return true; } @@ -105,8 +139,7 @@ class ParsedLogIterator implements \Iterator * Read a line from reader and parse. * Returns null if limit is reached or the reader is invalid. * - * @param ParsedLog $parsedlog - * @return ?ParsedLog + * @return ?ParsedLogLine */ private function read() { @@ -116,41 +149,72 @@ class ParsedLogIterator implements \Iterator } $line = $this->reader->current(); - return new ParsedLog($this->reader->key(), $line); + return new ParsedLogLine($this->reader->key(), $line); } - public function next() + + /** + * Fetch next parsed log line which match with filters or search and + * set it as current iterator value. + * + * @see Iterator::next() + * @return void + */ + public function next(): void { $parsed = $this->read(); - // if read() has not retuned none and - // the line don't match filters or search - // read the next line while (is_null($parsed) == false && !($this->filter($parsed) && $this->search($parsed))) { $parsed = $this->read(); } $this->value = $parsed; } - public function rewind() + + /** + * Rewind the iterator to the first matching log line + * + * @see Iterator::rewind() + * @return void + */ + public function rewind(): void { $this->value = null; $this->reader->rewind(); $this->next(); } - public function key() + /** + * Return current parsed log line number + * + * @see Iterator::key() + * @see ReversedFileReader::key() + * @return int + */ + public function key(): int { return $this->reader->key(); } - public function current() + /** + * Return current iterator value + * + * @see Iterator::current() + * @return ?ParsedLogLine + */ + public function current(): ?ParsedLogLine { return $this->value; } - public function valid() + /** + * Checks if current iterator value is valid, that is, not null + * + * @see Iterator::valid() + * @return bool + */ + public function valid(): bool { - return ! is_null($this->value); + return !is_null($this->value); } }