From 05ab7506959e6290629b7b8a8132d5fabfe31bc6 Mon Sep 17 00:00:00 2001 From: inhere Date: Thu, 24 Oct 2019 14:38:01 +0800 Subject: [PATCH] update mem table class --- src/MemTable.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/MemTable.php b/src/MemTable.php index 71f4ff4..34a6145 100644 --- a/src/MemTable.php +++ b/src/MemTable.php @@ -180,7 +180,7 @@ public function set(string $key, array $data): bool * @param string $key Index key * @param string $field Filed name of Index * - * @return array|false Will return an array when success, return false when failure + * @return array|false|mixed Will return an array when success, return false when failure * @throws RuntimeException */ public function get(string $key, string $field = null) @@ -256,6 +256,16 @@ public function count(): int * Extra methods ****************************************************************************/ + /** + * @param callable $fn + */ + public function each(callable $fn): void + { + foreach ($this->table as $row) { + $fn($row); + } + } + /** * clear/flush table data */ @@ -292,7 +302,9 @@ public function restore(bool $coRead = false): void $content = file_get_contents($file); } - $this->load((array)json_decode($content, true)); + if ($content) { + $this->load((array)json_decode($content, true)); + } } /** @@ -330,7 +342,6 @@ public function load(array $data): void } } - /***************************************************************************** * Getter/Setter methods ****************************************************************************/