Skip to content

Commit

Permalink
Add Natural VO
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHash committed Sep 1, 2020
1 parent 3968991 commit 291a9bd
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 43 deletions.
40 changes: 20 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions src/FloatValue.php
Expand Up @@ -22,8 +22,7 @@ public static function fromNative($value): self
$value = floatval($value);
}
Assertion::nullOrFloat($value, 'Trying to create FloatValue VO from unsupported value type.');
/** @psalm-suppress PossiblyInvalidArgument */
return is_null($value) ? new self : new self(floatval($value));
return $value === null ? new self : new self(floatval($value));
}

public static function zero(): self
Expand All @@ -38,16 +37,18 @@ public static function makeEmpty(): self

public function format(int $decimals = 0, string $point = '.', string $separator = ','): string
{
$this->assertNotEmpty();
return number_format($this->value, $decimals, $point, $separator);
}

public function isEmpty(): bool
{
return is_null($this->value);
return $this->value === null;
}

public function isZero(): bool
{
$this->assertNotEmpty();
return $this->value === 0.0;
}

Expand All @@ -65,7 +66,12 @@ public function equals($comparator): bool

public function __toString(): string
{
return is_null($this->value) ? 'null' : (string)$this->value;
return (string)$this->value;
}

private function assertNotEmpty(): void
{
Assertion::false($this->isEmpty(), 'Float is empty.');
}

private function __construct(?float $value = null)
Expand Down
32 changes: 23 additions & 9 deletions src/IntValue.php
Expand Up @@ -17,45 +17,54 @@ final class IntValue implements MakeEmptyInterface, ValueObjectInterface

public function isZero(): bool
{
$this->assertNotEmpty();
return $this->value === 0;
}

public function isEmpty(): bool
{
return is_null($this->value);
return $this->value === null;
}

public function add(self $amount): self
{
Assertion::false($this->isEmpty() || $amount->isEmpty(), 'Operands must not be null.');
/** @psalm-suppress PossiblyNullOperand */
return self::fromNative($this->toNative() + $amount->toNative());
$this->assertNotEmpty();
Assertion::false($amount->isEmpty(), 'Addition must not be empty.');
return self::fromNative((int)$this->toNative() + (int)$amount->toNative());
}

public function subtract(self $amount): self
{
Assertion::false($this->isEmpty() || $amount->isEmpty(), 'Operands must not be null.');
/** @psalm-suppress PossiblyNullOperand */
return self::fromNative($this->toNative() - $amount->toNative());
$this->assertNotEmpty();
Assertion::false($amount->isEmpty(), 'Subtraction must not be empty.');
return self::fromNative((int)$this->toNative() - (int)$amount->toNative());
}

public function isGreaterThanOrEqualTo(self $comparator): bool
{
$this->assertNotEmpty();
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.');
return $this->toNative() >= $comparator->toNative();
}

public function isLessThanOrEqualTo(self $comparator): bool
{
$this->assertNotEmpty();
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.');
return $this->toNative() <= $comparator->toNative();
}

public function isGreaterThan(self $comparator): bool
{
$this->assertNotEmpty();
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.');
return $this->toNative() > $comparator->toNative();
}

public function isLessThan(self $comparator): bool
{
$this->assertNotEmpty();
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.');
return $this->toNative() < $comparator->toNative();
}

Expand All @@ -74,7 +83,7 @@ public static function fromNative($value): self
{
$value = $value === '' ? null : $value;
Assertion::nullOrIntegerish($value, 'Trying to create IntValue VO from unsupported value type.');
return new self(is_null($value) ? null : (int)$value);
return $value === null ? new self : new self((int)$value);
}

public function toNative(): ?int
Expand All @@ -91,7 +100,12 @@ public function equals($comparator): bool

public function __toString(): string
{
return $this->isEmpty() ? '' : (string)$this->value;
return (string)$this->value;
}

private function assertNotEmpty(): void
{
Assertion::false($this->isEmpty(), 'Int value is empty.');
}

private function __construct(int $value = null)
Expand Down
116 changes: 116 additions & 0 deletions src/Natural.php
@@ -0,0 +1,116 @@
<?php declare(strict_types=1);
/**
* This file is part of the daikon-cqrs/value-object project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Daikon\ValueObject;

use Daikon\Interop\Assertion;
use Daikon\Interop\MakeEmptyInterface;

final class Natural implements MakeEmptyInterface, ValueObjectInterface
{
private ?int $value;

public function isZero(): bool
{
$this->assertNotEmpty();
return $this->value === 0;
}

public function isEmpty(): bool
{
return $this->value === null;
}

public function add(self $amount): self
{
$this->assertNotEmpty();
Assertion::false($amount->isEmpty(), 'Addition must not be empty.');
return self::fromNative((int)$this->toNative() + (int)$amount->toNative());
}

public function subtract(self $amount): self
{
$this->assertNotEmpty();
Assertion::false($amount->isEmpty(), 'Subtraction must not be empty.');
return self::fromNative((int)$this->toNative() - (int)$amount->toNative());
}

public function isGreaterThanOrEqualTo(self $comparator): bool
{
$this->assertNotEmpty();
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.');
return $this->toNative() >= $comparator->toNative();
}

public function isLessThanOrEqualTo(self $comparator): bool
{
$this->assertNotEmpty();
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.');
return $this->toNative() <= $comparator->toNative();
}

public function isGreaterThan(self $comparator): bool
{
$this->assertNotEmpty();
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.');
return $this->toNative() > $comparator->toNative();
}

public function isLessThan(self $comparator): bool
{
$this->assertNotEmpty();
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.');
return $this->toNative() < $comparator->toNative();
}

public static function makeEmpty(): self
{
return new self;
}

public static function zero(): self
{
return new self(0);
}

/** @param null|int|string $value */
public static function fromNative($value): self
{
$value = $value === '' ? null : $value;
Assertion::nullOrIntegerish($value, 'Trying to create Natural VO from unsupported value type.');
return $value === null ? new self : new self((int)$value);
}

public function toNative(): ?int
{
return $this->value;
}

/** @param self $comparator */
public function equals($comparator): bool
{
Assertion::isInstanceOf($comparator, self::class);
return $this->toNative() === $comparator->toNative();
}

public function __toString(): string
{
return (string)$this->value;
}

private function assertNotEmpty(): void
{
Assertion::false($this->isEmpty(), 'Natural is empty.');
}

private function __construct(int $value = null)
{
Assertion::nullOrGreaterOrEqualThan($value, 0, 'Must be at least 0.');
$this->value = $value;
}
}
1 change: 1 addition & 0 deletions src/ValueObjectCollectionTrait.php
Expand Up @@ -56,6 +56,7 @@ public static function fromNative($state): self
// Override fromNative() to support multiple types, currently first seen type factory is used.
$typeFactory = current(static::inferTypeFactories());
Assertion::isCallable($typeFactory, 'No valid type factory specified.');
/** @var callable $typeFactory */
$objects = [];
if (!is_null($state)) {
foreach ($state as $key => $data) {
Expand Down

0 comments on commit 291a9bd

Please sign in to comment.