Permalink
Browse files

Extend common BinDataType class and ensure DB conversion is idempotent

  • Loading branch information...
1 parent 2d700b8 commit 11ffe95c712b9e1a6ed5c6fd65eb3ebf69f7c425 @jmikola jmikola committed Jan 14, 2014
@@ -28,23 +28,5 @@
*/
class BinDataCustomType extends BinDataType
{
- public function convertToDatabaseValue($value)
- {
- return $value !== null ? new \MongoBinData($value, \MongoBinData::CUSTOM) : null;
- }
-
- public function convertToPHPValue($value)
- {
- return $value !== null ? ($value instanceof \MongoBinData ? $value->bin : $value) : null;
- }
-
- public function closureToMongo()
- {
- return '$return = $value !== null ? new \MongoBinData($value, \MongoBinData::CUSTOM) : null;';
- }
-
- public function closureToPHP()
- {
- return '$return = $value !== null ? ($value instanceof \MongoBinData ? $value->bin : $value) : null;';
- }
+ protected $binDataType = \MongoBinData::CUSTOM;
}
@@ -26,25 +26,7 @@
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
-class BinDataFuncType extends Type
+class BinDataFuncType extends BinDataType
{
- public function convertToDatabaseValue($value)
- {
- return $value !== null ? new \MongoBinData($value, \MongoBinData::FUNC) : null;
- }
-
- public function convertToPHPValue($value)
- {
- return $value !== null ? ($value instanceof \MongoBinData ? $value->bin : $value) : null;
- }
-
- public function closureToMongo()
- {
- return '$return = $value !== null ? new \MongoBinData($value, \MongoBinData::FUNC) : null;';
- }
-
- public function closureToPHP()
- {
- return '$return = $value !== null ? ($value instanceof \MongoBinData ? $value->bin : $value) : null;';
- }
+ protected $binDataType = \MongoBinData::FUNC;
}
@@ -26,25 +26,7 @@
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
-class BinDataMD5Type extends Type
+class BinDataMD5Type extends BinDataType
{
- public function convertToDatabaseValue($value)
- {
- return $value !== null ? new \MongoBinData($value, \MongoBinData::MD5) : null;
- }
-
- public function convertToPHPValue($value)
- {
- return $value !== null ? ($value instanceof \MongoBinData ? $value->bin : $value) : null;
- }
-
- public function closureToMongo()
- {
- return '$return = $value !== null ? new \MongoBinData($value, \MongoBinData::MD5) : null;';
- }
-
- public function closureToPHP()
- {
- return '$return = $value !== null ? ($value instanceof \MongoBinData ? $value->bin : $value) : null;';
- }
+ protected $binDataType = \MongoBinData::MD5;
}
@@ -28,9 +28,29 @@
*/
class BinDataType extends Type
{
+ /**
+ * MongoBinData type
+ *
+ * @var integer
+ * @see http://php.net/manual/en/mongobindata.construct.php
+ */
+ protected $binDataType = \MongoBinData::BYTE_ARRAY;
+
public function convertToDatabaseValue($value)
{
- return $value !== null ? new \MongoBinData($value, \MongoBinData::BYTE_ARRAY) : null;
+ if ($value === null) {
+ return null;
+ }
+
+ if ( ! $value instanceof \MongoBinData) {
+ return new \MongoBinData($value, $this->binDataType);
+ }
+
+ if ($value->type !== $this->binDataType) {
+ return new \MongoBinData($value->bin, $this->binDataType);
+ }
+
+ return $value;
}
public function convertToPHPValue($value)
@@ -40,7 +60,7 @@ public function convertToPHPValue($value)
public function closureToMongo()
{
- return '$return = $value !== null ? new \MongoBinData($value, \MongoBinData::BYTE_ARRAY) : null;';
+ return sprintf('$return = $value !== null ? new \MongoBinData($value, %d) : null;', $this->binDataType);
}
public function closureToPHP()
@@ -26,25 +26,7 @@
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
-class BinDataUUIDType extends Type
+class BinDataUUIDType extends BinDataType
{
- public function convertToDatabaseValue($value)
- {
- return $value !== null ? new \MongoBinData($value, \MongoBinData::UUID) : null;
- }
-
- public function convertToPHPValue($value)
- {
- return $value !== null ? ($value instanceof \MongoBinData ? $value->bin : $value) : null;
- }
-
- public function closureToMongo()
- {
- return '$return = $value !== null ? new \MongoBinData($value, \MongoBinData::UUID) : null;';
- }
-
- public function closureToPHP()
- {
- return '$return = $value !== null ? ($value instanceof \MongoBinData ? $value->bin : $value) : null;';
- }
+ protected $binDataType = \MongoBinData::UUID;
}

0 comments on commit 11ffe95

Please sign in to comment.