Skip to content
Browse files

Added a __toString method to BinaryData.php so it prints out a base64…

… encoded version of the binary data, because before json_encode was throwing a 'invalid utf8 argument' when you tried to print out a binary data property
  • Loading branch information...
1 parent 146e665 commit 410b2ee5b601110e33e6a5220ce23163308b72ee @mgrandi committed May 31, 2011
Showing with 22 additions and 1 deletion.
  1. +7 −1 src/morph/Object.php
  2. +15 −0 src/morph/property/BinaryData.php
View
8 src/morph/Object.php
@@ -286,13 +286,19 @@ public function delete()
*/
public function __toString()
{
+ // create the array that we will be encoding and returning
+ // also put inside the array the mongodb ID and the 'state'
$data = array(
'Id' => $this->id(),
'State' => $this->state()
);
+
+ // iterate through all the properties this object has and print them out
foreach ($this->propertySet as $name => $property) {
+
$data[$name] = (string)$property;
- }
+ }
+
return \json_encode($data);
}
View
15 src/morph/property/BinaryData.php
@@ -86,4 +86,19 @@ public function __setRawValue($value)
return $this;
}
+
+ /**
+ * Returns the base64 encoding of the binary data
+ *
+ * @return String
+ */
+ public function __toString()
+ {
+
+ // we return the base64 encoding of the binary data because
+ // json does not have a native way to represent binary data
+ return base64_encode($this->value);
+
+ }
+
}

0 comments on commit 410b2ee

Please sign in to comment.
Something went wrong with that request. Please try again.