From 356e70de48745b0f030bf0fdd8d8de5f8d8c1ca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Wed, 21 Oct 2020 21:11:02 +0300 Subject: [PATCH] Encode dot in MongoDB saver as underscore --- src/Xhgui/Saver/MongoSaver.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Xhgui/Saver/MongoSaver.php b/src/Xhgui/Saver/MongoSaver.php index 1dd38cc4e..f384fc6cc 100644 --- a/src/Xhgui/Saver/MongoSaver.php +++ b/src/Xhgui/Saver/MongoSaver.php @@ -40,11 +40,34 @@ public function save(array $data): string $a = [ '_id' => $id, 'meta' => $meta, - 'profile' => $data['profile'], + 'profile' => $this->encodeProfile($data['profile']), ]; $this->_collection->insert($a, ['w' => 0]); return (string)$id; } + + /** + * MongoDB can't save keys with values containing a dot: + * + * InvalidArgumentException: invalid document for insert: keys cannot contain ".": + * "Zend_Controller_Dispatcher_Standard::loadClass==>load::controllers/ArticleController.php" + * + * Replace the dots with underscrore in keys. + * + * @link https://github.com/perftools/xhgui/issues/209 + */ + private function encodeProfile(array $profile): array + { + $results = []; + foreach ($profile as $k => $v) { + if (strpos($k, '.') !== false) { + $k = str_replace('.', '_', $k); + } + $results[$k] = $v; + } + + return $results; + } }