Skip to content

Commit

Permalink
fix(Saver): Mlaformed UTF8 char
Browse files Browse the repository at this point in the history
fix(Saver): Mlaformed UTF8 char
  • Loading branch information
4513 committed Apr 11, 2024
1 parent 40edd32 commit 81f23c1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/Saver/FileSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,20 @@ public function isSupported()

public function save(array $data)
{
$json = json_encode($data);
return file_put_contents($this->file, $this->encodeData($data) . PHP_EOL, FILE_APPEND);
}

/**
* @param array $data
*
* @return string
*/
private function encodeData(array $data)
{
$result = PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 2
? json_encode($data, JSON_INVALID_UTF8_IGNORE)
: json_encode($data);

return file_put_contents($this->file, $json . PHP_EOL, FILE_APPEND);
return $result === false ? '' : $result;
}
}
17 changes: 15 additions & 2 deletions src/Saver/UploadSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public function isSupported()

public function save(array $data)
{
$json = json_encode($data);
$this->submit($this->url, $json);
$this->submit($this->url, $this->encodeData($data));

return true;
}
Expand Down Expand Up @@ -83,4 +82,18 @@ private function submit($url, $payload)
throw new ProfilerException($message);
}
}

/**
* @param array $data
*
* @return string
*/
private function encodeData(array $data)
{
$result = PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 2
? json_encode($data, JSON_INVALID_UTF8_IGNORE)
: json_encode($data);

return $result === false ? '' : $result;
}
}

0 comments on commit 81f23c1

Please sign in to comment.