Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-209: Encode keys before storing on mongo. #210

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Xhgui/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ public function __construct($profile, $convert = true)
}
}

/**
* Decodes the profile array by removing unicode replacements.
*
* @param array $profile
*
* @return array
*/
protected function decode($profile)
{
if (!is_array($profile) || !isset($profile['__encoded'])) {
return $profile;
}
$target = array();
foreach ($profile as $k => $v) {
if ($k === '__encoded') {
continue;
}
if (is_array($v)) {
$v = $this->decode($v);
}
$replacementKey = strtr($k, array(
'.' => '.',
));
$target[$replacementKey] = $v;
}
return $target;
}

/**
* Convert the raw data into a flatter list that is easier to use.
*
Expand All @@ -39,6 +67,7 @@ public function __construct($profile, $convert = true)
*/
protected function _process()
{
$this->_data['profile'] = $this->decode($this->_data['profile']);
$result = array();
foreach ($this->_data['profile'] as $name => $values) {
list($parent, $func) = $this->splitName($name);
Expand Down
27 changes: 27 additions & 0 deletions src/Xhgui/Profiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,32 @@ public function getAll($options = array())
return $this->paginate($options);
}

/**
* Encodes a profile to avoid mongodb key errors.
* @param array $profile
*
* @return array
*/
protected function encodeProfile($profile)
{
if (!is_array($profile)) {
return $profile;
}
$target = array(
'__encoded' => true,
);
foreach ($profile as $k => $v) {
if (is_array($v)) {
$v = $this->encodeProfile($v);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this recursive call? do we have any other problematic keys than the content of array_keys($profile['profile'])?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any nested structures inside a profile that I have ignored?

Copy link
Contributor

@lauripiisang lauripiisang Oct 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the case that we do not need recursive checking, we should instead opt for an easier solution.

What do you think about something like

public function encode($profile) {
  // return $profile if the array is empty or not an array
  $replacedKeys = str_replace('.', '', array_keys($profile));
  return array_combine($replacedKeys, $profile);
}

?

Here's a demonstration, if you're intersted

<?php
$a = ['fx' => ['a' => 'xxx', 'b' => 2, 'c' => 3], 'quux' => ['f' => 7, 'cx' => 3]];
print_r(array_combine(str_replace('x', 'y', array_keys($a)), $a));

prints

Array
(
    [fy] => Array
        (
            [a] => xxx
            [b] => 2
            [c] => 3
        )

    [quuy] => Array
        (
            [f] => 7
            [cx] => 3
        )

)

Copy link
Contributor

@lauripiisang lauripiisang Oct 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also recommend defining the . and as constants (UNICODE_PERIOD and UNICODE_FULLWIDTH_PERIOD for example?)

}
$replacementKey = strtr($k, array(
'.' => '.',
));
$target[$replacementKey] = $v;
}
return $target;
}

/**
* Insert a profile run.
*
Expand All @@ -261,6 +287,7 @@ public function getAll($options = array())
*/
public function insert($profile)
{
$profile['profile'] = $this->encodeProfile($profile['profile']);
return $this->_collection->insert($profile, array('w' => 0));
}

Expand Down