Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8bed9c2
Created shell to work with Decimal part as integer
Jul 15, 2019
d7486b4
Parte entera de Number
Sikay Jul 15, 2019
c4653f3
Parte entera de Number
Sikay Jul 15, 2019
f40790e
Make the Decimal class, create validations for the number class and a…
Jul 16, 2019
d8f726e
Making a small refactor on the Number class to work with the Decimal …
Jul 16, 2019
ace1ca4
Created some test to the Deciamal Class and how it should behaviour
Jul 16, 2019
481a069
Added a test with a random String
Jul 16, 2019
af3f813
Small refactoring, modified construct to private, created an interfac…
Sikay Jul 16, 2019
4fca211
Validation by regex, name changes to integer and created exception to…
Sikay Jul 18, 2019
a10f5ac
Added tests for the new integer class
Sikay Jul 18, 2019
7b71be4
WIP: Added new test and modified the error. Eliminated space in metho…
Sikay Jul 19, 2019
7f0202e
decimal: modify the name of the decimal interface
khru Jul 20, 2019
8280500
Merge pull request #1 from WeDevCompany/decimal
Sikay Jul 20, 2019
f56174c
Updated: integer for cleaning zeros, number for changes in integer an…
Sikay Jul 24, 2019
d4a2419
Merge branch 'master' into integer
Sikay Jul 24, 2019
23b8053
Merge conflicts
Sikay Jul 24, 2019
cad2cb3
Parte entera de Number
Sikay Jul 15, 2019
798b339
Small refactoring, modified construct to private, created an interfac…
Sikay Jul 16, 2019
65290a1
Validation by regex, name changes to integer and created exception to…
Sikay Jul 18, 2019
9378192
Added tests for the new integer class
Sikay Jul 18, 2019
b5a390b
WIP: Added new test and modified the error. Eliminated space in metho…
Sikay Jul 19, 2019
40a94bd
Updated: integer for cleaning zeros, number for changes in integer an…
Sikay Jul 24, 2019
2ed84a9
Merge conflicts
Sikay Jul 24, 2019
2c04c42
Merge branch 'integer' of https://github.com/WeDevCompany/Money into …
Sikay Jul 30, 2019
a7068e0
prueba github actions
Sikay Apr 22, 2020
f04971f
Merge branch 'integer' of https://github.com/WeDevCompany/Money into …
Sikay Apr 22, 2020
50619d6
prueba actions
Sikay Apr 22, 2020
6fbc6e1
subiendo action con contenido
Sikay Apr 22, 2020
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
14 changes: 14 additions & 0 deletions Src/Domain/.github/workflows/cicd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: CICD

on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Run composer install
run: composer install -n --prefer-dist
- name: Run tests
run: ./vendor/bin/phpunit
87 changes: 87 additions & 0 deletions Src/Domain/Decimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace WeDev\Price\Domain;

use WeDev\Price\Domain\Exception\DecimalInvalidArgument;

class Decimal implements NumericPartInterface, DecimalInterface
{
private const EMPTY_STRING = '';
private const VALIDATOR_REGEX = '/^[\d]+$/m';

private $decimal;

private function __construct(string $decimal)
{
$this->setDecimal($decimal);
}

public static function fromString(string $decimal): self
{
return new self($decimal);
}

private function setDecimal(string $decimal): void
{
if (!$this->validateDecimal($decimal)) {
throw new DecimalInvalidArgument(
sprintf('Invalid decimal part %1$s. Is not numeric', $decimal)
);
}
$this->decimal = $decimal;
}

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

public function getDecimals(): string
{
return $this->decimal;
}

public function equals(DecimalInterface $numericPart): bool
{
return $this->getDecimals() === $numericPart->getDecimals();
}

public function __invoke(): string
{
return $this->getDecimals();
}

private function validateDecimal(string $number): bool
{
if (self::EMPTY_STRING === $number) {
return true;
}
$this->mustBeNumeric($number);

return $this->validValueForADecimal($number);
}

private function mustBeNumeric(string $number)
{
if (!is_numeric($number)) {
throw new DecimalInvalidArgument(
sprintf('Invalid decimal part %1$s. Is not numeric', $number)
);
}
}

private function validValueForADecimal($number): bool
{
try {
$number = (string) $number;
} catch (\Exception $e) {
throw new DecimalInvalidArgument(
sprintf('Invalid decimal part %1$s. Is not numeric', $number)
);
}

return (bool) preg_match_all(self::VALIDATOR_REGEX, $number, $matches, PREG_SET_ORDER, 0);
}
}
12 changes: 12 additions & 0 deletions Src/Domain/DecimalInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace WeDev\Price\Domain;

interface DecimalInterface
{
public function getDecimals(): string;

public function equals(DecimalInterface $decimalPart): bool;
}
7 changes: 7 additions & 0 deletions Src/Domain/Exception/DecimalInvalidArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace WeDev\Price\Domain\Exception;

class DecimalInvalidArgument extends \InvalidArgumentException
{
}
8 changes: 8 additions & 0 deletions Src/Domain/Exception/IntegerInvalidArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace WeDev\Price\Domain\Exception;


class IntegerInvalidArgument extends \InvalidArgumentException
{
}
7 changes: 7 additions & 0 deletions Src/Domain/Exception/NumberInvalidArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace WeDev\Price\Domain\Exception;

class NumberInvalidArgument extends \InvalidArgumentException
{
}
102 changes: 102 additions & 0 deletions Src/Domain/Integer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace WeDev\Price\Domain;

use WeDev\Price\Domain\Exception\IntegerInvalidArgument;

class Integer implements IntegerInterface
{
private $integer;

private const EMPTY_STRING = '';
private const ZERO_STRING = '0';
private const INVALID_INTEGER_PART_MGS = 'Invalid integer part %1$s';
private const EMPTY_STRING_MGS = 'Integer part can not be empty';
private const VALIDATOR_REGEX = '/^[-+]?[\d]+$/';
private const SIGN_REGEX = '/^[-+]/';
private const LEADING_ZERO_WITH_SIGN_VALIDATOR_REGEX = '/^[-+][0]+/';

private function __construct(string $integer)
{
$this->setInteger($integer);
}

public static function fromString(string $integer)
{
return new self($integer);
}

private function setInteger(string $integer)
{
$this->validateInteger($integer);
$this->integer = $this->cleanLeadingZero($integer);
}

private function validateInteger(string $integer)
{
if ($this->isIntegerEmpty($integer)) {
throw new IntegerInvalidArgument(
sprintf(self::EMPTY_STRING_MGS, $integer)
);
}
$this->validValueForAInteger($integer);
}

private function validValueForAInteger($integer)
{
if (!preg_match(self::VALIDATOR_REGEX, $integer)) {
throw new IntegerInvalidArgument(
sprintf(self::INVALID_INTEGER_PART_MGS, $integer)
);
}
}

private function isIntegerEmpty($integer): bool
{
if (self::EMPTY_STRING === $integer) {
return true;
}
return false;
}

private function cleanLeadingZero($integer): string
{
if (preg_match(self::LEADING_ZERO_WITH_SIGN_VALIDATOR_REGEX, $integer)) {
$integer = $integer[0] . ltrim(substr($integer, 1), self::ZERO_STRING);
} else {
$integer = ltrim($integer, self::ZERO_STRING);
}

if (preg_match(self::SIGN_REGEX, $integer) && strlen($integer) == 1) {
$integer = $integer[0] . self::ZERO_STRING;
}

if ($this->isIntegerEmpty($integer)) {
$integer = self::ZERO_STRING;
}

return $integer;
}

public function __invoke(): string
{
return $this->getInteger();
}

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

public function getInteger(): string
{
return $this->integer;
}

public function equals(IntegerInterface $integer): bool
{
return $this->getInteger() === $integer->getInteger();
}
}
10 changes: 10 additions & 0 deletions Src/Domain/IntegerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace WeDev\Price\Domain;

interface IntegerInterface
{
public function getInteger();

public function equals(IntegerInterface $integer);
}
97 changes: 97 additions & 0 deletions Src/Domain/IntegerPart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Created by PhpStorm.
* User: sikay
* Date: 13/07/2019
* Time: 14:00
*/
declare(strict_types=1);

namespace WeDev\Price\Domain;


class integerPart extends Numerable
{
private $integerPart;

private const DEFAULT_POSITIVE_NUMERIC_VALUE = '0';
private const NEGATIVE_SIGN = '-';

public function __construct(string $integerPart)
{
$this->setIntegerPart($integerPart);
}

private function setIntegerPart(string $integerPart)
{
$this->integerPart = $this->parseIntegerPart($integerPart);
}

private function parseIntegerPart(string $integer): string
{
$default = $this->defaultValuesForIntegerValidation($integer);
if (null !== $default) {
return $default;
}

$this->checkNumber($integer);

return $integer;
}

private function defaultValuesForIntegerValidation(string $integer): ?string
{
if (self::EMPTY_STRING === $integer || self::DEFAULT_POSITIVE_NUMERIC_VALUE === $integer) {
return self::DEFAULT_POSITIVE_NUMERIC_VALUE;
}

if (self::NEGATIVE_SIGN === $integer) {
return self::NEGATIVE_SIGN . self::DEFAULT_POSITIVE_NUMERIC_VALUE;
}

return null;
}

private function checkNumber(string $integer): void
{
$nonZero = false;
$characters = strlen($integer);
for ($position = 0; $position < $characters; ++$position) {
$digit = $integer[$position];

if (!isset(self::VALID_NUMBERS[$digit]) && !(0 === $position && self::NEGATIVE_SIGN === $digit)) {
throw new \InvalidArgumentException(
sprintf('Invalid integer part %1$s. Invalid digit %2$s found', $integer, $digit)
);
}

if (false === $nonZero && '0' === $digit) {
throw new \InvalidArgumentException(
'Leading zeros are not allowed'
);
}

$nonZero = true;
}
}

public function isNegative(): bool
{
return self::NEGATIVE_SIGN === $this->integerPart[0];
}

public function __invoke(): string
{
$this->getIntegerPart();
}

public function getIntegerPart(): string
{
return $this->integerPart;
}

public function equals (self $integerPart): bool
{
return $this->getIntegerPart() === $integerPart->getIntegerPart();
}
}
Loading