Skip to content

Commit

Permalink
Add negate() method to numeric types
Browse files Browse the repository at this point in the history
  • Loading branch information
akitson-fu committed Jul 27, 2014
1 parent cefa227 commit 19a8a62
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 4 deletions.
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -125,6 +125,11 @@ All types support the TypeInterface:
* \__toString() - Magic toString method. Return value as a string
* \__invoke() - Proxy to get(), allows you to write $c() instead of $c->get()

Numeric types, that is IntType, WholeIntType, NaturalIntType, FloatType, RationalType
and ComplexType support the NumericTypeInterface which defines the method

* negate(): negate the number - NB Negation is will throw a \BadMethodCallException for WholeInt and NaturalInt types as they cannot be negative

Additionally, the RationalType supports the RationalTypeInterface:

* setFromTypes(IntType $num, IntType $den, BoolType $reduce = null) - strict typed setter method
Expand Down Expand Up @@ -223,4 +228,6 @@ V1.0.2 Add conjugate method to complex type

V1.0.3 Add modulus method to complex type

V1.0.4 Fix RationalTypefactory::fromFloat not recognising zero
V1.0.4 Fix RationalTypefactory::fromFloat not recognising zero

V1.0.5 Add negate() method to numeric types
12 changes: 12 additions & 0 deletions src/chippyash/Type/Number/Complex/ComplexType.php
Expand Up @@ -168,4 +168,16 @@ public function setFromTypes(FloatType $real, FloatType $imaginary)
return $this;
}

/**
* Negates the number
*
* @returns chippyash\Type\Number\Complex\ComplexType Fluent Interface
*/
public function negate()
{
$this->real *= -1;
$this->imaginary *= -1;

return $this;
}
}
12 changes: 12 additions & 0 deletions src/chippyash/Type/Number/FloatType.php
Expand Up @@ -19,6 +19,18 @@
class FloatType extends AbstractType implements NumericTypeInterface
{

/**
* Negates the number
*
* @returns chippyash\Type\Number\FloatType Fluent Interface
*/
public function negate()
{
$this->value *= -1;

return $this;
}

protected function typeOf($value)
{
return floatval($value);
Expand Down
14 changes: 13 additions & 1 deletion src/chippyash/Type/Number/IntType.php
Expand Up @@ -10,7 +10,7 @@

namespace chippyash\Type\Number;

use \chippyash\Type\AbstractType;
use chippyash\Type\AbstractType;
use chippyash\Type\Number\NumericTypeInterface;

/**
Expand All @@ -19,6 +19,18 @@
class IntType extends AbstractType implements NumericTypeInterface
{

/**
* Negates the number
*
* @returns chippyash\Type\Number\IntType Fluent Interface
*/
public function negate()
{
$this->value *= -1;

return $this;
}

protected function typeOf($value)
{
return intval($value);
Expand Down
10 changes: 10 additions & 0 deletions src/chippyash/Type/Number/NaturalIntType.php
Expand Up @@ -19,6 +19,16 @@
class NaturalIntType extends IntType
{

/**
* Negates the number
*
* @throws \BadMethodCallException
*/
public function negate()
{
throw new \BadMethodCallException('Negate not supported for Natural Int Types');
}

protected function typeOf($value)
{
$v = intval($value);
Expand Down
12 changes: 10 additions & 2 deletions src/chippyash/Type/Number/NumericTypeInterface.php
Expand Up @@ -10,6 +10,14 @@
namespace chippyash\Type\Number;

/**
* A dummy interface to mark numeric types
* A interface to mark numeric types
*/
interface NumericTypeInterface {}
interface NumericTypeInterface {

/**
* negates the number
*
* @returns chippyash\Type\Number\NumericTypeInterface Fluent Interface
*/
public function negate();
}
12 changes: 12 additions & 0 deletions src/chippyash/Type/Number/Rational/RationalType.php
Expand Up @@ -107,6 +107,18 @@ public function __toString()
return "{$this->num}/{$this->den}";
}

/**
* Negates the number
*
* @returns chippyash\Type\Number\Rational\RationalType Fluent Interface
*/
public function negate()
{
$this->num *= -1;

return $this;
}

/**
* Reduce this number to it's lowest form
*/
Expand Down
10 changes: 10 additions & 0 deletions src/chippyash/Type/Number/WholeIntType.php
Expand Up @@ -19,6 +19,16 @@
class WholeIntType extends IntType
{

/**
* Negates the number
*
* @throws \BadMethodCallException
*/
public function negate()
{
throw new \BadMethodCallException('Negate not supported for Whole Int Types');
}

protected function typeOf($value)
{
$v = intval($value);
Expand Down
6 changes: 6 additions & 0 deletions test/src/chippyash/Type/Number/Complex/ComplexTypeTest.php
Expand Up @@ -191,4 +191,10 @@ public function testCommutativeMultiplicationAttributeForModulus()

$this->assertEquals($modc1mulc2(), $mod1() * $mod2());
}

public function testCanNegateTheNumber()
{
$c = new ComplexType(new FloatType(1), new FloatType(2));
$this->assertEquals('-1-2i', $c->negate()->get());
}
}
5 changes: 5 additions & 0 deletions test/src/chippyash/Type/Number/FloatTypeTest.php
Expand Up @@ -17,4 +17,9 @@ public function testFloatTypeConvertsValuesToFloat()
$this->assertEquals(0, $t->get());
}

public function testCanNegateTheNumber()
{
$t = new FloatType(2.0);
$this->assertEquals(-2.0, $t->negate()->get());
}
}
6 changes: 6 additions & 0 deletions test/src/chippyash/Type/Number/IntTypeTest.php
Expand Up @@ -22,5 +22,11 @@ public function testIntTypeConvertsValuesToInteger()
$this->assertInternalType('int', $t->get());
$this->assertEquals(34, $t->get());
}

public function testCanNegateTheNumber()
{
$t = new IntType(2);
$this->assertEquals(-2, $t->negate()->get());
}

}
9 changes: 9 additions & 0 deletions test/src/chippyash/Type/Number/NaturalIntTypeTest.php
Expand Up @@ -29,4 +29,13 @@ public function testConstructNaturalIntWithIntegerLessThanOneThrowsException()
$t = new NaturalIntType(0);
}

/**
* @expectedException \BadMethodCallException
* @expectedExceptionMessage Negate not supported for Natural Int Types
*/
public function testCannotNegateTheNumber()
{
$t = new NaturalIntType(12);
$t->negate();
}
}
6 changes: 6 additions & 0 deletions test/src/chippyash/Type/Number/Rational/RationalTypeTest.php
Expand Up @@ -71,4 +71,10 @@ public function testGetReturnsFloat()
$this->assertInternalType('float', $r->get());
$this->assertEquals(0.5, $r->get());
}

public function testCanNegateTheNumber()
{
$r = new RationalType(new IntType(1), new IntType(2));
$this->assertEquals(-0.5, $r->negate()->get());
}
}
9 changes: 9 additions & 0 deletions test/src/chippyash/Type/Number/WholeIntTypeTest.php
Expand Up @@ -32,4 +32,13 @@ public function testConstructWholeIntWithIntegerLessThanZeroThrowsException()
$t = new WholeIntType(-1);
}

/**
* @expectedException \BadMethodCallException
* @expectedExceptionMessage Negate not supported for Whole Int Types
*/
public function testCannotNegateTheNumber()
{
$t = new WholeIntType(12);
$t->negate();
}
}

0 comments on commit 19a8a62

Please sign in to comment.