Permalink
Browse files

added property types for MongoInt32, MongoInt64 and MongoRegex

  • Loading branch information...
1 parent ff23a42 commit 6a0c893ebed42801faf4186cf9323dab4e5428cc @a-musing-moose committed Apr 30, 2011
@@ -0,0 +1,68 @@
+<?php
+/**
+ * @package Morph
+ * @subpackage Property
+ * @author Jonathan Moss <xirisr@gmail.com>
+ * @copyright Jonathan Moss 2009
+ */
+namespace morph\property;
+/**
+ * Class to represent an integer property
+ *
+ * @package Morph
+ * @subpackage Property
+ */
+class Integer32 extends Generic
+{
+
+ /**
+ * The minimum allowed value
+ * @var integer
+ */
+ protected $minimum;
+
+ /**
+ * The maximum allowed value
+ * @var integer
+ */
+ protected $maximum;
+
+ /**
+ * @param string $Name
+ * @param integer $Default
+ * @param integer $Minimum
+ * @param integer $Maximum
+ */
+ public function __construct($name, $default = null, $minimum = null, $maximum = null){
+ parent::__construct($name, $default);
+ $this->minimum = (\is_null($minimum))? null : (int)$minimum;
+ $this->maximum = (\is_null($maximum))? null : (int)$maximum;
+ }
+
+ /**
+ * Sets the value of this attribute
+ *
+ * @param integer $Value
+ */
+ public function setValue($value){
+ $cleanValue = (int)$value;
+ if(!empty($this->minimum) && ($cleanValue < $this->minimum) ){
+ //throw validation exception
+ $cleanValue = $this->minimum;
+ }
+ if(!empty($this->maximum) && ($cleanValue > $this->maximum) ){
+ //throw validation exception
+ $cleanValue = $this->maximum;
+ }
+ parent::setValue(new \MongoInt32($cleanValue));
+ }
+
+ /**
+ * Returns this attributes value
+ *
+ * @return integer
+ */
+ public function getValue(){
+ return (int)parent::getValue()->value;
+ }
+}
@@ -0,0 +1,68 @@
+<?php
+/**
+ * @package Morph
+ * @subpackage Property
+ * @author Jonathan Moss <xirisr@gmail.com>
+ * @copyright Jonathan Moss 2009
+ */
+namespace morph\property;
+/**
+ * Class to represent an integer property
+ *
+ * @package Morph
+ * @subpackage Property
+ */
+class Integer64 extends Generic
+{
+
+ /**
+ * The minimum allowed value
+ * @var integer
+ */
+ protected $minimum;
+
+ /**
+ * The maximum allowed value
+ * @var integer
+ */
+ protected $maximum;
+
+ /**
+ * @param string $Name
+ * @param integer $Default
+ * @param integer $Minimum
+ * @param integer $Maximum
+ */
+ public function __construct($name, $default = null, $minimum = null, $maximum = null){
+ parent::__construct($name, $default);
+ $this->minimum = (\is_null($minimum))? null : (int)$minimum;
+ $this->maximum = (\is_null($maximum))? null : (int)$maximum;
+ }
+
+ /**
+ * Sets the value of this attribute
+ *
+ * @param integer $Value
+ */
+ public function setValue($value){
+ $cleanValue = (int)$value;
+ if(!empty($this->minimum) && ($cleanValue < $this->minimum) ){
+ //throw validation exception
+ $cleanValue = $this->minimum;
+ }
+ if(!empty($this->maximum) && ($cleanValue > $this->maximum) ){
+ //throw validation exception
+ $cleanValue = $this->maximum;
+ }
+ parent::setValue(new \MongoInt64($cleanValue));
+ }
+
+ /**
+ * Returns this attributes value
+ *
+ * @return integer
+ */
+ public function getValue(){
+ return (int)parent::getValue()->value;
+ }
+}
@@ -0,0 +1,46 @@
+<?php
+/**
+ * @package Morph
+ * @subpackage Property
+ * @author Jonathan Moss <xirisr@gmail.com>
+ * @copyright Jonathan Moss 2009
+ */
+namespace morph\property;
+/**
+ * Class to represent an integer property
+ *
+ * @package Morph
+ * @subpackage Property
+ */
+class Regex extends Generic
+{
+
+
+ /**
+ * @param string $name
+ */
+ public function __construct($name)
+ {
+ parent::__construct($name, null);
+ }
+
+ /**
+ * Sets the value of this attribute
+ *
+ * @param string $value
+ */
+ public function setValue($value)
+ {
+ parent::setValue(new \MongoRegex((string)$value));
+ }
+
+ /**
+ * Returns this attributes value
+ *
+ * @return integer
+ */
+ public function getValue()
+ {
+ return '/' . (string)parent::getValue()->regex . '/';
+ }
+}
@@ -8,6 +8,9 @@
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';
+require_once dirname(__FILE__).'/../../src/morph/property/Integer32.php';
+require_once dirname(__FILE__).'/../../src/morph/property/Integer64.php';
+require_once dirname(__FILE__).'/../../src/morph/property/Regex.php';
/**
* @package Morph
@@ -126,15 +129,66 @@ public function testBinaryData()
$this->assertEquals($data, $property->getValue());
//check the output type is correct
- $this->assertType('\MongoBinData', $property->__getRawValue());
+ $this->assertInstanceOf('\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());
+ }
+
+ public function testInteger32Property()
+ {
+ $min = 1;
+ $max = 100;
+ $validInteger = 5;
+ $toLarge = 102;
+ $toSmall = 0;
+ $property = new Integer32('AProperty', null, $min, $max);
+
+ $property->setValue($validInteger);
+ $this->assertEquals($validInteger, $property->getValue());
+
+ $property->setValue($toLarge);
+ $this->assertEquals($max, $property->getValue());
+
+ $property->setValue($toSmall);
+ $this->assertEquals($min, $property->getValue());
+ $this->assertInstanceOf('\MongoInt32', $property->__getRawValue());
+ }
+
+ public function testInteger64Property()
+ {
+ $min = 1;
+ $max = 100;
+ $validInteger = 5;
+ $toLarge = 102;
+ $toSmall = 0;
+ $property = new Integer64('AProperty', null, $min, $max);
+
+ $property->setValue($validInteger);
+ $this->assertEquals($validInteger, $property->getValue());
+
+ $property->setValue($toLarge);
+ $this->assertEquals($max, $property->getValue());
+
+ $property->setValue($toSmall);
+ $this->assertEquals($min, $property->getValue());
+ $this->assertInstanceOf('\MongoInt64', $property->__getRawValue());
+ }
+
+ public function testRegex()
+ {
+ $property = new Regex("AProperty");
+ $data = '/bob/';
+ $property->setValue($data);
+ $this->assertEquals($data, $property->getValue());
+ $this->assertInstanceOf('\MongoRegex', $property->__getRawValue());
+ $this->assertEquals($data, '/' . $property->__getRawValue()->regex . '/');
}
+
}

0 comments on commit 6a0c893

Please sign in to comment.