Skip to content

Commit

Permalink
Encode dot in MongoDB saver as underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Oct 21, 2020
1 parent 9d02c13 commit 356e70d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Xhgui/Saver/MongoSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 356e70d

Please sign in to comment.