Permalink
Browse files

added BinaryData property type

  • Loading branch information...
1 parent bc7c5f7 commit f143462bf4aefc6f59b9388dc5ab92ee32b5e167 @a-musing-moose committed Apr 29, 2011
Showing with 112 additions and 3 deletions.
  1. +89 −0 src/morph/property/BinaryData.php
  2. +23 −3 unit-tests/property/TestProperties.php
@@ -0,0 +1,89 @@
+<?php
+/**
+ * @package Morph
+ * @subpackage Property
+ * @author Jonathan Moss <xirisr@gmail.com>
+ * @copyright 2009 Jonathan Moss
+ * @version SVN: $Id$
+ */
+namespace morph\property;
+/**
+ * This property object is used to hold binary data
+ *
+ * This is a wrapper around MongoBinData. It will accept and string data
+ *
+ *
+ * @package Morph
+ * @subpackage Property
+ */
+class BinaryData extends Generic
+{
+
+ /**
+ * This is not really used as such at present
+ *
+ * @var The binary type
+ */
+ protected $type;
+
+ /**
+ * @param type $name
+ * @param type $type
+ * @param type $default
+ */
+ public function __construct($name, $type = \MongoBinData::BYTE_ARRAY, $default = null)
+ {
+ $this->type = $type;
+ parent::__construct($name, $default);
+ }
+
+ /**
+ * Returns the date associated with this property as a timestamp
+ *
+ * @return int
+ */
+ public function getValue()
+ {
+ return $this->value;
+ }
+
+ /**
+ * Sets the value of this property
+ *
+ * @param string $Value
+ * @return void
+ */
+ public function setValue($value = null)
+ {
+ if (isset($value)) {
+ $this->value = (string)$value;
+ } else {
+ $this->value = null;
+ }
+ }
+
+ /**
+ * Returns the raw value of this property
+ *
+ * @return mixed
+ */
+ public function __getRawValue()
+ {
+ if (!isset($this->value)) return null;
+ $rawValue = new \MongoBinData($this->value, $this->type);
+ return $rawValue;
+ }
+
+ /**
+ * Sets the raw value of this property
+ *
+ * @param $value
+ * @return Morph_Property_Generic
+ */
+ public function __setRawValue($value)
+ {
+ $this->value = $value->bin;
+ return $this;
+ }
+
+}
@@ -7,6 +7,7 @@
require_once dirname(__FILE__).'/../../src/morph/property/Float.php';
require_once dirname(__FILE__).'/../../src/morph/property/Enum.php';
require_once dirname(__FILE__).'/../../src/morph/property/Date.php';
+require_once dirname(__FILE__).'/../../src/morph/property/BinaryData.php';
/**
* @package Morph
@@ -89,7 +90,7 @@ public function testEnumProperty()
$valid = 'Yes';
$invalid = 'Bob';
- $property = new Enum('AProperty', $valid, $enums);
+ $property = new Enum('AProperty', $enums, $valid);
$this->assertEquals($enums, $property->getChoices());
@@ -116,5 +117,24 @@ public function testDateProperty()
//check the MorphDate objects content is correct
$this->assertEquals($time, $property->__getRawValue()->sec);
}
-}
-?>
+
+ public function testBinaryData()
+ {
+ $property = new BinaryData("AProperty");
+ $data = md5('morph', true);
+ $property->setValue($data);
+ $this->assertEquals($data, $property->getValue());
+
+ //check the output type is correct
+ $this->assertType('\MongoBinData', $property->__getRawValue());
+
+ //check the MorphDate objects content is correct
+ $this->assertEquals($data, $property->__getRawValue()->bin);
+
+ $property2 = new BinaryData("AProperty");
+ $property2->__setRawValue($property->__getRawValue());
+ $this->assertEquals($data, $property2->getValue());
+
+
+ }
+}

0 comments on commit f143462

Please sign in to comment.