-
-
Notifications
You must be signed in to change notification settings - Fork 453
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
[2.0] Serializer interface #454
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ | |
* | ||
* @package raven | ||
*/ | ||
class Serializer | ||
class Serializer implements SerializerInterface | ||
{ | ||
/* | ||
* The default mb detect order | ||
|
@@ -62,6 +62,33 @@ public function __construct($mb_detect_order = null) | |
} | ||
} | ||
|
||
/** | ||
* Serialize an object (recursively) into something safe for data | ||
* sanitization and encoding. | ||
* | ||
* @param mixed $value | ||
* @param array $context | ||
* @return string|bool|double|int|null|object|array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing documentation, and return value should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got this doc from this file itself. Do we want to change all serializing method to "return mixed" INCLUDING There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would, because it's prettier imho There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Pretiest" ways are always wrong in our art. This is the programming not a drawing Mona Lisa There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accept — of course. But we are talking about param and return types, aren't we? Of course we should change param types to "mixed". I will do it. But what about return types? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return types should be only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait a minute
There is no other doc items which contain some long string with types. Only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wrong in saying that the return value of the function should be that because the return types can only be scalars. I would use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't know what should I write. And I am afraid that my line should be corrected because english is not my native language. I know it isn't yours either. |
||
*/ | ||
public function serialize($value, array $context = []) | ||
{ | ||
$full_context = $this->get_full_context($context); | ||
return $this->serializeInner($value, $full_context['max_depth'], 0); | ||
} | ||
|
||
/** | ||
* @param array $context | ||
* @return array | ||
*/ | ||
protected function get_full_context(array $context) | ||
{ | ||
if (!array_key_exists('max_depth', $context)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to always set this option? If he wants to serialize all objects he can't this way, but should be able to do it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean "max_depth"? I saved it for back compatibility There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You are wrong. Serialize function could be called beyond \Raven\Client. So we must make sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is this referring to the fact that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Trust me when I say that I always think about it, and rest assured that any decision I take is to give a clear public API to the end-users, and to me exposing something used internally is a bad practice. As this discussion is going too much further in time: @dcramer can you please say what's your opinion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just checked. There is no \Raven\Client::serialize. Only "sanitize". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, I meant to say that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tomorrow (2017/05/09) I will do pull request which will make all methods and properties protected and will break all compatibility we have at this moment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Okay, It's already done: #457 |
||
$context['max_depth'] = 3; | ||
} | ||
|
||
return $context; | ||
} | ||
|
||
/** | ||
* Serialize an object (recursively) into something safe for data | ||
* sanitization and encoding. | ||
|
@@ -71,13 +98,13 @@ public function __construct($mb_detect_order = null) | |
* @param int $_depth | ||
* @return string|bool|double|int|null|object|array | ||
*/ | ||
public function serialize($value, $max_depth = 3, $_depth = 0) | ||
protected function serializeInner($value, $max_depth, $_depth) | ||
{ | ||
if ($_depth < $max_depth) { | ||
if (is_array($value)) { | ||
$new = []; | ||
foreach ($value as $k => $v) { | ||
$new[$this->serializeValue($k)] = $this->serialize($v, $max_depth, $_depth + 1); | ||
$new[$this->serializeValue($k)] = $this->serializeInner($v, $max_depth, $_depth + 1); | ||
} | ||
|
||
return $new; | ||
|
@@ -111,7 +138,7 @@ public function serializeObject($object, $max_depth = 3, $_depth = 0, $hashes = | |
if (is_object($value)) { | ||
$new_value = $this->serializeObject($value, $max_depth, $_depth + 1, $hashes); | ||
} else { | ||
$new_value = $this->serialize($value, $max_depth, $_depth + 1); | ||
$new_value = $this->serialize($value, ['max_depth' => $max_depth - $_depth - 1]); | ||
} | ||
$return[$key] = $new_value; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
|
||
namespace Raven; | ||
|
||
interface SerializerInterface | ||
{ | ||
/** | ||
* Serialize an object (recursively) into something safe for data | ||
* sanitization and encoding. | ||
* | ||
* @param mixed $value | ||
* @param array $context | ||
* @return string|bool|double|int|null|object|array | ||
*/ | ||
public function serialize($value, array $context = []); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accordind to the comment left below about the
serialize
function definition, we could use the$context
parameter to pass additional options to the serializer (in this case about serializing all objects) so that we don't need to rely on some methods not even defined in the interfaceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.