diff --git a/composer.json b/composer.json index c0705cf..f78d1a8 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ }, "scripts": { "test": "phpunit", + "test-report": "phpunit --coverage-html reports/", "cs": "phpcs --standard=PSR12 src/", "phan": "phan --allow-polyfill-parser", "check": [ diff --git a/composer.lock b/composer.lock index 44779b5..3d290a8 100644 --- a/composer.lock +++ b/composer.lock @@ -3430,5 +3430,5 @@ "prefer-lowest": false, "platform": [], "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" } diff --git a/examples/index.php b/examples/index.php index 94c5c0d..734a44e 100644 --- a/examples/index.php +++ b/examples/index.php @@ -1,11 +1,11 @@ sum($value2); diff --git a/src/Format/NumberFormat.php b/src/Format/NumberFormat.php new file mode 100644 index 0000000..d469027 --- /dev/null +++ b/src/Format/NumberFormat.php @@ -0,0 +1,99 @@ +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; + } +} diff --git a/src/Interfaces/Format/FormatInterface.php b/src/Interfaces/Format/FormatInterface.php new file mode 100644 index 0000000..1d3c246 --- /dev/null +++ b/src/Interfaces/Format/FormatInterface.php @@ -0,0 +1,10 @@ +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 @@ -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 diff --git a/tests-executed.txt b/tests-executed.txt index 7e6fd57..6b2c0cf 100644 --- a/tests-executed.txt +++ b/tests-executed.txt @@ -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 diff --git a/tests/TypeValue/NumberTest.php b/tests/TypeValue/NumberTest.php index f5da48f..b123344 100644 --- a/tests/TypeValue/NumberTest.php +++ b/tests/TypeValue/NumberTest.php @@ -1,5 +1,6 @@ value()); } public function testCreateWithInt() { - $value = Number::with(10); + $value = Number::create(10); static::assertInstanceOf(Number::class, $value); static::assertEquals(10, $value->value()); } public function testCreateWithFloat() { - $value = Number::with(10.0); + $value = Number::create(10.0); static::assertInstanceOf(Number::class, $value); static::assertEquals(10, $value->value()); } @@ -33,18 +34,11 @@ public function testSetValue() static::assertInstanceOf(Number::class, $value); static::assertEquals(7, $value->value()); } - - public function testGetValue() - { - $value = new Number(12); - $value2 = $value->get(); - static::assertInstanceOf(Number::class, $value2); - } public function testMultiplication() { - $value1 = Number::with(2000.50); - $value2 = Number::with(2); + $value1 = Number::create(2000.50); + $value2 = Number::create(2); $result = $value1->multiplication($value2)->sum($value2); static::assertInstanceOf(Number::class, $value1); @@ -57,8 +51,8 @@ public function testMultiplication() public function testSubtraction() { - $value1 = Number::with(10); - $value2 = Number::with(2); + $value1 = Number::create(10); + $value2 = Number::create(2); $result = $value1->subtraction($value2); static::assertInstanceOf(Number::class, $value1); @@ -71,10 +65,10 @@ public function testSubtraction() public function testSum() { - $value1 = Number::with(10.5); - $value2 = Number::with(2.5); - $value3 = Number::with('7.5'); - $value4 = Number::with('0,5'); + $value1 = Number::create(10.5); + $value2 = Number::create(2.5); + $value3 = Number::create('7.5'); + $value4 = Number::create('0,5'); $result = $value1->sum($value2)->sum($value3)->subtraction($value4); static::assertInstanceOf(Number::class, $value1); @@ -85,9 +79,9 @@ public function testSum() public function testSumZero() { - $value1 = Number::with('0,00'); - $value2 = Number::with(28.001); - $result = $value1->sum($value2)->sum(Number::with('0.00')); + $value1 = Number::create('0,00'); + $value2 = Number::create(28.001); + $result = $value1->sum($value2)->sum(Number::create('0.00')); static::assertInstanceOf(Number::class, $value1); static::assertEquals(0, $value1->value()); @@ -96,8 +90,8 @@ public function testSumZero() public function testDivider() { - $value1 = Number::with(10); - $value2 = Number::with(2); + $value1 = Number::create(10); + $value2 = Number::create(2); $result = $value1->divider($value2); static::assertInstanceOf(Number::class, $value1); @@ -111,7 +105,7 @@ public function testFailCreateWithTextEmpty() static::expectException(InvalidArgumentException::class); static::expectExceptionMessage("Value not to be empty"); - Number::with(''); + Number::create(''); } public function testFailCreateWithNull() @@ -119,7 +113,7 @@ public function testFailCreateWithNull() static::expectException(InvalidArgumentException::class); static::expectExceptionMessage("Value need to be a number type"); - Number::with(null); + Number::create(null); } public function testFailCreateWithWord() @@ -127,7 +121,7 @@ public function testFailCreateWithWord() static::expectException(InvalidArgumentException::class); static::expectExceptionMessage("Value need to be a number type"); - Number::with('word'); + Number::create('word'); } public function testFailDividerByZero() @@ -135,8 +129,29 @@ public function testFailDividerByZero() static::expectException(InvalidArgumentException::class); static::expectExceptionMessage("Value not to be zero"); - $value1 = Number::with(0); - $value2 = Number::with(0); + $value1 = Number::create(0); + $value2 = Number::create(0); $value1->divider($value2); } + + public function testFormatSettingClass() + { + $value1 = Number::create(2000.5); + $value2 = Number::create(20500.5); + $value3 = Number::create(10.558); + $value4 = Number::create(8592759); + $value5 = Number::create(4.5848); + $value6 = Number::create(31234567894.01); + $value7 = Number::create(0.5); + + $format = NumberFormat::create(',', '.', 2, 'R$'); + + static::assertEquals('R$ 2.000,50', $value1->format($format)); + static::assertEquals('R$ 20.500,50', $value2->format($format)); + static::assertEquals('R$ 10,56', $value3->format($format)); + static::assertEquals('R$ 8.592.759,00', $value4->format($format)); + static::assertEquals('R$ 4,58', $value5->format($format)); + static::assertEquals('R$ 31.234.567.894,01', $value6->format($format)); + static::assertEquals('R$ 0,50', $value7->format($format)); + } }