diff --git a/README.md b/README.md index cc44f06..a21dbf4 100644 --- a/README.md +++ b/README.md @@ -224,9 +224,15 @@ ComplexTypeFactory::fromPolar(RationalType $radius, RationalType $theta) method. ### Support for GMP extension - V2 onwards only The library automatically recognises the availability of the gmp extension and -will use it for int, rational and complex types. There is no gmp support for -wholeIntType, NaturalIntType or FloatType. You can force the library to use -PHP native types by calling +will use it for int, rational and complex types. + +There is no gmp support for WholeIntType, NaturalIntType or FloatType. + +- WholeIntType and NaturalIntType creation via the TypeFactory will return GMPIntType. + - The validation for whole and natural numbers is ignored - they are treated as integers +- FloatType creation via the TypeFactory will return a GMPRationalType. + +You can force the library to use PHP native types by calling
     TypeFactory::setNumberType(TypeFactory::TYPE_NATIVE);
@@ -240,9 +246,12 @@ If you want to get the gmp typed value of a number you can call its gmp() method
 
     //assuming we are running under gmp
     $i = TypeFactory::create('int', 2); //returns GMPIntType
+    $i = TypeFactory::create('whole', -1); //returns GMPIntType
+    $i = TypeFactory::create('natural', 0); //returns GMPIntType
     $gmp = $i->gmp(); //returns resource or GMP object depending on PHP version
 
     $r = TypeFactory::create('rational', 2, 3); //returns GMPRationalType
+    $r = TypeFactory::create('float', 2/3); //returns GMPRationalType
     $gmp = $r->gmp(); //returns array of gmp types, [numerator, denominator]
 
     $c = TypeFactory::create('complex', '2+3i'); //returns GMPComplexType
@@ -384,3 +393,8 @@ V2.0.6 update phpunit to ~V4.3.0
 
 V2.0.7 add test contract
 
+V2.0.8 when GMP support enabled:
+
+- WholeIntType and NaturalIntType creation via the TypeFactory will return GMPIntType.
+- FloatType creation via the TypeFactory will return a GMPRationalType.
+
diff --git a/build.sh b/build.sh
index bc00c95..9d0e2ce 100755
--- a/build.sh
+++ b/build.sh
@@ -1,5 +1,5 @@
 #!/bin/bash
-cd ~/Projects/Type
+cd ~/Projects/chippyash/source/Strong-Type
 vendor/phpunit/phpunit/phpunit -c test/phpunit.xml --testdox-html contract.html test/
 tdconv -t "Chippyash Strong Types" contract.html docs/Test-Contract.md
 rm contract.html
diff --git a/docs/Test-Contract.md b/docs/Test-Contract.md
index cd87d2c..64e2084 100644
--- a/docs/Test-Contract.md
+++ b/docs/Test-Contract.md
@@ -330,6 +330,9 @@
 *  Create with numeric type interface parameter returns numeric type interface
 *  Set number type to default will set gmp if available
 *  Set number type to invalid type throws exception
+*  Creating whole ints via type factory under gmp will return g m p int type
+*  Creating natural ints via type factory under gmp will return g m p int type
+*  Creating floats via type factory under gmp will return g m p rational type
 
 
 Generated by chippyash/testdox-converter
\ No newline at end of file
diff --git a/src/chippyash/Type/Number/Complex/GMPComplexType.php b/src/chippyash/Type/Number/Complex/GMPComplexType.php
index d3439b5..bc22e3b 100644
--- a/src/chippyash/Type/Number/Complex/GMPComplexType.php
+++ b/src/chippyash/Type/Number/Complex/GMPComplexType.php
@@ -17,6 +17,7 @@
 use chippyash\Type\Exceptions\NotRealComplexException;
 use chippyash\Type\Interfaces\GMPInterface;
 use chippyash\Type\Number\GMPIntType;
+use chippyash\Type\Number\FloatType;
 use chippyash\Type\TypeFactory;
 
 /**
@@ -215,6 +216,20 @@ public function asRational()
         }
     }
     
+    /**
+     * Return number as a FloatType number.
+     *
+     * @returns chippyash\Type\Number\FloatType
+     */
+    public function asFloatType()
+    {
+        if ($this->isReal()) {
+            return new FloatType($this->value['real']->get());
+        } else {
+            throw new NotRealComplexException();
+        }
+    }
+    
     /**
      * Return Least Common Multiple of two numbers
      * @param int $a
diff --git a/src/chippyash/Type/TypeFactory.php b/src/chippyash/Type/TypeFactory.php
index 0e64adb..8d3d77c 100644
--- a/src/chippyash/Type/TypeFactory.php
+++ b/src/chippyash/Type/TypeFactory.php
@@ -116,7 +116,7 @@ public static function createInt($value)
      * Create a FloatType
      *
      * @param mixed $value
-     * @return \chippyash\Type\Number\FloatType
+     * @return \chippyash\Type\Number\FloatType|\chippyash\Type\Number\Rational\GMPRationalType
      *
      * @throws \InvalidArgumentException
      */
@@ -128,7 +128,12 @@ public static function createFloat($value)
         if (!is_numeric($value)) {
             throw new \InvalidArgumentException("'{$value}' is no valid numeric for FloatType");
         }
-        return new FloatType($value);
+        
+        if (self::getRequiredType() == self::TYPE_GMP) {
+            return RationalTypeFactory::create($value);
+        } else {
+            return new FloatType($value);
+        }
     }
 
     /**
@@ -168,7 +173,7 @@ public static function createDigit($value)
      * Create a whole number
      *
      * @param mixed $value
-     * @return \chippyash\Type\Number\WholeIntType
+     * @return \chippyash\Type\Number\WholeIntType|\chippyash\Type\Number\GMPIntType
      *
      * @throws \InvalidArgumentException
      */
@@ -180,14 +185,19 @@ public static function createWhole($value)
         if (!is_numeric($value)) {
             throw new \InvalidArgumentException("'{$value}' is no valid numeric for WholeIntType");
         }
-        return new WholeIntType($value);
+        
+        if (self::getRequiredType() == self::TYPE_GMP) {
+            return new GMPIntType($value);
+        } else {
+            return new WholeIntType($value);
+        }
     }
 
     /**
      * Create a Natural number
      *
      * @param mixed $value
-     * @return \chippyash\Type\Number\NaturalIntType
+     * @return \chippyash\Type\Number\NaturalIntType|\chippyash\Type\Number\GMPIntType
      *
      * @throws \InvalidArgumentException
      */
@@ -199,7 +209,12 @@ public static function createNatural($value)
         if (!is_numeric($value)) {
             throw new \InvalidArgumentException("'{$value}' is no valid numeric for NaturalIntType");
         }
-        return new NaturalIntType($value);
+        
+        if (self::getRequiredType() == self::TYPE_GMP) {
+            return new GMPIntType($value);
+        } else {
+            return new NaturalIntType($value);
+        }
     }
 
     /**
diff --git a/test/src/chippyash/Type/TypeFactoryTest.php b/test/src/chippyash/Type/TypeFactoryTest.php
index a9d7175..9b721e5 100644
--- a/test/src/chippyash/Type/TypeFactoryTest.php
+++ b/test/src/chippyash/Type/TypeFactoryTest.php
@@ -272,4 +272,34 @@ public function testSetNumberTypeToInvalidTypeThrowsException()
     {
         TypeFactory::setNumberType('foo');
     }
+    
+    /**
+     * @requires extension gmp
+     * @runInSeparateProcess
+     */
+    public function  testCreatingWholeIntsViaTypeFactoryUnderGmpWillReturnGMPIntType()
+    {
+        TypeFactory::setNumberType(TypeFactory::TYPE_DEFAULT);
+        $this->assertInstanceOf('chippyash\Type\Number\GMPIntType', TypeFactory::create('whole', -1));
+    }
+    
+    /**
+     * @requires extension gmp
+     * @runInSeparateProcess
+     */
+    public function  testCreatingNaturalIntsViaTypeFactoryUnderGmpWillReturnGMPIntType()
+    {
+        TypeFactory::setNumberType(TypeFactory::TYPE_DEFAULT);
+        $this->assertInstanceOf('chippyash\Type\Number\GMPIntType', TypeFactory::create('natural', 0));
+    }
+    
+    /**
+     * @requires extension gmp
+     * @runInSeparateProcess
+     */
+    public function  testCreatingFloatsViaTypeFactoryUnderGmpWillReturnGMPRationalType()
+    {
+        TypeFactory::setNumberType(TypeFactory::TYPE_DEFAULT);
+        $this->assertInstanceOf('chippyash\Type\Number\Rational\GMPRationalType', TypeFactory::create('float', 2/3));
+    }
 }