Skip to content

Commit

Permalink
return gmp types for whole, natural and float creation from type fact…
Browse files Browse the repository at this point in the history
…ory when gmp support enabled
  • Loading branch information
chippyash committed Feb 2, 2015
1 parent 08d841b commit 52fe91b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 10 deletions.
20 changes: 17 additions & 3 deletions README.md
Expand Up @@ -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

<pre>
TypeFactory::setNumberType(TypeFactory::TYPE_NATIVE);
Expand All @@ -240,9 +246,12 @@ If you want to get the gmp typed value of a number you can call its gmp() method
<pre>
//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
Expand Down Expand Up @@ -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.

2 changes: 1 addition & 1 deletion 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
Expand Down
3 changes: 3 additions & 0 deletions docs/Test-Contract.md
Expand Up @@ -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
15 changes: 15 additions & 0 deletions src/chippyash/Type/Number/Complex/GMPComplexType.php
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down
27 changes: 21 additions & 6 deletions src/chippyash/Type/TypeFactory.php
Expand Up @@ -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
*/
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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);
}
}

/**
Expand Down
30 changes: 30 additions & 0 deletions test/src/chippyash/Type/TypeFactoryTest.php
Expand Up @@ -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));
}
}

0 comments on commit 52fe91b

Please sign in to comment.