Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"scripts": {
"test": "phpunit",
"test-report": "phpunit --coverage-html reports/",
"cs": "phpcs --standard=PSR12 src/",
"phan": "phan --allow-polyfill-parser",
"check": [
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

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

6 changes: 3 additions & 3 deletions examples/index.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

use Leandro47\SimpleMath\TypeData\Number;
use Leandro47\SimpleMath\TypeValue\Number;

require_once './vendor/autoload.php';

$value1 = Number::with(10);
$value2 = Number::with(20);
$value1 = Number::create(10);
$value2 = Number::create(20);

$result = $value1->sum($value2);

Expand Down
99 changes: 99 additions & 0 deletions src/Format/NumberFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Leandro47\SimpleMath\Format;

use Leandro47\SimpleMath\Interfaces\Format\FormatInterface;
use Leandro47\SimpleMath\Interfaces\Format\NumberFormatInterface;

class NumberFormat implements NumberFormatInterface
{
private string $symbol;
private string $thousandSeparator;
private string $decimalSeparator;
private int $precision;
private string $value;

public function __construct(
string $decimalSeparator,
string $thousandSeparator,
int $precision = 2,
string $symbol = ''
) {
$this->symbol = $symbol;
$this->thousandSeparator = $thousandSeparator;
$this->decimalSeparator = $decimalSeparator;
$this->precision = $precision;
}

public static function create(
string $decimalSeparator,
string $thousandSeparator,
int $precision = 2,
string $symbol = ''
): self {
return new self($decimalSeparator, $thousandSeparator, $precision, $symbol);
}

public function setValue(mixed $value): FormatInterface
{
$this->value = $value;

return $this;
}

public function show(): string
{
$this->setPrecision();
$this->setThousandSeparator();

return $this->value;
}

private function setThousandSeparator(): void
{
$value = explode($this->decimalSeparator, $this->value);
$chars = str_split($value[0]);
$chars = array_reverse($chars);

$qtdSeparator = 0;
$newChars = [];
foreach ($chars as $char) {
if ($qtdSeparator === 3) {
$newChars[] = $this->thousandSeparator;
$newChars[] = $char;
$qtdSeparator = 1;

continue;
}

$newChars[] = $char;
$qtdSeparator++;
}

$value[0] = implode('', array_reverse($newChars));

$this->value = $this->symbol . ' ' . implode($this->decimalSeparator, $value);
}

private function setPrecision(): void
{
$this->value = (string) round((float) $this->value, $this->precision);
$value = explode('.', $this->value);

$decimal = $value[1] ?? '';
$value[1] = $this->setdecimal($decimal);

$this->value = implode($this->decimalSeparator, $value);
}

private function setdecimal(string $value): string
{
$i = strlen($value);
while ($i < $this->precision) {
$value .= '0';
$i++;
}

return $value;
}
}
10 changes: 10 additions & 0 deletions src/Interfaces/Format/FormatInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Leandro47\SimpleMath\Interfaces\Format;

interface FormatInterface
{
public function setValue(mixed $value): self;

public function show(): string;
}
13 changes: 13 additions & 0 deletions src/Interfaces/Format/NumberFormatInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Leandro47\SimpleMath\Interfaces\Format;

interface NumberFormatInterface extends FormatInterface
{
public static function create(
string $symbol,
string $thousandSeparator,
int $precision,
string $decimalSeparator
): self;
}
8 changes: 5 additions & 3 deletions src/Interfaces/TypeValue/ValueInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Leandro47\SimpleMath\Interfaces\TypeValue;

use Leandro47\SimpleMath\Interfaces\Format\FormatInterface;

interface ValueInterface
{
public static function with(mixed $value): self;
public static function create(mixed $value): self;

public function set(mixed $value): void;

public function get(): ValueInterface;

public function value(): mixed;

public function format(?FormatInterface $format = null): string;
}
26 changes: 19 additions & 7 deletions src/TypeValue/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

namespace Leandro47\SimpleMath\TypeValue;

use Leandro47\SimpleMath\Interfaces\Format\FormatInterface;
use Leandro47\SimpleMath\Interfaces\Format\NumberFormatInterface;
use Leandro47\SimpleMath\Interfaces\TypeValue\NumberInterface;

class Number implements NumberInterface
{
private float $value;
private ?NumberFormatInterface $numberFormat;

public function __construct($value)
public function __construct(mixed $value, ?NumberFormatInterface $numberFormat = null)
{
$this->numberFormat = $numberFormat;
$this->set($value);
}

public static function with(mixed $value): NumberInterface
public static function create(mixed $value, ?NumberFormatInterface $numberFormat = null): NumberInterface
{
return new self($value);
return new self($value, $numberFormat);
}

public function set(mixed $value): void
Expand All @@ -36,14 +40,22 @@ public function set(mixed $value): void
$this->value = (float) $value;
}

public function get(): NumberInterface
public function value(): float
{
return $this;
return (float) $this->value;
}

public function value(): float
public function format(?FormatInterface $numberFormat = null): string
{
return (float) $this->value;
if (!is_null($numberFormat)) {
return $numberFormat->setValue($this->value())->show();
}

if (is_null($this->numberFormat)) {
throw new \InvalidArgumentException('NumberFormatInterface is not defined');
}

return $this->numberFormat->setValue($this->value())->show();
}

public function multiplication(NumberInterface $value): NumberInterface
Expand Down
4 changes: 2 additions & 2 deletions tests-executed.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Number
[x] Create with float
[x] Multiplication
[x] Subtraction
[x] Sum
[x] Sum zero
[x] Divider
[x] Get value
[x] Format setting class
[x] Multiplication
[x] Create with string
[x] Create with int
[x] Set value
Expand Down
Loading