From 81f3dfe475d72df01d0a91d262b2784bafdae8d7 Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Fri, 21 Oct 2022 17:10:11 +0200 Subject: [PATCH 1/7] Cleaning --- component/Abstract/Aggregate.php | 8 ------ component/Abstract/Entity.php | 8 ------ component/Abstract/Enum.php | 38 ----------------------------- component/Abstract/Value.php | 31 ----------------------- test/Abstract/AggregateTest.php | 31 ----------------------- test/Abstract/EntityTest.php | 26 -------------------- test/Abstract/EnumTest.php | 31 ----------------------- test/Abstract/Fixture/Aggregate.php | 37 ---------------------------- test/Abstract/Fixture/Entity.php | 23 ----------------- test/Abstract/Fixture/Enum.php | 17 ------------- test/Abstract/Fixture/Value.php | 9 ------- test/Abstract/ValueTest.php | 19 --------------- 12 files changed, 278 deletions(-) delete mode 100644 component/Abstract/Aggregate.php delete mode 100644 component/Abstract/Entity.php delete mode 100644 component/Abstract/Enum.php delete mode 100644 component/Abstract/Value.php delete mode 100644 test/Abstract/AggregateTest.php delete mode 100644 test/Abstract/EntityTest.php delete mode 100644 test/Abstract/EnumTest.php delete mode 100644 test/Abstract/Fixture/Aggregate.php delete mode 100644 test/Abstract/Fixture/Entity.php delete mode 100644 test/Abstract/Fixture/Enum.php delete mode 100644 test/Abstract/Fixture/Value.php delete mode 100644 test/Abstract/ValueTest.php diff --git a/component/Abstract/Aggregate.php b/component/Abstract/Aggregate.php deleted file mode 100644 index f7cbbe5..0000000 --- a/component/Abstract/Aggregate.php +++ /dev/null @@ -1,8 +0,0 @@ -value = $value; - $this->label = static::VALUES[$value]; - } - - public function __toString() - { - return (string) $this->label; - } - - public function getValue(): mixed - { - return $this->value; - } - - public function getLabel(): mixed - { - return $this->label; - } -} diff --git a/component/Abstract/Value.php b/component/Abstract/Value.php deleted file mode 100644 index c2530d9..0000000 --- a/component/Abstract/Value.php +++ /dev/null @@ -1,31 +0,0 @@ -value = $value; - } - } - - public function __toString(): string - { - return (string) $this->get(); - } - - public function get(): mixed - { - return property_exists($this, 'value') ? $this->value : null; - } - - protected static function addNonBreakingSpace(string $value): string - { - return str_replace(' ', "\xC2\xA0", $value); - } -} diff --git a/test/Abstract/AggregateTest.php b/test/Abstract/AggregateTest.php deleted file mode 100644 index 6bc02bc..0000000 --- a/test/Abstract/AggregateTest.php +++ /dev/null @@ -1,31 +0,0 @@ -assertEquals('foo : OK', (string)$aggregate); - - $this->assertIsString($aggregate->getFoo()); - $this->assertEquals('foo', $aggregate->getFoo()); - - $this->assertIsBool($aggregate->getBar()); - $this->assertEquals(true, $aggregate->getBar()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Aggregate('', true); - } -} diff --git a/test/Abstract/EntityTest.php b/test/Abstract/EntityTest.php deleted file mode 100644 index d47457a..0000000 --- a/test/Abstract/EntityTest.php +++ /dev/null @@ -1,26 +0,0 @@ -assertIsObject($entity->foo); - $this->assertIsObject($entity->bar); - } -} diff --git a/test/Abstract/EnumTest.php b/test/Abstract/EnumTest.php deleted file mode 100644 index 8d0189b..0000000 --- a/test/Abstract/EnumTest.php +++ /dev/null @@ -1,31 +0,0 @@ -assertEquals(Enum::VALUES[ Enum::BAR ], (string)$enum); - - $this->assertIsString($enum->getValue()); - $this->assertEquals(Enum::BAR, $enum->getValue()); - - $this->assertIsString($enum->getLabel()); - $this->assertEquals(Enum::VALUES[ Enum::BAR ], $enum->getLabel()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Enum('unknown'); - } -} diff --git a/test/Abstract/Fixture/Aggregate.php b/test/Abstract/Fixture/Aggregate.php deleted file mode 100644 index 733f774..0000000 --- a/test/Abstract/Fixture/Aggregate.php +++ /dev/null @@ -1,37 +0,0 @@ -foo = $foo; - $this->bar = $bar; - } - - public function getFoo(): string - { - return $this->foo; - } - - public function getBar(): bool - { - return $this->bar; - } - - public function __toString(): string - { - return (string) $this->foo . ' : ' . ($this->bar ? 'OK' : 'KO'); - } -} diff --git a/test/Abstract/Fixture/Entity.php b/test/Abstract/Fixture/Entity.php deleted file mode 100644 index 58e6746..0000000 --- a/test/Abstract/Fixture/Entity.php +++ /dev/null @@ -1,23 +0,0 @@ -foo = $foo; - $this->bar = $bar; - } -} diff --git a/test/Abstract/Fixture/Enum.php b/test/Abstract/Fixture/Enum.php deleted file mode 100644 index f28a1ea..0000000 --- a/test/Abstract/Fixture/Enum.php +++ /dev/null @@ -1,17 +0,0 @@ - 'Foo', - self::BAR => 'Bar', - ]; -} diff --git a/test/Abstract/Fixture/Value.php b/test/Abstract/Fixture/Value.php deleted file mode 100644 index fea902a..0000000 --- a/test/Abstract/Fixture/Value.php +++ /dev/null @@ -1,9 +0,0 @@ -assertEquals('foo bar', (string)$value->get()); - - $this->assertIsString($value->get()); - $this->assertEquals('foo bar', $value->get()); - } -} From 3ddf8a4c64808bf0d96f10bf5711dea23d3890fb Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Fri, 21 Oct 2022 17:12:15 +0200 Subject: [PATCH 2/7] Config --- .gitignore | 3 ++- composer.json | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index bd57042..86377a2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,9 @@ vendor/* # Test .phpunit* -public/code-coverage +.public # Dev .DS_Store .nova/* +.php-cs-fixer.cache diff --git a/composer.json b/composer.json index 5bfa542..3b37281 100644 --- a/composer.json +++ b/composer.json @@ -10,19 +10,21 @@ } ], "require": { - "php": "^8.0", + "php": ">=8.1", "phant/error": "1.*", "ramsey/uuid": "^4.3", "firebase/php-jwt": "^5.2" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.0", "phpstan/phpstan": "^1.4", "phpunit/phpunit": "^9.5" }, "scripts": { + "lint": "vendor/bin/php-cs-fixer fix ./ --rules=@PSR12", "analyse": "vendor/bin/phpstan analyse component test -c phpstan.neon --memory-limit=4G", "test": "vendor/bin/phpunit test --testdox", - "code-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html public/code-coverage" + "coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html .public/code-coverage" }, "autoload": { "psr-4": { From 382e5fe87357306bb710ad14bda1d7a499290aca Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Fri, 21 Oct 2022 17:44:43 +0200 Subject: [PATCH 3/7] Cleaning --- component/Person/Person.php | 54 ------------------------------------- test/Person/PersonTest.php | 44 ------------------------------ 2 files changed, 98 deletions(-) delete mode 100644 component/Person/Person.php delete mode 100644 test/Person/PersonTest.php diff --git a/component/Person/Person.php b/component/Person/Person.php deleted file mode 100644 index 4349255..0000000 --- a/component/Person/Person.php +++ /dev/null @@ -1,54 +0,0 @@ -lastname = $lastname; - $this->firstname = $firstname; - $this->gender = $gender; - $this->birthday = $birthday; - } -} diff --git a/test/Person/PersonTest.php b/test/Person/PersonTest.php deleted file mode 100644 index 39e36f6..0000000 --- a/test/Person/PersonTest.php +++ /dev/null @@ -1,44 +0,0 @@ -assertIsObject($person->lastname); - $this->assertEquals('DOE', (string)$person->lastname); - - $this->assertIsObject($person->firstname); - $this->assertEquals('John', (string)$person->firstname); - - $this->assertIsObject($person->gender); - $this->assertEquals('Male', (string)$person->gender); - - $this->assertIsObject($person->birthday); - $this->assertEquals('1970-01-01', (string)$person->birthday); - } -} From f18d908cc9b743cf9c96919952e3f8b15302bed4 Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Fri, 21 Oct 2022 17:56:36 +0200 Subject: [PATCH 4/7] Hello world ! --- component/Abstract/Collection.php | 95 ++-- component/Abstract/CollectionPaginated.php | 170 +++---- component/Abstract/Value/Boolean.php | 18 +- component/Abstract/Value/Decimal.php | 18 +- component/Abstract/Value/Integer.php | 18 +- component/Abstract/Value/Varchar.php | 37 +- component/Company/Fr/CodeActivite.php | 19 +- component/Company/Fr/Siren.php | 73 +-- component/Company/Fr/Siret.php | 125 ++--- component/Company/Name.php | 17 +- component/Geography/Fr/CodeCommune.php | 41 +- component/Geography/Fr/CodePostal.php | 23 +- component/Geography/Fr/NumeroDepartement.php | 25 +- component/Geography/GpsCoordinates.php | 333 +++++++------ component/Id/Uuid.php | 35 +- component/Money/Currency.php | 48 +- component/Money/Price.php | 90 ++-- component/Number/Grade.php | 38 ++ component/Number/Note.php | 41 -- component/Number/Rate.php | 9 +- component/Person/Birthday.php | 11 + component/Person/Firstname.php | 29 +- component/Person/Gender.php | 21 +- component/Person/Lastname.php | 27 +- component/Time/Date.php | 50 +- component/Time/DateInterval.php | 116 +++-- component/Time/DateTime.php | 29 +- component/Time/DateTimeInterval.php | 116 +++-- component/Time/Duration.php | 190 ++++---- component/Token/Jwt.php | 53 +-- component/Web/DomainName.php | 57 +-- component/Web/Email.php | 83 ++-- component/Web/EmailAddress.php | 64 +-- component/Web/EmailAddressAndName.php | 43 +- component/Web/Url.php | 441 +++++++++--------- component/Web/UserName.php | 22 +- test/Abstract/CollectionPaginatedTest.php | 133 +++--- test/Abstract/CollectionTest.php | 75 ++- test/Abstract/Fixture/Collection.php | 21 +- test/Abstract/Fixture/CollectionPaginated.php | 21 +- test/Abstract/Fixture/Value/Boolean.php | 1 + test/Abstract/Fixture/Value/Decimal.php | 1 + test/Abstract/Fixture/Value/Integer.php | 1 + test/Abstract/Fixture/Value/Varchar.php | 3 +- test/Abstract/Value/BooleanTest.php | 17 +- test/Abstract/Value/DecimalTest.php | 17 +- test/Abstract/Value/IntegerTest.php | 17 +- test/Abstract/Value/VarcharTest.php | 35 +- test/Company/Fr/CodeActiviteTest.php | 33 +- test/Company/Fr/SirenTest.php | 39 +- test/Company/Fr/SiretTest.php | 57 +-- test/Company/NameTest.php | 33 +- test/Geography/Fr/CodeCommuneTest.php | 39 +- test/Geography/Fr/CodePostalTest.php | 33 +- test/Geography/Fr/NumeroDepartementTest.php | 33 +- test/Geography/GpsCoordinatesTest.php | 71 +-- test/Id/UuidTest.php | 47 +- test/Money/CurrencyTest.php | 32 +- test/Money/PriceTest.php | 35 +- test/Number/GradeTest.php | 39 ++ test/Number/NoteTest.php | 38 -- test/Number/RateTest.php | 19 +- test/Person/FirstnameTest.php | 33 +- test/Person/GenderTest.php | 32 +- test/Person/LastnameTest.php | 33 +- test/Time/DateIntervalTest.php | 93 ++-- test/Time/DateTest.php | 73 +-- test/Time/DateTimeIntervalTest.php | 93 ++-- test/Time/DateTimeTest.php | 79 ++-- test/Time/DurationTest.php | 27 +- test/Token/JwtTest.php | 79 ++-- test/Web/DomainNameTest.php | 51 +- test/Web/EmailAddressAndNameTest.php | 27 +- test/Web/EmailAddressTest.php | 73 +-- test/Web/EmailTest.php | 113 ++--- test/Web/UrlTest.php | 120 ++--- test/Web/UserNameTest.php | 33 +- 77 files changed, 2213 insertions(+), 2261 deletions(-) create mode 100644 component/Number/Grade.php delete mode 100644 component/Number/Note.php create mode 100644 component/Person/Birthday.php create mode 100644 test/Number/GradeTest.php delete mode 100644 test/Number/NoteTest.php diff --git a/component/Abstract/Collection.php b/component/Abstract/Collection.php index aefdc57..fefce3d 100644 --- a/component/Abstract/Collection.php +++ b/component/Abstract/Collection.php @@ -1,55 +1,56 @@ items = []; - } - - protected function addItem(mixed $item): self - { - if (array_search($item, $this->items) == false) { - $this->items[] = $item; - } - - return $this; - } - - protected function removeItem(mixed $item): self - { - if (($key = array_search($item, $this->items)) !== false) { - unset($this->items[ $key ]); - $this->items = array_values($this->items); - } - - return $this; - } - - public function itemsIterator(): \Generator - { - foreach ($this->items as $item) { - yield $item; - } - } - - public function isEmpty(): bool - { - return empty($this->items); - } - - public function getNbItems(): int - { - return count($this->items); - } - - public function getByKey(int $key): mixed - { - return $this->items[ $key ] ?? null; - } + protected array $items; + + public function __construct() + { + $this->items = []; + } + + protected function addItem(mixed $item): self + { + if (array_search($item, $this->items) == false) { + $this->items[] = $item; + } + + return $this; + } + + protected function removeItem(mixed $item): self + { + if (($key = array_search($item, $this->items)) !== false) { + unset($this->items[ $key ]); + $this->items = array_values($this->items); + } + + return $this; + } + + public function itemsIterator(): \Generator + { + foreach ($this->items as $item) { + yield $item; + } + } + + public function isEmpty(): bool + { + return empty($this->items); + } + + public function getNbItems(): int + { + return count($this->items); + } + + public function getByKey(int $key): mixed + { + return $this->items[ $key ] ?? null; + } } diff --git a/component/Abstract/CollectionPaginated.php b/component/Abstract/CollectionPaginated.php index 65b5c00..4822997 100644 --- a/component/Abstract/CollectionPaginated.php +++ b/component/Abstract/CollectionPaginated.php @@ -1,4 +1,5 @@ itemByPage = $itemByPage; - $this->itemTotal = $itemTotal; - $this->itemPage = null; - - $this->pageCurrent = $pageCurrent; - $this->pageTotal = null; - - parent::__construct(); - } - - public function getItemByPage(): ?int - { - return $this->itemByPage; - } - - public function getItemTotal(): ?int - { - return $this->itemTotal; - } - - public function getItemPage(): ?int - { - return $this->itemPage; - } - - public function getPageCurrent(): ?int - { - return $this->pageCurrent; - } - - public function getPageTotal(): ?int - { - return $this->pageTotal; - } - - protected function addItem(mixed $item): self - { - parent::addItem($item); - - $this->paginationCalculation(); - - return $this; - } - - protected function removeItem(mixed $item): self - { - parent::removeItem($item); - - $this->paginationCalculation(); - - return $this; - } - - protected function paginationCalculation(): void - { - $this->itemPageCalculation(); - $this->pageTotalCalculation(); - } - - private function itemPageCalculation(): void - { - $this->itemPage = $this->getNbItems(); - } - - private function pageTotalCalculation(): void - { - $this->pageTotal = 0; - - if ($this->itemTotal && $this->itemByPage) { - $this->pageTotal = (int)ceil($this->itemTotal / $this->itemByPage); - } - } + private ?int $itemPage; + private ?int $itemByPage; + private ?int $itemTotal; + + private ?int $pageCurrent; + private ?int $pageTotal; + + public function __construct( + ?int $pageCurrent = null, + ?int $itemByPage = null, + ?int $itemTotal = null + ) { + $this->itemByPage = $itemByPage; + $this->itemTotal = $itemTotal; + $this->itemPage = null; + + $this->pageCurrent = $pageCurrent; + $this->pageTotal = null; + + parent::__construct(); + } + + public function getItemByPage(): ?int + { + return $this->itemByPage; + } + + public function getItemTotal(): ?int + { + return $this->itemTotal; + } + + public function getItemPage(): ?int + { + return $this->itemPage; + } + + public function getPageCurrent(): ?int + { + return $this->pageCurrent; + } + + public function getPageTotal(): ?int + { + return $this->pageTotal; + } + + protected function addItem(mixed $item): self + { + parent::addItem($item); + + $this->paginationCalculation(); + + return $this; + } + + protected function removeItem(mixed $item): self + { + parent::removeItem($item); + + $this->paginationCalculation(); + + return $this; + } + + protected function paginationCalculation(): void + { + $this->itemPageCalculation(); + $this->pageTotalCalculation(); + } + + private function itemPageCalculation(): void + { + $this->itemPage = $this->getNbItems(); + } + + private function pageTotalCalculation(): void + { + $this->pageTotal = 0; + + if ($this->itemTotal && $this->itemByPage) { + $this->pageTotal = (int)ceil($this->itemTotal / $this->itemByPage); + } + } } diff --git a/component/Abstract/Value/Boolean.php b/component/Abstract/Value/Boolean.php index 33f8a6d..b887865 100644 --- a/component/Abstract/Value/Boolean.php +++ b/component/Abstract/Value/Boolean.php @@ -1,19 +1,13 @@ value; + } - public function get(): string - { - return parent::get(); - } + public function addNonBreakingSpace(): string + { + return str_replace(' ', "\xC2\xA0", $this->value); + } } diff --git a/component/Company/Fr/CodeActivite.php b/component/Company/Fr/CodeActivite.php index 8feb8e9..9037f31 100755 --- a/component/Company/Fr/CodeActivite.php +++ b/component/Company/Fr/CodeActivite.php @@ -1,17 +1,18 @@ = 0; $i--) { - $add = $flag++ & 1 ? $value[$i] * 2 : $value[$i]; - $sum += $add > 9 ? $add - 9 : $add; - } - - return $sum % 10 === 0; - } - - public function getFormatted(bool $espaceInsecable = true): string - { - $siren = $this->value; - $siren = preg_replace('/^(\d{3})(\d{3})(\d{3})$/', '$1 $2 $3', $siren); - if ($espaceInsecable) { - $siren = str_replace(' ', "\xC2\xA0", $siren); // Espace insécable - } - - return $siren; - } + public const PATTERN = '/^(\d{9})$/'; + + public function __construct(string $siren, bool $check = true) + { + $siren = preg_replace('/\D/', '', $siren); + + if ($check && !self::luhnCheck($siren)) { + throw new NotCompliant('Siren : ' . $siren); + } + + parent::__construct($siren); + } + + public static function luhnCheck(string $value): bool + { + $sum = 0; + $flag = 0; + + for ($i = strlen($value) - 1; $i >= 0; $i--) { + $add = $flag++ & 1 ? $value[$i] * 2 : $value[$i]; + $sum += $add > 9 ? $add - 9 : $add; + } + + return $sum % 10 === 0; + } + + public function getFormatted(bool $espaceInsecable = true): string + { + $siren = $this->value; + $siren = preg_replace('/^(\d{3})(\d{3})(\d{3})$/', '$1 $2 $3', $siren); + if ($espaceInsecable) { + $siren = str_replace(' ', "\xC2\xA0", $siren); // Espace insécable + } + + return $siren; + } } diff --git a/component/Company/Fr/Siret.php b/component/Company/Fr/Siret.php index f05e25e..b67391b 100755 --- a/component/Company/Fr/Siret.php +++ b/component/Company/Fr/Siret.php @@ -1,4 +1,5 @@ value, 0, 9)); - } - - public function getFormatted(bool $espaceInsecable = true): string - { - $siret = $this->value; - $siret = preg_replace('/^(\d{3})(\d{3})(\d{3})(\d{5})$/', '$1 $2 $3 $4', $siret); - if ($espaceInsecable) { - $siret = str_replace(' ', "\xC2\xA0", $siret); // Espace insécable - } - - return $siret; - } - - public static function isValid(string $siret): bool - { - if (substr($siret, 0, 9) == self::SIREN_LA_POSTE) { - return self::checkLaPoste($siret); - } - - return self::luhnCheck($siret); - } - - private static function luhnCheck(string $value): bool - { - $sum = 0; - $flag = 0; - - for ($i = strlen($value) - 1; $i >= 0; $i--) { - $add = $flag++ & 1 ? $value[$i] * 2 : $value[$i]; - $sum += $add > 9 ? $add - 9 : $add; - } - - return $sum % 10 === 0; - } - - private static function checkLaPoste(string $value): bool - { - $sum = 0; - - for ($i = strlen($value) - 1; $i >= 0; $i--) { - $sum += $value[$i]; - } - - return $sum % 5 === 0; - } + public const PATTERN = '/^(\d{14})$/'; + public const SIREN_LA_POSTE = '356000000'; + + public function __construct(string $siret, bool $check = true) + { + $siret = preg_replace('/\D/', '', $siret); + + if ($check && !self::isValid($siret)) { + throw new NotCompliant('Siret : ' . $siret); + } + + parent::__construct($siret); + } + + public function getSiren(): Siren + { + return new Siren(substr($this->value, 0, 9)); + } + + public function getFormatted(bool $espaceInsecable = true): string + { + $siret = $this->value; + $siret = preg_replace('/^(\d{3})(\d{3})(\d{3})(\d{5})$/', '$1 $2 $3 $4', $siret); + if ($espaceInsecable) { + $siret = str_replace(' ', "\xC2\xA0", $siret); // Espace insécable + } + + return $siret; + } + + public static function isValid(string $siret): bool + { + if (substr($siret, 0, 9) == self::SIREN_LA_POSTE) { + return self::checkLaPoste($siret); + } + + return self::luhnCheck($siret); + } + + private static function luhnCheck(string $value): bool + { + $sum = 0; + $flag = 0; + + for ($i = strlen($value) - 1; $i >= 0; $i--) { + $add = $flag++ & 1 ? $value[$i] * 2 : $value[$i]; + $sum += $add > 9 ? $add - 9 : $add; + } + + return $sum % 10 === 0; + } + + private static function checkLaPoste(string $value): bool + { + $sum = 0; + + for ($i = strlen($value) - 1; $i >= 0; $i--) { + $sum += $value[$i]; + } + + return $sum % 5 === 0; + } } diff --git a/component/Company/Name.php b/component/Company/Name.php index 37cfa53..09506c4 100644 --- a/component/Company/Name.php +++ b/component/Company/Name.php @@ -1,16 +1,17 @@ value, 0, 2); + if (in_array($numeroDepartement, ['97', '98'])) { + $numeroDepartement = substr($this->value, 0, 3); + } - public function getNumeroDepartement(): NumeroDepartement - { - $numeroDepartement = substr($this->value, 0, 2); - if (in_array($numeroDepartement, ['97', '98'])) { - $numeroDepartement = substr($this->value, 0, 3); - } - - return new NumeroDepartement($numeroDepartement); - } + return new NumeroDepartement($numeroDepartement); + } } diff --git a/component/Geography/Fr/CodePostal.php b/component/Geography/Fr/CodePostal.php index 926308a..9aac721 100755 --- a/component/Geography/Fr/CodePostal.php +++ b/component/Geography/Fr/CodePostal.php @@ -1,19 +1,20 @@ 90 || $latitude < -90 || $longitude > 180 || $longitude < -180) { - throw new NotCompliant('GPS coordinates: ' . $latitude . ';' . $longitude); - } - - $this->latitude = $latitude; - $this->longitude = $longitude; - } - - public function getLatitude(): float - { - return $this->latitude; - } - - public function getLongitude(): float - { - return $this->longitude; - } - - public function __toString(): string - { - return (string) $this->latitude . ';' . $this->longitude; - } - - public static function createFromLambert93(float $x, float $y): static - { - $x = number_format($x, 10, '.', ''); - $y = number_format($y, 10, '.', ''); - $b6 = 6378137.0000; - $b7 = 298.257222101; - $b8 = 1 / $b7; - $b9 = 2 * $b8 - $b8 * $b8; - $b10 = sqrt($b9); - $b13 = 3.000000000; - $b14 = 700000.0000; - $b15 = 12655612.0499; - $b16 = 0.7256077650532670; - $b17 = 11754255.426096; - $delx = $x - $b14; - $dely = $y - $b15; - $gamma = atan( -( $delx ) / $dely ); - $r = sqrt( ( $delx * $delx ) + ( $dely * $dely )); - $latiso = log( $b17 / $r ) / $b16; - $sinphiit0 = tanh($latiso + $b10 * atanh($b10 * sin(1))); - $sinphiit1 = tanh($latiso + $b10 * atanh($b10 * $sinphiit0)); - $sinphiit2 = tanh($latiso + $b10 * atanh($b10 * $sinphiit1)); - $sinphiit3 = tanh($latiso + $b10 * atanh($b10 * $sinphiit2)); - $sinphiit4 = tanh($latiso + $b10 * atanh($b10 * $sinphiit3)); - $sinphiit5 = tanh($latiso + $b10 * atanh($b10 * $sinphiit4)); - $sinphiit6 = tanh($latiso + $b10 * atanh($b10 * $sinphiit5)); - $longrad = $gamma / $b16 + $b13 / 180 * pi(); - $latrad = asin($sinphiit6); - $longitude = ($longrad / pi() * 180); - $latitude = ($latrad / pi() * 180); - - $longitude = round($longitude, 7); - $latitude = round($latitude, 7); - - return new static($latitude, $longitude); - } - - public static function createFromUtm(float $x, float $y, int $zone, bool $southernHemisphere = false): static - { - $UTMCentralMeridian = function (int $zone): float - { - $degree2radian = function (float $deg): float - { - return $deg / 180.0 * pi(); - }; - - return $degree2radian(-183.0 + ($zone * 6.0)); - }; - - $FootpointLatitude = function (float $y): float - { - $sm_b = 6356752.314; - $sm_a = 6378137.0; - $UTMScaleFactor = 0.9996; - $sm_EccSquared = .00669437999013; - $n = ($sm_a - $sm_b) / ($sm_a + $sm_b); - $alpha_ = (($sm_a + $sm_b) / 2.0)* (1 + (pow($n, 2.0) / 4) + (pow($n, 4.0) / 64)); - $y_ = $y / $alpha_; - $beta_ = (3.0 * $n / 2.0) + (-27.0 * pow($n, 3.0) / 32.0)+ (269.0 * pow($n, 5.0) / 512.0); - $gamma_ = (21.0 * pow($n, 2.0) / 16.0)+ (-55.0 * pow($n, 4.0) / 32.0); - $delta_ = (151.0 * pow($n, 3.0) / 96.0)+ (-417.0 * pow($n, 5.0) / 128.0); - $epsilon_ = (1097.0 * pow($n, 4.0) / 512.0); - $result = $y_ + ($beta_ * sin(2.0 * $y_)) - + ($gamma_ * sin(4.0 * $y_)) - + ($delta_ * sin(6.0 * $y_)) - + ($epsilon_ * sin(8.0 * $y_)); - - return $result; - }; - - $radian2degree = function (float $rad): float - { - return $rad / pi() * 180.0; - }; - - $UTMScaleFactor = 0.9996; - $x -= 500000.0; - $x /= $UTMScaleFactor; - /* If in southern hemisphere, adjust y accordingly. */ - if ($southernHemisphere) $y -= 10000000.0; - $y /= $UTMScaleFactor; - $cmeridian = $UTMCentralMeridian($zone); - - $philambda = []; - $sm_b = 6356752.314; - $sm_a = 6378137.0; - $UTMScaleFactor = 0.9996; - $sm_EccSquared = .00669437999013; - $phif = $FootpointLatitude($y); - $ep2 = (pow($sm_a, 2.0) - pow($sm_b, 2.0)) / pow($sm_b, 2.0); - $cf = cos($phif); - $nuf2 = $ep2 * pow($cf, 2.0); - $Nf = pow($sm_a, 2.0) / ($sm_b * sqrt(1 + $nuf2)); - $Nfpow = $Nf; - $tf = tan($phif); - $tf2 = $tf * $tf; - $tf4 = $tf2 * $tf2; - $x1frac = 1.0 / ($Nfpow * $cf); - $Nfpow *= $Nf; - $x2frac = $tf / (2.0 * $Nfpow); - $Nfpow *= $Nf; - $x3frac = 1.0 / (6.0 * $Nfpow * $cf); - $Nfpow *= $Nf; - $x4frac = $tf / (24.0 * $Nfpow); - $Nfpow *= $Nf; - $x5frac = 1.0 / (120.0 * $Nfpow * $cf); - $Nfpow *= $Nf; - $x6frac = $tf / (720.0 * $Nfpow); - $Nfpow *= $Nf; - $x7frac = 1.0 / (5040.0 * $Nfpow * $cf); - $Nfpow *= $Nf; - $x8frac = $tf / (40320.0 * $Nfpow); - $x2poly = -1.0 - $nuf2; - $x3poly = -1.0 - 2 * $tf2 - $nuf2; - $x4poly = 5.0 + 3.0 * $tf2 + 6.0 * $nuf2 - 6.0 * $tf2 * $nuf2- 3.0 * ($nuf2 *$nuf2) - 9.0 * $tf2 * ($nuf2 * $nuf2); - $x5poly = 5.0 + 28.0 * $tf2 + 24.0 * $tf4 + 6.0 * $nuf2 + 8.0 * $tf2 * $nuf2; - $x6poly = -61.0 - 90.0 * $tf2 - 45.0 * $tf4 - 107.0 * $nuf2 + 162.0 * $tf2 * $nuf2; - $x7poly = -61.0 - 662.0 * $tf2 - 1320.0 * $tf4 - 720.0 * ($tf4 * $tf2); - $x8poly = 1385.0 + 3633.0 * $tf2 + 4095.0 * $tf4 + 1575 * ($tf4 * $tf2); - - $latitude = $radian2degree( - $phif + $x2frac * $x2poly * ($x * $x) - + $x4frac * $x4poly * pow($x, 4.0) - + $x6frac * $x6poly * pow($x, 6.0) - + $x8frac * $x8poly * pow($x, 8.0) - ); - - $longitude = $radian2degree( - $cmeridian + $x1frac * $x - + $x3frac * $x3poly * pow($x, 3.0) - + $x5frac * $x5poly * pow($x, 5.0) - + $x7frac * $x7poly * pow($x, 7.0) - ); - - $longitude = round($longitude, 7); - $latitude = round($latitude, 7); - - return new static($latitude, $longitude); - } + // Format : WGS84 (https://en.wikipedia.org/wiki/World_Geodetic_System) + final public function __construct( + public readonly float $latitude, + public readonly float $longitude + ) { + if ($latitude > 90 || $latitude < -90 || $longitude > 180 || $longitude < -180) { + throw new NotCompliant('GPS coordinates: ' . $latitude . ';' . $longitude); + } + } + + public function __toString(): string + { + return (string) $this->latitude . ';' . $this->longitude; + } + + public static function make(string $coordinates): self + { + $parts = explode(';', $coordinates); + + return new static( + (float) $parts[0], + (float) $parts[1] + ); + } + + public static function makeFromLambert93(float $x, float $y): static + { + $x = number_format($x, 10, '.', ''); + $y = number_format($y, 10, '.', ''); + $b6 = 6378137.0000; + $b7 = 298.257222101; + $b8 = 1 / $b7; + $b9 = 2 * $b8 - $b8 * $b8; + $b10 = sqrt($b9); + $b13 = 3.000000000; + $b14 = 700000.0000; + $b15 = 12655612.0499; + $b16 = 0.7256077650532670; + $b17 = 11754255.426096; + $delx = $x - $b14; + $dely = $y - $b15; + $gamma = atan(-($delx) / $dely); + $r = sqrt(($delx * $delx) + ($dely * $dely)); + $latiso = log($b17 / $r) / $b16; + $sinphiit0 = tanh($latiso + $b10 * atanh($b10 * sin(1))); + $sinphiit1 = tanh($latiso + $b10 * atanh($b10 * $sinphiit0)); + $sinphiit2 = tanh($latiso + $b10 * atanh($b10 * $sinphiit1)); + $sinphiit3 = tanh($latiso + $b10 * atanh($b10 * $sinphiit2)); + $sinphiit4 = tanh($latiso + $b10 * atanh($b10 * $sinphiit3)); + $sinphiit5 = tanh($latiso + $b10 * atanh($b10 * $sinphiit4)); + $sinphiit6 = tanh($latiso + $b10 * atanh($b10 * $sinphiit5)); + $longrad = $gamma / $b16 + $b13 / 180 * pi(); + $latrad = asin($sinphiit6); + $longitude = ($longrad / pi() * 180); + $latitude = ($latrad / pi() * 180); + + $longitude = round($longitude, 7); + $latitude = round($latitude, 7); + + return new static($latitude, $longitude); + } + + public static function makeFromUtm(float $x, float $y, int $zone, bool $southernHemisphere = false): static + { + $UTMCentralMeridian = function (int $zone): float { + $degree2radian = function (float $deg): float { + return $deg / 180.0 * pi(); + }; + + return $degree2radian(-183.0 + ($zone * 6.0)); + }; + + $FootpointLatitude = function (float $y): float { + $sm_b = 6356752.314; + $sm_a = 6378137.0; + $UTMScaleFactor = 0.9996; + $sm_EccSquared = .00669437999013; + $n = ($sm_a - $sm_b) / ($sm_a + $sm_b); + $alpha_ = (($sm_a + $sm_b) / 2.0)* (1 + (pow($n, 2.0) / 4) + (pow($n, 4.0) / 64)); + $y_ = $y / $alpha_; + $beta_ = (3.0 * $n / 2.0) + (-27.0 * pow($n, 3.0) / 32.0)+ (269.0 * pow($n, 5.0) / 512.0); + $gamma_ = (21.0 * pow($n, 2.0) / 16.0)+ (-55.0 * pow($n, 4.0) / 32.0); + $delta_ = (151.0 * pow($n, 3.0) / 96.0)+ (-417.0 * pow($n, 5.0) / 128.0); + $epsilon_ = (1097.0 * pow($n, 4.0) / 512.0); + $result = $y_ + ($beta_ * sin(2.0 * $y_)) + + ($gamma_ * sin(4.0 * $y_)) + + ($delta_ * sin(6.0 * $y_)) + + ($epsilon_ * sin(8.0 * $y_)); + + return $result; + }; + + $radian2degree = function (float $rad): float { + return $rad / pi() * 180.0; + }; + + $UTMScaleFactor = 0.9996; + $x -= 500000.0; + $x /= $UTMScaleFactor; + /* If in southern hemisphere, adjust y accordingly. */ + if ($southernHemisphere) { + $y -= 10000000.0; + } + $y /= $UTMScaleFactor; + $cmeridian = $UTMCentralMeridian($zone); + + $philambda = []; + $sm_b = 6356752.314; + $sm_a = 6378137.0; + $UTMScaleFactor = 0.9996; + $sm_EccSquared = .00669437999013; + $phif = $FootpointLatitude($y); + $ep2 = (pow($sm_a, 2.0) - pow($sm_b, 2.0)) / pow($sm_b, 2.0); + $cf = cos($phif); + $nuf2 = $ep2 * pow($cf, 2.0); + $Nf = pow($sm_a, 2.0) / ($sm_b * sqrt(1 + $nuf2)); + $Nfpow = $Nf; + $tf = tan($phif); + $tf2 = $tf * $tf; + $tf4 = $tf2 * $tf2; + $x1frac = 1.0 / ($Nfpow * $cf); + $Nfpow *= $Nf; + $x2frac = $tf / (2.0 * $Nfpow); + $Nfpow *= $Nf; + $x3frac = 1.0 / (6.0 * $Nfpow * $cf); + $Nfpow *= $Nf; + $x4frac = $tf / (24.0 * $Nfpow); + $Nfpow *= $Nf; + $x5frac = 1.0 / (120.0 * $Nfpow * $cf); + $Nfpow *= $Nf; + $x6frac = $tf / (720.0 * $Nfpow); + $Nfpow *= $Nf; + $x7frac = 1.0 / (5040.0 * $Nfpow * $cf); + $Nfpow *= $Nf; + $x8frac = $tf / (40320.0 * $Nfpow); + $x2poly = -1.0 - $nuf2; + $x3poly = -1.0 - 2 * $tf2 - $nuf2; + $x4poly = 5.0 + 3.0 * $tf2 + 6.0 * $nuf2 - 6.0 * $tf2 * $nuf2- 3.0 * ($nuf2 *$nuf2) - 9.0 * $tf2 * ($nuf2 * $nuf2); + $x5poly = 5.0 + 28.0 * $tf2 + 24.0 * $tf4 + 6.0 * $nuf2 + 8.0 * $tf2 * $nuf2; + $x6poly = -61.0 - 90.0 * $tf2 - 45.0 * $tf4 - 107.0 * $nuf2 + 162.0 * $tf2 * $nuf2; + $x7poly = -61.0 - 662.0 * $tf2 - 1320.0 * $tf4 - 720.0 * ($tf4 * $tf2); + $x8poly = 1385.0 + 3633.0 * $tf2 + 4095.0 * $tf4 + 1575 * ($tf4 * $tf2); + + $latitude = $radian2degree( + $phif + $x2frac * $x2poly * ($x * $x) + + $x4frac * $x4poly * pow($x, 4.0) + + $x6frac * $x6poly * pow($x, 6.0) + + $x8frac * $x8poly * pow($x, 8.0) + ); + + $longitude = $radian2degree( + $cmeridian + $x1frac * $x + + $x3frac * $x3poly * pow($x, 3.0) + + $x5frac * $x5poly * pow($x, 5.0) + + $x7frac * $x7poly * pow($x, 7.0) + ); + + $longitude = round($longitude, 7); + $latitude = round($latitude, 7); + + return new static($latitude, $longitude); + } } diff --git a/component/Id/Uuid.php b/component/Id/Uuid.php index aacc011..41a031e 100644 --- a/component/Id/Uuid.php +++ b/component/Id/Uuid.php @@ -1,4 +1,5 @@ ToString(); - } catch (InvalidUuidStringException $e) { - throw new NotCompliant(); - } - - parent::__construct($uuid); - } - - public static function generate(): self - { - return new static(UuidBuilder::Uuid4()->ToString()); - } + public const PATTERN = '/[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}/'; + + final public function __construct(string $uuid) + { + try { + $uuid = UuidBuilder::fromString($uuid)->ToString(); + } catch (InvalidUuidStringException $e) { + throw new NotCompliant(); + } + + parent::__construct($uuid); + } + + public static function generate(): self + { + return new static(UuidBuilder::Uuid4()->ToString()); + } } diff --git a/component/Money/Currency.php b/component/Money/Currency.php index 10b2043..afe769c 100644 --- a/component/Money/Currency.php +++ b/component/Money/Currency.php @@ -1,29 +1,33 @@ 'AUD', - self::CAD => 'CAD', - self::CHF => 'CHF', - self::DKK => 'DKK', - self::EUR => '€', - self::GBP => '£', - self::SEK => 'SEK', - self::USD => 'USD', - self::ZSD => 'ZSD', - ]; + case AUD = 'AUD'; + case CAD = 'CAD'; + case CHF = 'CHF'; + case DKK = 'DKK'; + case EUR = 'EUR'; + case GBP = 'GBP'; + case SEK = 'SEK'; + case USD = 'USD'; + case ZSD = 'ZSD'; + + public function getLabel(): string + { + return match ($this) { + self::AUD => 'AUD', + self::CAD => 'CAD', + self::CHF => 'CHF', + self::DKK => 'DKK', + self::EUR => '€', + self::GBP => '£', + self::SEK => 'SEK', + self::USD => 'USD', + self::ZSD => 'ZSD', + }; + } } diff --git a/component/Money/Price.php b/component/Money/Price.php index 9bf9962..2b14e82 100644 --- a/component/Money/Price.php +++ b/component/Money/Price.php @@ -1,67 +1,41 @@ price = $price; - $this->currency = $currency; - $this->unit = $unit; - } - - public function get(): float - { - return $this->price; - } - - public function getCurrency(): ?Currency - { - return $this->currency; - } - - public function getUnit(): ?string - { - return $this->unit; - } - - public function __toString(): string - { - return $this->getFormatted(); - } - - public function getFormatted(bool $espaceInsecable = true): string - { - $price = number_format($this->price, 2, ',', ' '); - - if (!is_null($this->currency)) { - $price.= ' ' . ((string)$this->currency); - } - - if (!is_null($this->unit)) { - $price.= '/' . ((string)$this->unit); - } - - if ($espaceInsecable) { - $price = str_replace(' ', "\xC2\xA0", $price); // Espace insécable - } - - return $price; - } + public function __construct( + public readonly float $amount, + public readonly ?Currency $currency, + public readonly ?string $unit + ) { + } + + public function __toString(): string + { + return $this->getFormatted(); + } + + public function getFormatted(bool $espaceInsecable = true): string + { + $price = number_format($this->amount, 2, ',', ' '); + + if ($this->currency) { + $price.= ' ' . $this->currency->getLabel(); + } + + if ($this->unit) { + $price.= '/' . $this->unit; + } + + if ($espaceInsecable) { + $price = str_replace(' ', "\xC2\xA0", $price); // Espace insécable + } + + return $price; + } } diff --git a/component/Number/Grade.php b/component/Number/Grade.php new file mode 100644 index 0000000..c3b3a1a --- /dev/null +++ b/component/Number/Grade.php @@ -0,0 +1,38 @@ +position . '/' . $this->scale; + } + + public static function make(string $grade): self + { + $parts = explode('/', $grade); + + return new static( + (int) $parts[0], + (int) $parts[1] + ); + } +} diff --git a/component/Number/Note.php b/component/Number/Note.php deleted file mode 100644 index 633bf3d..0000000 --- a/component/Number/Note.php +++ /dev/null @@ -1,41 +0,0 @@ -note = $note; - $this->unit = $unit; - } - - public function __toString(): string - { - return $this->note . '/' . $this->unit; - } - - public function getNote(): int - { - return $this->note; - } - - public function getUnit(): int - { - return $this->unit; - } -} diff --git a/component/Number/Rate.php b/component/Number/Rate.php index f33f308..eda5d22 100644 --- a/component/Number/Rate.php +++ b/component/Number/Rate.php @@ -1,12 +1,13 @@ get() . ' %'); - } + public function __toString() + { + return str_replace(' ', "\xC2\xA0", $this->value . ' %'); + } } diff --git a/component/Person/Birthday.php b/component/Person/Birthday.php new file mode 100644 index 0000000..7135bbb --- /dev/null +++ b/component/Person/Birthday.php @@ -0,0 +1,11 @@ + 'Female', - self::MALE => 'Male', - ]; + case Female = 'female'; + + case Male = 'male'; + + public function getLabel(): string + { + return match ($this) { + self::Female => 'Female', + self::Male => 'Male', + }; + } } diff --git a/component/Person/Lastname.php b/component/Person/Lastname.php index 04c3ee4..9110a92 100644 --- a/component/Person/Lastname.php +++ b/component/Person/Lastname.php @@ -1,4 +1,5 @@ time = $time; - - $date = date($format, $this->time); - - parent::__construct($date); - } - - public function getTime(): int - { - return $this->time; - } + public readonly int $time; + public readonly string $date; + public readonly string $format; + + public function __construct( + int|string $date, + string $format = 'Y-m-d' + ) { + if (is_string($date) && strtolower($date) == 'now') { + $date = strtotime('today midnight'); + } + + $time = is_string($date) ? strtotime($date) : $date; + + if ($time === false) { + throw new NotCompliant('Date: ' . $date); + } + + $this->time = $time; + $this->format = $format; + $this->date = date($format, $this->time); + } } diff --git a/component/Time/DateInterval.php b/component/Time/DateInterval.php index 0c825ea..a6e2ee8 100644 --- a/component/Time/DateInterval.php +++ b/component/Time/DateInterval.php @@ -1,72 +1,68 @@ from = $from; - $this->to = $to; - $this->duration = ($this->from && $this->to) ? new Duration($this->to->getTime() - $this->from->getTime()) : null; - } - - public function getFrom(): ?Date - { - return $this->from; - } - - public function getTo(): ?Date - { - return $this->to; - } - - public function getDuration(): ?Duration - { - return $this->duration; - } - - public function isDuring(string|int|Date $date): bool - { - if (is_string($date) || is_int($date)) { - $date = new Date($date); - } - - if ($this->from && $date->getTime() < $this->from->getTime()) { - return false; - } - - if ($this->to && $date->getTime() > $this->to->getTime()) { - return false; - } - - return true; - } + public readonly ?Duration $duration; + + public function __construct( + public readonly ?Date $from, + public readonly ?Date $to + ) { + if (!$from && !$to) { + throw new NotCompliant('Date intervals: from ' . $from . ' to' . $to); + } + + $this->calculateDuration(); + } + + public function isDuring(string|int|Date $date): bool + { + if (is_string($date) || is_int($date)) { + $date = new Date($date); + } + + if ($this->from && $date->time < $this->from->time) { + return false; + } + + if ($this->to && $date->time > $this->to->time) { + return false; + } + + return true; + } + + private function calculateDuration(): void + { + $this->duration = ($this->from && $this->to) ? new Duration($this->to->time - $this->from->time) : null; + } + + public static function make( + null|int|string $from, + null|int|string $to + ): self { + if (is_string($from) || is_int($from)) { + $from = new Date($from); + } + + if (is_string($to) || is_int($to)) { + $to = new Date($to); + } + + return new static( + $from, + $to + ); + } } diff --git a/component/Time/DateTime.php b/component/Time/DateTime.php index d9ef91d..a9be661 100644 --- a/component/Time/DateTime.php +++ b/component/Time/DateTime.php @@ -1,4 +1,5 @@ time + date('Z', $this->time)); - } + public function __construct( + int|string $date, + string $format = 'Y-m-d H:i:s' + ) { + if (is_string($date) && strtolower($date) == 'now') { + $date = strtotime('now'); + } + + parent::__construct($date, $format); + } + + public function getUtc(): string + { + return gmdate('Y-m-d\TH:i:s\Z', $this->time + date('Z', $this->time)); + } } diff --git a/component/Time/DateTimeInterval.php b/component/Time/DateTimeInterval.php index f42ff32..d68f0f7 100644 --- a/component/Time/DateTimeInterval.php +++ b/component/Time/DateTimeInterval.php @@ -1,72 +1,68 @@ from = $from; - $this->to = $to; - $this->duration = ($this->from && $this->to) ? new Duration($this->to->getTime() - $this->from->getTime()) : null; - } - - public function getFrom(): ?DateTime - { - return $this->from; - } - - public function getTo(): ?DateTime - { - return $this->to; - } - - public function getDuration(): ?Duration - { - return $this->duration; - } - - public function isDuring(int|string|DateTime $dateTime): bool - { - if (is_string($dateTime) || is_int($dateTime)) { - $dateTime = new DateTime($dateTime); - } - - if ($this->from && $dateTime->getTime() < $this->from->getTime()) { - return false; - } - - if ($this->to && $dateTime->getTime() > $this->to->getTime()) { - return false; - } - - return true; - } + public readonly ?Duration $duration; + + public function __construct( + public readonly ?DateTime $from, + public readonly ?DateTime $to + ) { + if (!$from && !$to) { + throw new NotCompliant('Date time intervals: from ' . $from . ' to' . $to); + } + + $this->calculateDuration(); + } + + public function isDuring(int|string|DateTime $dateTime): bool + { + if (is_string($dateTime) || is_int($dateTime)) { + $dateTime = new DateTime($dateTime); + } + + if ($this->from && $dateTime->time < $this->from->time) { + return false; + } + + if ($this->to && $dateTime->time > $this->to->time) { + return false; + } + + return true; + } + + private function calculateDuration(): void + { + $this->duration = ($this->from && $this->to) ? new Duration($this->to->time - $this->from->time) : null; + } + + public static function make( + null|int|string $from, + null|int|string $to + ): self { + if (is_string($from) || is_int($from)) { + $from = new DateTime($from); + } + + if (is_string($to) || is_int($to)) { + $to = new DateTime($to); + } + + return new static( + $from, + $to + ); + } } diff --git a/component/Time/Duration.php b/component/Time/Duration.php index 8ee5885..10af8b2 100644 --- a/component/Time/Duration.php +++ b/component/Time/Duration.php @@ -1,109 +1,99 @@ time = $time; - $this->label = $this->buildLabel(); - } - - public function __toString() - { - return (string) $this->getLabel(); - } - - public function get(): int - { - return $this->time; - } - - public function getLabel(): string - { - return $this->label; - } - - protected function buildLabel(): string - { - $remainingTime = $this->time; - - $labels = []; - - if ($remainingTime >= self::YEAR) { - $years = intval($remainingTime / self::YEAR); - if ($years) { - $labels[] = $years . ' ' . ($years > 1 ? self::YEAR_LABEL_PLURAL : self::YEAR_LABEL); - $remainingTime = $remainingTime % self::YEAR; - } - } - - if ($remainingTime >= self::MONTH) { - $months = intval($remainingTime / self::MONTH); - if ($months) { - $labels[] = $months . ' ' . ($months > 1 ? self::MONTH_LABEL_PLURAL : self::MONTH_LABEL); - $remainingTime = $remainingTime % self::MONTH; - } - } - - if ($remainingTime >= self::DAY) { - $days = intval($remainingTime / self::DAY); - if ($days) { - $labels[] = $days . ' ' . ($days > 1 ? self::DAY_LABEL_PLURAL : self::DAY_LABEL); - $remainingTime = $remainingTime % self::DAY; - } - } - - if ($remainingTime >= self::HOUR) { - $hours = intval($remainingTime / self::HOUR); - if ($hours) { - $labels[] = $hours . ' ' . ($hours > 1 ? self::HOUR_LABEL_PLURAL : self::HOUR_LABEL); - $remainingTime = $remainingTime % self::HOUR; - } - } - - if ($remainingTime >= self::MINUTE) { - $minutes = intval($remainingTime / self::MINUTE); - if ($minutes) { - $labels[] = $minutes . ' ' . ($minutes > 1 ? self::MINUTE_LABEL_PLURAL : self::MINUTE_LABEL); - $remainingTime = $remainingTime % self::MINUTE; - } - } - - if ($remainingTime > 0) { - $secondes = intval($remainingTime); - if ($secondes) { - $labels[] = $secondes . ' ' . ($secondes > 1 ? self::SECOND_LABEL_PLURAL : self::SECOND_LABEL); - } - } - - return implode(', ', $labels); - } + // Duration in secondes + public const MINUTE = 60; + public const HOUR = 3600; + public const DAY = 86400; + public const MONTH = 2628000; + public const YEAR = 31536000; + + public const SECOND_LABEL = 's'; + public const SECOND_LABEL_PLURAL = 's'; + public const MINUTE_LABEL = 'min'; + public const MINUTE_LABEL_PLURAL = 'min'; + public const HOUR_LABEL = 'h'; + public const HOUR_LABEL_PLURAL = 'h'; + public const DAY_LABEL = 'day'; + public const DAY_LABEL_PLURAL = 'days'; + public const MONTH_LABEL = 'month'; + public const MONTH_LABEL_PLURAL = 'months'; + public const YEAR_LABEL = 'year'; + public const YEAR_LABEL_PLURAL = 'years'; + + protected readonly string $label; + + public function __construct( + public readonly int $value + ) { + $this->label = $this->buildLabel(); + } + + public function __toString() + { + return $this->label; + } + + protected function buildLabel(): string + { + $remainingTime = $this->value; + + $labels = []; + + if ($remainingTime >= self::YEAR) { + $years = intval($remainingTime / self::YEAR); + if ($years) { + $labels[] = $years . ' ' . ($years > 1 ? self::YEAR_LABEL_PLURAL : self::YEAR_LABEL); + $remainingTime = $remainingTime % self::YEAR; + } + } + + if ($remainingTime >= self::MONTH) { + $months = intval($remainingTime / self::MONTH); + if ($months) { + $labels[] = $months . ' ' . ($months > 1 ? self::MONTH_LABEL_PLURAL : self::MONTH_LABEL); + $remainingTime = $remainingTime % self::MONTH; + } + } + + if ($remainingTime >= self::DAY) { + $days = intval($remainingTime / self::DAY); + if ($days) { + $labels[] = $days . ' ' . ($days > 1 ? self::DAY_LABEL_PLURAL : self::DAY_LABEL); + $remainingTime = $remainingTime % self::DAY; + } + } + + if ($remainingTime >= self::HOUR) { + $hours = intval($remainingTime / self::HOUR); + if ($hours) { + $labels[] = $hours . ' ' . ($hours > 1 ? self::HOUR_LABEL_PLURAL : self::HOUR_LABEL); + $remainingTime = $remainingTime % self::HOUR; + } + } + + if ($remainingTime >= self::MINUTE) { + $minutes = intval($remainingTime / self::MINUTE); + if ($minutes) { + $labels[] = $minutes . ' ' . ($minutes > 1 ? self::MINUTE_LABEL_PLURAL : self::MINUTE_LABEL); + $remainingTime = $remainingTime % self::MINUTE; + } + } + + if ($remainingTime > 0) { + $secondes = intval($remainingTime); + if ($secondes) { + $labels[] = $secondes . ' ' . ($secondes > 1 ? self::SECOND_LABEL_PLURAL : self::SECOND_LABEL); + } + } + + return implode(', ', $labels); + } } diff --git a/component/Token/Jwt.php b/component/Token/Jwt.php index c0f5e65..03ef133 100644 --- a/component/Token/Jwt.php +++ b/component/Token/Jwt.php @@ -1,4 +1,5 @@ value, new FirebaseKey($publicKey, self::ALGORITHM)); - } catch (ExpiredException | SignatureInvalidException $e) { - throw new NotCompliant; - } - } - - public static function encode(string $privateKey, array $payload, int $lifetime = 3600): self - { - $payload[ self::PAYLOAD_CREATION_TIME ] = time(); - $payload[ self::PAYLOAD_LIFE_TIME ] = $payload[ self::PAYLOAD_CREATION_TIME ] + $lifetime; - - return new static(FirebaseJwt::encode($payload, $privateKey, self::ALGORITHM)); - } + public const PAYLOAD_CREATION_TIME = 'iat'; + public const PAYLOAD_LIFE_TIME = 'exp'; + + public const ALGORITHM = 'RS256'; + + final public function __construct(string $value) + { + parent::__construct($value); + } + + public function decode(string $publicKey): array + { + try { + return (array) FirebaseJwt::decode($this->value, new FirebaseKey($publicKey, self::ALGORITHM)); + } catch (ExpiredException | SignatureInvalidException $e) { + throw new NotCompliant(); + } + } + + public static function encode(string $privateKey, array $payload, int $lifetime = 3600): self + { + $payload[ self::PAYLOAD_CREATION_TIME ] = time(); + $payload[ self::PAYLOAD_LIFE_TIME ] = $payload[ self::PAYLOAD_CREATION_TIME ] + $lifetime; + + return new static(FirebaseJwt::encode($payload, $privateKey, self::ALGORITHM)); + } } diff --git a/component/Web/DomainName.php b/component/Web/DomainName.php index 9e0fd15..d1549c9 100644 --- a/component/Web/DomainName.php +++ b/component/Web/DomainName.php @@ -1,36 +1,37 @@ value)[0] ?? null; - } - - public function getPretty(): self - { - return new self( - preg_replace('#^www\d?\.(.+\.)#i', '$1', $this->value) - ); - } - - public function getExtension(): ?string - { - $parts = explode('.', $this->value); - - return $parts ? end($parts) : null; - } + public const PATTERN = '/^([a-zA-Z0-9][a-zA-Z0-9-\.]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}|localhost)$/'; + + public function __construct(string $domainName) + { + $domainName = preg_replace('/ /', '', $domainName); + $domainName = strtolower($domainName); + + parent::__construct($domainName); + } + + public function getName(): ?string + { + return preg_split('/(?=\.[^.]+$)/', $this->value)[0] ?? null; + } + + public function getPretty(): self + { + return new self( + preg_replace('#^www\d?\.(.+\.)#i', '$1', $this->value) + ); + } + + public function getExtension(): ?string + { + $parts = explode('.', $this->value); + + return $parts ? end($parts) : null; + } } diff --git a/component/Web/Email.php b/component/Web/Email.php index 78fca22..2841b2f 100644 --- a/component/Web/Email.php +++ b/component/Web/Email.php @@ -1,49 +1,50 @@ subject = $subject; - $this->messageTxt = $messageTxt; - $this->messageHtml = $messageHtml; - $this->from = $from; - $this->to = $to; - $this->replyTo = $replyTo; - } + final public function __construct( + public string $subject, + public string $messageTxt, + public string $messageHtml, + public EmailAddressAndName $from, + public EmailAddressAndName $to, + public ?EmailAddressAndName $replyTo + ) { + } + + public static function make( + string $subject, + string $messageTxt, + string $messageHtml, + string $fromEmailAddress, + string $fromName, + string $toEmailAddress, + string $toName, + ?string $replyToEmailAddress = null, + ?string $replyToName = null + ): self { + return new static( + $subject, + $messageTxt, + $messageHtml, + EmailAddressAndName::make( + $fromEmailAddress, + $fromName + ), + EmailAddressAndName::make( + $toEmailAddress, + $toName + ), + $replyToEmailAddress ? EmailAddressAndName::make( + $replyToEmailAddress, + $replyToName + ) : null + ); + } } diff --git a/component/Web/EmailAddress.php b/component/Web/EmailAddress.php index e6be69b..7491ad2 100644 --- a/component/Web/EmailAddress.php +++ b/component/Web/EmailAddress.php @@ -1,41 +1,45 @@ ()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/'; - - public function __construct(string $emailAddress) - { - $emailAddress = preg_replace('/ /', '', $emailAddress); - $emailAddress = strtolower($emailAddress); - - parent::__construct($emailAddress); - } - - public function getUserName(): UserName - { - return new UserName( - strstr($this->value, '@', true) - ); - } - - public function getDomainName(): DomainName - { - return new DomainName( - substr(strrchr($this->value, '@'), 1) - ); - } - - public static function build(UserName $userName, DomainName $domainName): self - { - return new self($userName . '@' . $domainName); - } + public const PATTERN = '/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/'; + + public function __construct(string $emailAddress) + { + $emailAddress = preg_replace('/ /', '', $emailAddress); + $emailAddress = strtolower($emailAddress); + + parent::__construct($emailAddress); + } + + public function getUserName(): UserName + { + return new UserName( + strstr($this->value, '@', true) + ); + } + + public function getDomainName(): DomainName + { + return new DomainName( + substr(strrchr($this->value, '@'), 1) + ); + } + + public static function make( + UserName $userName, + DomainName $domainName + ): self + { + return new static($userName . '@' . $domainName); + } } diff --git a/component/Web/EmailAddressAndName.php b/component/Web/EmailAddressAndName.php index e1cd0d5..682fe29 100644 --- a/component/Web/EmailAddressAndName.php +++ b/component/Web/EmailAddressAndName.php @@ -1,35 +1,26 @@ emailAddress = $emailAddress; - $this->name = !is_null($name) ? trim($name) : null; - } - - public function getEmailAddress(): EmailAddress - { - return $this->emailAddress; - } - - public function getName(): ?string - { - return $this->name; - } + public function __construct( + public readonly EmailAddress $emailAddress, + public readonly ?string $name + ) { + } + + public static function make( + string $emailAddress, + ?string $name = null + ): self { + return new static( + new EmailAddress($emailAddress), + !is_null($name) ? trim($name) : null + ); + } } diff --git a/component/Web/Url.php b/component/Web/Url.php index 62dfdff..f54c02a 100644 --- a/component/Web/Url.php +++ b/component/Web/Url.php @@ -1,4 +1,5 @@ ]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s'; - - protected ?string $scheme; - protected ?string $user; - protected ?string $pass; - protected ?string $host; - protected ?int $port; - protected ?string $path; - protected ?array $query; - protected ?string $fragment; - - public function __construct($url) - { - parent::__construct($url); - - $this->decompose(); - } - - public function getScheme(): ?string - { - return $this->scheme; - } - - public function setScheme(?string $scheme): self - { - $url = clone $this; - - $scheme = trim($scheme); - - $url->scheme = $scheme; - - return self::compose($url); - } - - public function getUser(): ?string - { - return $this->user; - } - - public function setUser(?string $user): self - { - $url = clone $this; - - $user = trim($user); - - $url->user = $user; - - return self::compose($url); - } - - public function getPass(): ?string - { - return $this->pass; - } - - public function setPass(?string $pass): self - { - $url = clone $this; - - $pass = trim($pass); - - $url->pass = $pass; - - return self::compose($url); - } - - public function getHost(): ?string - { - return $this->host; - } - - public function setHost(?string $host): self - { - $url = clone $this; - - $host = trim($host); - - $url->host = $host; - - return self::compose($url); - } - - public function getPort(): ?int - { - return $this->port; - } - - public function setPort(?int $port): self - { - $url = clone $this; - - $url->port = $port; - - return self::compose($url); - } - - public function getPath(): ?string - { - return $this->path; - } - - public function setPath(?string $path): self - { - $url = clone $this; - - $path = trim($path); - - if (substr($path, 0, 1) != '/') { - $path = '/' . $path; - } - - $url->path = $path; - - return self::compose($url); - } - - public function getQuery(): ?array - { - return $this->query; - } - - public function addQueryParameter(string $key, string $value): self - { - $url = clone $this; - - $key = trim($key); - - if (!$url->query) $url->query = []; - - $url->query[ $key ] = $value; - - return self::compose($url); - } - - public function removeQueryParameter(string $key): self - { - $url = clone $this; - - $key = trim($key); - - unset($url->query[ $key ]); - - if (!$this->query) $url->query = null; - - return self::compose($url); - } - - public function getFragment(): ?string - { - return $this->fragment; - } - - public function setFragment(?string $fragment): self - { - $url = clone $this; - - $fragment = trim($fragment); - - $url->fragment = $fragment; - - return self::compose($url); - } - - protected function decompose(): void - { - $urlParts = parse_url($this->value); - - $this->scheme = $urlParts[ 'scheme' ] ?? null; - $this->user = $urlParts[ 'user' ] ?? null; - $this->pass = $urlParts[ 'pass' ] ?? null; - $this->host = $urlParts[ 'host' ] ?? null; - $this->port = $urlParts[ 'port' ] ?? null; - $this->path = $urlParts[ 'path' ] ?? null; - if (isset($urlParts[ 'query' ]) && is_string($urlParts[ 'query' ])) { - parse_str($urlParts[ 'query' ], $this->query); - } else { - $this->query = null; - } - $this->fragment = $urlParts[ 'fragment' ] ?? null; - } - - public static function compose(self $url): self - { - $urlString = ''; - - $urlString.= $url->getScheme(); - - $urlString.= '://'; - - if ($url->getUser()) { - $urlString.= $url->getUser(); - if ($url->getPass()) { - $urlString.= ':'; - $urlString.= $url->getPass(); - } - $urlString.= '@'; - } - - $urlString.= $url->getHost(); - - if ($url->getPort()) { - $urlString.= ':' . $url->getPort(); - } - - if ($url->getPath()) { - $urlString.= $url->getPath(); - } - - if ($url->getQuery()) { - $urlString.= '?' . http_build_query($url->getQuery()); - } - - if ($url->getFragment()) { - $urlString.= '#' . $url->getFragment(); - } - - return new self($urlString); - } + public const PATTERN = '%\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s'; + + protected ?string $scheme; + protected ?string $user; + protected ?string $pass; + protected ?string $host; + protected ?int $port; + protected ?string $path; + protected ?array $query; + protected ?string $fragment; + + public function __construct($url) + { + parent::__construct($url); + + $this->decompose(); + } + + public function getScheme(): ?string + { + return $this->scheme; + } + + public function setScheme(?string $scheme): self + { + $url = clone $this; + + $scheme = trim($scheme); + + $url->scheme = $scheme; + + return self::compose($url); + } + + public function getUser(): ?string + { + return $this->user; + } + + public function setUser(?string $user): self + { + $url = clone $this; + + $user = trim($user); + + $url->user = $user; + + return self::compose($url); + } + + public function getPass(): ?string + { + return $this->pass; + } + + public function setPass(?string $pass): self + { + $url = clone $this; + + $pass = trim($pass); + + $url->pass = $pass; + + return self::compose($url); + } + + public function getHost(): ?string + { + return $this->host; + } + + public function setHost(?string $host): self + { + $url = clone $this; + + $host = trim($host); + + $url->host = $host; + + return self::compose($url); + } + + public function getPort(): ?int + { + return $this->port; + } + + public function setPort(?int $port): self + { + $url = clone $this; + + $url->port = $port; + + return self::compose($url); + } + + public function getPath(): ?string + { + return $this->path; + } + + public function setPath(?string $path): self + { + $url = clone $this; + + $path = trim($path); + + if (substr($path, 0, 1) != '/') { + $path = '/' . $path; + } + + $url->path = $path; + + return self::compose($url); + } + + public function getQuery(): ?array + { + return $this->query; + } + + public function addQueryParameter(string $key, string $value): self + { + $url = clone $this; + + $key = trim($key); + + if (!$url->query) { + $url->query = []; + } + + $url->query[ $key ] = $value; + + return self::compose($url); + } + + public function removeQueryParameter(string $key): self + { + $url = clone $this; + + $key = trim($key); + + unset($url->query[ $key ]); + + if (!$this->query) { + $url->query = null; + } + + return self::compose($url); + } + + public function getFragment(): ?string + { + return $this->fragment; + } + + public function setFragment(?string $fragment): self + { + $url = clone $this; + + $fragment = trim($fragment); + + $url->fragment = $fragment; + + return self::compose($url); + } + + protected function decompose(): void + { + $urlParts = parse_url($this->value); + + $this->scheme = $urlParts[ 'scheme' ] ?? null; + $this->user = $urlParts[ 'user' ] ?? null; + $this->pass = $urlParts[ 'pass' ] ?? null; + $this->host = $urlParts[ 'host' ] ?? null; + $this->port = $urlParts[ 'port' ] ?? null; + $this->path = $urlParts[ 'path' ] ?? null; + if (isset($urlParts[ 'query' ]) && is_string($urlParts[ 'query' ])) { + parse_str($urlParts[ 'query' ], $this->query); + } else { + $this->query = null; + } + $this->fragment = $urlParts[ 'fragment' ] ?? null; + } + + public static function compose(self $url): self + { + $urlString = ''; + + $urlString.= $url->getScheme(); + + $urlString.= '://'; + + if ($url->getUser()) { + $urlString.= $url->getUser(); + if ($url->getPass()) { + $urlString.= ':'; + $urlString.= $url->getPass(); + } + $urlString.= '@'; + } + + $urlString.= $url->getHost(); + + if ($url->getPort()) { + $urlString.= ':' . $url->getPort(); + } + + if ($url->getPath()) { + $urlString.= $url->getPath(); + } + + if ($url->getQuery()) { + $urlString.= '?' . http_build_query($url->getQuery()); + } + + if ($url->getFragment()) { + $urlString.= '#' . $url->getFragment(); + } + + return new self($urlString); + } } diff --git a/component/Web/UserName.php b/component/Web/UserName.php index 5b929a6..f07001e 100644 --- a/component/Web/UserName.php +++ b/component/Web/UserName.php @@ -1,4 +1,5 @@ ()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))$/i'; - - public function __construct( - string $userName - ) - { - $userName = preg_replace('/ /', '', $userName); - $userName = strtolower($userName); - - parent::__construct($userName); - } + public const PATTERN = '/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))$/i'; + + public function __construct( + string $userName + ) { + $userName = preg_replace('/ /', '', $userName); + $userName = strtolower($userName); + + parent::__construct($userName); + } } diff --git a/test/Abstract/CollectionPaginatedTest.php b/test/Abstract/CollectionPaginatedTest.php index c82352c..3b8f34d 100644 --- a/test/Abstract/CollectionPaginatedTest.php +++ b/test/Abstract/CollectionPaginatedTest.php @@ -1,78 +1,77 @@ assertIsBool($collection->isEmpty()); - $this->assertEquals(true, $collection->isEmpty()); - - $this->assertIsInt($collection->getNbItems()); - $this->assertEquals(0, $collection->getNbItems()); - - $collection->addValue( - new Value('Foo') - ); - - $this->assertEquals(false, $collection->isEmpty()); - $this->assertEquals(1, $collection->getNbItems()); - - $collection->addValue( - new Value('Bar') - ); - - $this->assertEquals(false, $collection->isEmpty()); - $this->assertEquals(2, $collection->getNbItems()); - - $this->assertIsInt($collection->getItemByPage()); - $this->assertEquals(2, $collection->getItemByPage()); - - $this->assertIsInt($collection->getItemTotal()); - $this->assertEquals(3, $collection->getItemTotal()); - - $this->assertIsInt($collection->getItemPage()); - $this->assertEquals(2, $collection->getItemPage()); - - $this->assertIsInt($collection->getPageCurrent()); - $this->assertEquals(1, $collection->getPageCurrent()); - - $this->assertIsInt($collection->getPageTotal()); - $this->assertEquals(2, $collection->getPageTotal()); - - $this->assertIsObject($collection->getByKey(0)); - - foreach ($collection->itemsIterator() as $item) { - $this->assertIsObject($item); - } - - $collection->removeValue( - new Value('Foo') - ); - - $this->assertEquals(false, $collection->isEmpty()); - $this->assertEquals(1, $collection->getNbItems()); - - $collection->addValue( - new Value('Foo') - ); - - $this->assertEquals(false, $collection->isEmpty()); - $this->assertEquals(2, $collection->getNbItems()); - } + public function testInterface(): void + { + $collection = new CollectionPaginated( + 1, + 2, + 3 + ); + + $this->assertIsBool($collection->isEmpty()); + $this->assertEquals(true, $collection->isEmpty()); + + $this->assertIsInt($collection->getNbItems()); + $this->assertEquals(0, $collection->getNbItems()); + + $collection->addValue( + new Value('Foo') + ); + + $this->assertEquals(false, $collection->isEmpty()); + $this->assertEquals(1, $collection->getNbItems()); + + $collection->addValue( + new Value('Bar') + ); + + $this->assertEquals(false, $collection->isEmpty()); + $this->assertEquals(2, $collection->getNbItems()); + + $this->assertIsInt($collection->getItemByPage()); + $this->assertEquals(2, $collection->getItemByPage()); + + $this->assertIsInt($collection->getItemTotal()); + $this->assertEquals(3, $collection->getItemTotal()); + + $this->assertIsInt($collection->getItemPage()); + $this->assertEquals(2, $collection->getItemPage()); + + $this->assertIsInt($collection->getPageCurrent()); + $this->assertEquals(1, $collection->getPageCurrent()); + + $this->assertIsInt($collection->getPageTotal()); + $this->assertEquals(2, $collection->getPageTotal()); + + $this->assertIsObject($collection->getByKey(0)); + + foreach ($collection->itemsIterator() as $item) { + $this->assertIsObject($item); + } + + $collection->removeValue( + new Value('Foo') + ); + + $this->assertEquals(false, $collection->isEmpty()); + $this->assertEquals(1, $collection->getNbItems()); + + $collection->addValue( + new Value('Foo') + ); + + $this->assertEquals(false, $collection->isEmpty()); + $this->assertEquals(2, $collection->getNbItems()); + } } diff --git a/test/Abstract/CollectionTest.php b/test/Abstract/CollectionTest.php index 3abd701..79be69e 100644 --- a/test/Abstract/CollectionTest.php +++ b/test/Abstract/CollectionTest.php @@ -1,47 +1,46 @@ assertIsBool($collection->isEmpty()); - $this->assertEquals(true, $collection->isEmpty()); - - $this->assertIsInt($collection->getNbItems()); - $this->assertEquals(0, $collection->getNbItems()); - - $collection->addValue( - new Value('Foo') - ); - - $this->assertEquals(false, $collection->isEmpty()); - $this->assertEquals(1, $collection->getNbItems()); - - $this->assertIsObject($collection->getByKey(0)); - - foreach ($collection->itemsIterator() as $item) { - $this->assertIsObject($item); - } - - $collection->removeValue( - new Value('Foo') - ); - - $this->assertEquals(true, $collection->isEmpty()); - $this->assertEquals(0, $collection->getNbItems()); - - $collection->addValue( - new Value('Bar') - ); - } + public function testInterface(): void + { + $collection = new Collection(); + + $this->assertIsBool($collection->isEmpty()); + $this->assertEquals(true, $collection->isEmpty()); + + $this->assertIsInt($collection->getNbItems()); + $this->assertEquals(0, $collection->getNbItems()); + + $collection->addValue( + new Value('Foo') + ); + + $this->assertEquals(false, $collection->isEmpty()); + $this->assertEquals(1, $collection->getNbItems()); + + $this->assertIsObject($collection->getByKey(0)); + + foreach ($collection->itemsIterator() as $item) { + $this->assertIsObject($item); + } + + $collection->removeValue( + new Value('Foo') + ); + + $this->assertEquals(true, $collection->isEmpty()); + $this->assertEquals(0, $collection->getNbItems()); + + $collection->addValue( + new Value('Bar') + ); + } } diff --git a/test/Abstract/Fixture/Collection.php b/test/Abstract/Fixture/Collection.php index e8b9c55..7ae39bb 100644 --- a/test/Abstract/Fixture/Collection.php +++ b/test/Abstract/Fixture/Collection.php @@ -1,19 +1,20 @@ assertEquals('1', (string)$boolean->get()); - - $this->assertIsBool($boolean->get()); - $this->assertEquals(true, $boolean->get()); - } + public function testInterface(): void + { + $boolean = new Boolean(true); + + $this->assertIsBool($boolean->value); + $this->assertEquals(true, $boolean->value); + } } diff --git a/test/Abstract/Value/DecimalTest.php b/test/Abstract/Value/DecimalTest.php index e1603af..4f85dc7 100644 --- a/test/Abstract/Value/DecimalTest.php +++ b/test/Abstract/Value/DecimalTest.php @@ -1,4 +1,5 @@ assertEquals('1234.5678', (string)$decimal->get()); - - $this->assertIsFloat($decimal->get()); - $this->assertEquals(1234.5678, $decimal->get()); - } + public function testInterface(): void + { + $decimal = new Decimal(1234.5678); + + $this->assertIsFloat($decimal->value); + $this->assertEquals(1234.5678, $decimal->value); + } } diff --git a/test/Abstract/Value/IntegerTest.php b/test/Abstract/Value/IntegerTest.php index d805e1e..2c45383 100644 --- a/test/Abstract/Value/IntegerTest.php +++ b/test/Abstract/Value/IntegerTest.php @@ -1,4 +1,5 @@ assertEquals('1234', (string)$integer->get()); - - $this->assertIsInt($integer->get()); - $this->assertEquals(1234, $integer->get()); - } + public function testInterface(): void + { + $integer = new Integer(1234); + + $this->assertIsInt($integer->value); + $this->assertEquals(1234, $integer->value); + } } diff --git a/test/Abstract/Value/VarcharTest.php b/test/Abstract/Value/VarcharTest.php index a863483..94a37a3 100644 --- a/test/Abstract/Value/VarcharTest.php +++ b/test/Abstract/Value/VarcharTest.php @@ -1,4 +1,5 @@ assertEquals('Hello world!', (string)$varchar->get()); - - $this->assertIsString($varchar->get()); - $this->assertEquals('Hello world!', $varchar->get()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Varchar(''); - } +{ + public function testInterface(): void + { + $varchar = new Varchar('Hello world!'); + + $this->assertEquals('Hello world!', (string)$varchar); + + $this->assertIsString($varchar->value); + $this->assertEquals('Hello world!', $varchar->value); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new Varchar(''); + } } diff --git a/test/Company/Fr/CodeActiviteTest.php b/test/Company/Fr/CodeActiviteTest.php index 0384c60..9f33d1c 100644 --- a/test/Company/Fr/CodeActiviteTest.php +++ b/test/Company/Fr/CodeActiviteTest.php @@ -1,4 +1,5 @@ assertEquals('62.01Z', (string)$codeActivite); - - $this->assertIsString($codeActivite->get()); - $this->assertEquals('62.01Z', $codeActivite->get()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new CodeActivite('A0A0A'); - } + public function testInterface(): void + { + $codeActivite = new CodeActivite('62.01Z'); + + $this->assertEquals('62.01Z', (string)$codeActivite); + + $this->assertIsString($codeActivite->value); + $this->assertEquals('62.01Z', $codeActivite->value); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new CodeActivite('A0A0A'); + } } diff --git a/test/Company/Fr/SirenTest.php b/test/Company/Fr/SirenTest.php index cd52a2b..63bf100 100644 --- a/test/Company/Fr/SirenTest.php +++ b/test/Company/Fr/SirenTest.php @@ -1,4 +1,5 @@ assertEquals('512747395', (string)$siren); - - $this->assertIsString($siren->get()); - $this->assertEquals('512747395', $siren->get()); - - $this->assertIsString($siren->getFormatted()); - $this->assertEquals('512 747 395', $siren->getFormatted()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Siren('123456789'); - } + public function testInterface(): void + { + $siren = new Siren('512747395'); + + $this->assertEquals('512747395', (string)$siren); + + $this->assertIsString($siren->value); + $this->assertEquals('512747395', $siren->value); + + $this->assertIsString($siren->getFormatted()); + $this->assertEquals('512 747 395', $siren->getFormatted()); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new Siren('123456789'); + } } diff --git a/test/Company/Fr/SiretTest.php b/test/Company/Fr/SiretTest.php index 0ef7166..163e7f7 100644 --- a/test/Company/Fr/SiretTest.php +++ b/test/Company/Fr/SiretTest.php @@ -1,4 +1,5 @@ assertEquals('51274739500022', (string)$siret); - - $this->assertIsString($siret->get()); - $this->assertEquals('51274739500022', $siret->get()); - - $this->assertIsObject($siret->getSiren()); - $this->assertEquals('512747395', (string)$siret->getSiren()); - - $this->assertIsString($siret->getFormatted()); - $this->assertEquals('512 747 395 00022', $siret->getFormatted()); - } - - public function testCheckLaPoste(): void - { - $siret = new Siret('35600000000074'); - $this->assertEquals('35600000000074', (string)$siret); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Siret('12345678901234'); - } + public function testInterface(): void + { + $siret = new Siret('51274739500022'); + + $this->assertEquals('51274739500022', (string)$siret); + + $this->assertIsString($siret->value); + $this->assertEquals('51274739500022', $siret->value); + + $this->assertIsObject($siret->getSiren()); + $this->assertEquals('512747395', (string)$siret->getSiren()); + + $this->assertIsString($siret->getFormatted()); + $this->assertEquals('512 747 395 00022', $siret->getFormatted()); + } + + public function testCheckLaPoste(): void + { + $siret = new Siret('35600000000074'); + $this->assertEquals('35600000000074', (string)$siret); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new Siret('12345678901234'); + } } diff --git a/test/Company/NameTest.php b/test/Company/NameTest.php index 927e3fb..387d2d2 100644 --- a/test/Company/NameTest.php +++ b/test/Company/NameTest.php @@ -1,4 +1,5 @@ assertEquals('Acme', (string)$name); - - $this->assertIsString($name->get()); - $this->assertEquals('Acme', $name->get()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Name(''); - } + public function testInterface(): void + { + $name = new Name('Acme'); + + $this->assertEquals('Acme', (string)$name); + + $this->assertIsString($name->value); + $this->assertEquals('Acme', $name->value); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new Name(''); + } } diff --git a/test/Geography/Fr/CodeCommuneTest.php b/test/Geography/Fr/CodeCommuneTest.php index e2f3224..75a70fa 100644 --- a/test/Geography/Fr/CodeCommuneTest.php +++ b/test/Geography/Fr/CodeCommuneTest.php @@ -1,4 +1,5 @@ assertEquals('97421', (string)$codeCommune); - - $this->assertIsString($codeCommune->get()); - $this->assertEquals('97421', $codeCommune->get()); - - $this->assertIsObject($codeCommune->getNumeroDepartement()); - $this->assertEquals('974', (string)$codeCommune->getNumeroDepartement()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new CodeCommune('690I'); - } + public function testInterface(): void + { + $codeCommune = new CodeCommune('97421'); + + $this->assertEquals('97421', (string)$codeCommune); + + $this->assertIsString($codeCommune->value); + $this->assertEquals('97421', $codeCommune->value); + + $this->assertIsObject($codeCommune->getNumeroDepartement()); + $this->assertEquals('974', (string)$codeCommune->getNumeroDepartement()); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new CodeCommune('690I'); + } } diff --git a/test/Geography/Fr/CodePostalTest.php b/test/Geography/Fr/CodePostalTest.php index b44f766..f29dff9 100644 --- a/test/Geography/Fr/CodePostalTest.php +++ b/test/Geography/Fr/CodePostalTest.php @@ -1,4 +1,5 @@ assertEquals('69001', (string)$codePostal); - - $this->assertIsString($codePostal->get()); - $this->assertEquals('69001', $codePostal->get()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new CodePostal('690I'); - } + public function testInterface(): void + { + $codePostal = new CodePostal('69001'); + + $this->assertEquals('69001', (string)$codePostal); + + $this->assertIsString($codePostal->value); + $this->assertEquals('69001', $codePostal->value); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new CodePostal('690I'); + } } diff --git a/test/Geography/Fr/NumeroDepartementTest.php b/test/Geography/Fr/NumeroDepartementTest.php index f24d6c2..e65f3d0 100644 --- a/test/Geography/Fr/NumeroDepartementTest.php +++ b/test/Geography/Fr/NumeroDepartementTest.php @@ -1,4 +1,5 @@ assertEquals('01', (string)$numeroDepartement); - - $this->assertIsString($numeroDepartement->get()); - $this->assertEquals('01', $numeroDepartement->get()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new NumeroDepartement('2C'); - } + public function testInterface(): void + { + $numeroDepartement = new NumeroDepartement('1'); + + $this->assertEquals('01', (string)$numeroDepartement); + + $this->assertIsString($numeroDepartement->value); + $this->assertEquals('01', $numeroDepartement->value); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new NumeroDepartement('2C'); + } } diff --git a/test/Geography/GpsCoordinatesTest.php b/test/Geography/GpsCoordinatesTest.php index 1b81edd..a39be41 100644 --- a/test/Geography/GpsCoordinatesTest.php +++ b/test/Geography/GpsCoordinatesTest.php @@ -1,4 +1,5 @@ assertEquals('53.3284723;-2.2117116', (string)$gpsCoordinates); - - $this->assertIsFloat($gpsCoordinates->getLatitude()); - $this->assertEquals(53.3284723, $gpsCoordinates->getLatitude()); - - $this->assertIsFloat($gpsCoordinates->getLongitude()); - $this->assertEquals(-2.2117116, $gpsCoordinates->getLongitude()); - } - - public function testCreateFromLambert93(): void - { - $gpsCoordinates = GpsCoordinates::createFromLambert93(350772.53, 7372163.87); - - $this->assertEquals(53.3284723, $gpsCoordinates->getLatitude()); - $this->assertEquals(-2.2117116, $gpsCoordinates->getLongitude()); - } - - public function testCreateFromUtm(): void - { - $gpsCoordinates = GpsCoordinates::createFromUtm(153037.74, 5921483.19, 31); - - $this->assertEquals(53.3284723, $gpsCoordinates->getLatitude()); - $this->assertEquals(-2.2117116, $gpsCoordinates->getLongitude()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new GpsCoordinates(180, 360); - } + public function testInterface(): void + { + $gpsCoordinates = new GpsCoordinates(53.3284723, -2.2117116); + + $this->assertEquals('53.3284723;-2.2117116', (string)$gpsCoordinates); + + $this->assertIsFloat($gpsCoordinates->latitude); + $this->assertEquals(53.3284723, $gpsCoordinates->latitude); + + $this->assertIsFloat($gpsCoordinates->longitude); + $this->assertEquals(-2.2117116, $gpsCoordinates->longitude); + } + + public function testCreateFromLambert93(): void + { + $gpsCoordinates = GpsCoordinates::makeFromLambert93(350772.53, 7372163.87); + + $this->assertEquals(53.3284723, $gpsCoordinates->latitude); + $this->assertEquals(-2.2117116, $gpsCoordinates->longitude); + } + + public function testCreateFromUtm(): void + { + $gpsCoordinates = GpsCoordinates::makeFromUtm(153037.74, 5921483.19, 31); + + $this->assertEquals(53.3284723, $gpsCoordinates->latitude); + $this->assertEquals(-2.2117116, $gpsCoordinates->longitude); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new GpsCoordinates(180, 360); + } } diff --git a/test/Id/UuidTest.php b/test/Id/UuidTest.php index 246695f..14a6580 100644 --- a/test/Id/UuidTest.php +++ b/test/Id/UuidTest.php @@ -1,4 +1,5 @@ assertEquals('1a55e96e-7a47-465b-887b-2f517068065c', (string)$uuid); - - $this->assertIsString($uuid->get()); - $this->assertEquals('1a55e96e-7a47-465b-887b-2f517068065c', $uuid->get()); - } - - public function testGenerate(): void - { - $uuid = Uuid::generate(); - - $this->assertIsString($uuid->get()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Uuid('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); - } + public function testInterface(): void + { + $uuid = new Uuid('1a55e96e-7a47-465b-887b-2f517068065c'); + + $this->assertEquals('1a55e96e-7a47-465b-887b-2f517068065c', (string)$uuid); + + $this->assertIsString($uuid->value); + $this->assertEquals('1a55e96e-7a47-465b-887b-2f517068065c', $uuid->value); + } + + public function testGenerate(): void + { + $uuid = Uuid::generate(); + + $this->assertIsString($uuid->value); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new Uuid('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); + } } diff --git a/test/Money/CurrencyTest.php b/test/Money/CurrencyTest.php index 5fed4a1..1c5d421 100644 --- a/test/Money/CurrencyTest.php +++ b/test/Money/CurrencyTest.php @@ -1,31 +1,21 @@ assertEquals('€', (string)$currency); - - $this->assertIsString($currency->getValue()); - $this->assertEquals('EUR', $currency->getValue()); - - $this->assertIsString($currency->getLabel()); - $this->assertEquals('€', $currency->getLabel()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Currency('Franc'); - } + public function testInterface(): void + { + $currency = Currency::EUR; + + $this->assertIsString($currency->value); + $this->assertEquals('EUR', $currency->value); + + $this->assertIsString($currency->getLabel()); + $this->assertEquals('€', $currency->getLabel()); + } } diff --git a/test/Money/PriceTest.php b/test/Money/PriceTest.php index 035d9ea..698dedf 100644 --- a/test/Money/PriceTest.php +++ b/test/Money/PriceTest.php @@ -1,30 +1,31 @@ assertEquals('1 234,56 €/kg', (string)$price); - - $this->assertIsFloat($price->get()); - $this->assertEquals(1234.56, $price->get()); - - $this->assertIsObject($price->getCurrency()); - $this->assertEquals('€', (string)$price->getCurrency()); - - $this->assertIsString($price->getUnit()); - $this->assertEquals('kg', (string)$price->getUnit()); - } + public function testInterface(): void + { + $price = new Price(1234.56, Currency::EUR, 'kg'); + + $this->assertEquals('1 234,56 €/kg', (string)$price); + + $this->assertIsFloat($price->amount); + $this->assertEquals(1234.56, $price->amount); + + $this->assertIsObject($price->currency); + $this->assertEquals(Currency::EUR, $price->currency); + + $this->assertIsString($price->unit); + $this->assertEquals('kg', (string)$price->unit); + } } diff --git a/test/Number/GradeTest.php b/test/Number/GradeTest.php new file mode 100644 index 0000000..43ed1f3 --- /dev/null +++ b/test/Number/GradeTest.php @@ -0,0 +1,39 @@ +assertEquals('8/10', (string)$grade); + + $this->assertIsInt($grade->position); + $this->assertEquals(8, $grade->position); + + $this->assertIsInt($grade->scale); + $this->assertEquals(10, $grade->scale); + } + + public function testNotCompliantGrade(): void + { + $this->expectException(NotCompliant::class); + + new Grade(-2, 10); + } + + public function testNotCompliantUnit(): void + { + $this->expectException(NotCompliant::class); + + new Grade(2, -3); + } +} diff --git a/test/Number/NoteTest.php b/test/Number/NoteTest.php deleted file mode 100644 index 667f8b0..0000000 --- a/test/Number/NoteTest.php +++ /dev/null @@ -1,38 +0,0 @@ -assertEquals('8/10', (string)$note); - - $this->assertIsInt($note->getNote()); - $this->assertEquals(8, $note->getNote()); - - $this->assertIsInt($note->getUnit()); - $this->assertEquals(10, $note->getUnit()); - } - - public function testNotCompliantNote(): void - { - $this->expectException(NotCompliant::class); - - new Note(-2, 10); - } - - public function testNotCompliantUnit(): void - { - $this->expectException(NotCompliant::class); - - new Note(2, -3); - } -} diff --git a/test/Number/RateTest.php b/test/Number/RateTest.php index 08156be..2e437db 100644 --- a/test/Number/RateTest.php +++ b/test/Number/RateTest.php @@ -1,4 +1,5 @@ assertEquals('1234.4567 %', (string)$rate); - - $this->assertIsFloat($rate->get()); - $this->assertEquals(1234.4567, $rate->get()); - } + public function testInterface(): void + { + $rate = new Rate(1234.4567); + + $this->assertEquals('1234.4567 %', (string)$rate); + + $this->assertIsFloat($rate->value); + $this->assertEquals(1234.4567, $rate->value); + } } diff --git a/test/Person/FirstnameTest.php b/test/Person/FirstnameTest.php index 31173df..2ea9584 100644 --- a/test/Person/FirstnameTest.php +++ b/test/Person/FirstnameTest.php @@ -1,4 +1,5 @@ assertEquals('John', (string)$firstname); - - $this->assertIsString($firstname->get()); - $this->assertEquals('John', $firstname->get()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Firstname(''); - } + public function testInterface(): void + { + $firstname = new Firstname(' john '); + + $this->assertEquals('John', (string)$firstname); + + $this->assertIsString($firstname->value); + $this->assertEquals('John', $firstname->value); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new Firstname(''); + } } diff --git a/test/Person/GenderTest.php b/test/Person/GenderTest.php index 0863ccf..15724f6 100644 --- a/test/Person/GenderTest.php +++ b/test/Person/GenderTest.php @@ -1,31 +1,21 @@ assertEquals('Female', (string)$gender); - - $this->assertIsString($gender->getValue()); - $this->assertEquals('female', $gender->getValue()); - - $this->assertIsString($gender->getLabel()); - $this->assertEquals('Female', $gender->getLabel()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Gender('alien'); - } + public function testInterface(): void + { + $gender = Gender::Female; + + $this->assertIsString($gender->value); + $this->assertEquals('female', $gender->value); + + $this->assertIsString($gender->getLabel()); + $this->assertEquals('Female', $gender->getLabel()); + } } diff --git a/test/Person/LastnameTest.php b/test/Person/LastnameTest.php index 5fe70c9..fc868ed 100644 --- a/test/Person/LastnameTest.php +++ b/test/Person/LastnameTest.php @@ -1,4 +1,5 @@ assertEquals('DOE', (string)$lastname); - - $this->assertIsString($lastname->get()); - $this->assertEquals('DOE', $lastname->get()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Lastname(''); - } + public function testInterface(): void + { + $lastname = new Lastname(' doe '); + + $this->assertEquals('DOE', (string)$lastname); + + $this->assertIsString($lastname->value); + $this->assertEquals('DOE', $lastname->value); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new Lastname(''); + } } diff --git a/test/Time/DateIntervalTest.php b/test/Time/DateIntervalTest.php index 26e1518..00c76b7 100644 --- a/test/Time/DateIntervalTest.php +++ b/test/Time/DateIntervalTest.php @@ -1,4 +1,5 @@ assertIsObject($dateInterval->getFrom()); - $this->assertEquals('1954-06-07', (string)$dateInterval->getFrom()); - - $this->assertIsObject($dateInterval->getTo()); - $this->assertEquals('1970-01-01', (string)$dateInterval->getTo()); - - $this->assertIsObject($dateInterval->getDuration()); - $this->assertEquals('15 years, 6 months, 29 days, 12 h', (string)$dateInterval->getDuration()); - } - - public function testIsDuring(): void - { - $dateInterval = new DateInterval( - '1954-06-07', - '1970-01-01' - ); - - // Before - $result = $dateInterval->isDuring('1954-05-6'); - $this->assertIsBool($result); - $this->assertEquals(false, $result); - - // After - $result = $dateInterval->isDuring('1970-01-03'); - $this->assertIsBool($result); - $this->assertEquals(false, $result); - - // During - $result = $dateInterval->isDuring('1960-02-23'); - $this->assertIsBool($result); - $this->assertEquals(true, $result); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new DateInterval(null, null); - } + public function testInterface(): void + { + $dateInterval = DateInterval::make( + '1954-06-07', + '1970-01-01' + ); + + $this->assertIsObject($dateInterval->from); + $this->assertEquals('1954-06-07', (string)$dateInterval->from); + + $this->assertIsObject($dateInterval->to); + $this->assertEquals('1970-01-01', (string)$dateInterval->to); + + $this->assertIsObject($dateInterval->duration); + $this->assertEquals('15 years, 6 months, 29 days, 12 h', (string)$dateInterval->duration); + } + + public function testIsDuring(): void + { + $dateInterval = DateInterval::make( + '1954-06-07', + '1970-01-01' + ); + + // Before + $result = $dateInterval->isDuring('1954-05-6'); + $this->assertIsBool($result); + $this->assertEquals(false, $result); + + // After + $result = $dateInterval->isDuring('1970-01-03'); + $this->assertIsBool($result); + $this->assertEquals(false, $result); + + // During + $result = $dateInterval->isDuring('1960-02-23'); + $this->assertIsBool($result); + $this->assertEquals(true, $result); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new DateInterval(null, null); + } } diff --git a/test/Time/DateTest.php b/test/Time/DateTest.php index 1f126f7..0188516 100644 --- a/test/Time/DateTest.php +++ b/test/Time/DateTest.php @@ -1,4 +1,5 @@ assertEquals('1954-06-07', (string)$date); - - $this->assertIsString($date->get()); - $this->assertEquals('1954-06-07', $date->get()); - - $this->assertIsInt($date->getTime()); - $this->assertEquals(-491356800, $date->getTime()); - } - - public function testBuild(): void - { - $date = new Date('now'); - - $this->assertIsObject($date); - } - - public function testBuildFromTime(): void - { - $date = new Date(-491356800); - - $this->assertIsObject($date); - - $this->assertIsString($date->get()); - $this->assertEquals('1954-06-07', $date->get()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Date('wingardium leviosa'); - } + public function testInterface(): void + { + $date = new Date('1954-06-07', 'Y-m-d'); + + $this->assertEquals('1954-06-07', (string)$date); + + $this->assertIsString($date->value); + $this->assertEquals('1954-06-07', $date->value); + + $this->assertIsInt($date->time); + $this->assertEquals(-491356800, $date->time); + } + + public function testBuild(): void + { + $date = new Date('now'); + + $this->assertIsObject($date); + } + + public function testBuildFromTime(): void + { + $date = new Date(-491356800); + + $this->assertIsObject($date); + + $this->assertIsString($date->value); + $this->assertEquals('1954-06-07', $date->value); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new Date('wingardium leviosa'); + } } diff --git a/test/Time/DateTimeIntervalTest.php b/test/Time/DateTimeIntervalTest.php index 26968e6..dc47935 100644 --- a/test/Time/DateTimeIntervalTest.php +++ b/test/Time/DateTimeIntervalTest.php @@ -1,4 +1,5 @@ assertIsObject($dateTimeInterval->getFrom()); - $this->assertEquals('1954-06-07 12:34:56', (string)$dateTimeInterval->getFrom()); - - $this->assertIsObject($dateTimeInterval->getTo()); - $this->assertEquals('1970-01-01 00:00:00', (string)$dateTimeInterval->getTo()); - - $this->assertIsObject($dateTimeInterval->getDuration()); - $this->assertEquals('15 years, 6 months, 28 days, 23 h, 25 min, 4 s', (string)$dateTimeInterval->getDuration()); - } - - public function testIsDuring(): void - { - $dateTimeInterval = new DateTimeInterval( - '1954-06-07 12:34:56', - '1970-01-01 00:00:00' - ); - - // Before - $result = $dateTimeInterval->isDuring('1954-05-07 12:34:55'); - $this->assertIsBool($result); - $this->assertEquals(false, $result); - - // After - $result = $dateTimeInterval->isDuring('1970-01-01 00:00:01'); - $this->assertIsBool($result); - $this->assertEquals(false, $result); - - // During - $result = $dateTimeInterval->isDuring('1960-02-23 12:34:55'); - $this->assertIsBool($result); - $this->assertEquals(true, $result); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new DateTimeInterval(null, null); - } + public function testInterface(): void + { + $dateTimeInterval = DateTimeInterval::make( + '1954-06-07 12:34:56', + '1970-01-01 00:00:00' + ); + + $this->assertIsObject($dateTimeInterval->from); + $this->assertEquals('1954-06-07 12:34:56', (string)$dateTimeInterval->from); + + $this->assertIsObject($dateTimeInterval->to); + $this->assertEquals('1970-01-01 00:00:00', (string)$dateTimeInterval->to); + + $this->assertIsObject($dateTimeInterval->duration); + $this->assertEquals('15 years, 6 months, 28 days, 23 h, 25 min, 4 s', (string)$dateTimeInterval->duration); + } + + public function testIsDuring(): void + { + $dateTimeInterval = DateTimeInterval::make( + '1954-06-07 12:34:56', + '1970-01-01 00:00:00' + ); + + // Before + $result = $dateTimeInterval->isDuring('1954-05-07 12:34:55'); + $this->assertIsBool($result); + $this->assertEquals(false, $result); + + // After + $result = $dateTimeInterval->isDuring('1970-01-01 00:00:01'); + $this->assertIsBool($result); + $this->assertEquals(false, $result); + + // During + $result = $dateTimeInterval->isDuring('1960-02-23 12:34:55'); + $this->assertIsBool($result); + $this->assertEquals(true, $result); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new DateTimeInterval(null, null); + } } diff --git a/test/Time/DateTimeTest.php b/test/Time/DateTimeTest.php index b2a3dcc..681b687 100644 --- a/test/Time/DateTimeTest.php +++ b/test/Time/DateTimeTest.php @@ -1,4 +1,5 @@ assertEquals('1954-06-07 12:34:56', (string)$dateTime); - - $this->assertIsString($dateTime->get()); - $this->assertEquals('1954-06-07 12:34:56', $dateTime->get()); - - $this->assertIsInt($dateTime->getTime()); - $this->assertEquals(-491311504, $dateTime->getTime()); - - $this->assertIsString($dateTime->getUtc()); - $this->assertEquals('1954-06-07T12:34:56Z', $dateTime->getUtc()); - } - - public function testBuild(): void - { - $dateTime = new DateTime('now'); - - $this->assertIsObject($dateTime); - } - - public function testBuildFromTime(): void - { - $dateTime = new DateTime(-491311504); - - $this->assertIsObject($dateTime); - - $this->assertIsString($dateTime->get()); - $this->assertEquals('1954-06-07 12:34:56', $dateTime->get()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new DateTime('wingardium leviosa'); - } + public function testInterface(): void + { + $dateTime = new DateTime('1954-06-07 12:34:56', 'Y-m-d H:i:s'); + + $this->assertEquals('1954-06-07 12:34:56', (string)$dateTime); + + $this->assertIsString($dateTime->value); + $this->assertEquals('1954-06-07 12:34:56', $dateTime->value); + + $this->assertIsInt($dateTime->time); + $this->assertEquals(-491311504, $dateTime->time); + + $this->assertIsString($dateTime->getUtc()); + $this->assertEquals('1954-06-07T12:34:56Z', $dateTime->getUtc()); + } + + public function testBuild(): void + { + $dateTime = new DateTime('now'); + + $this->assertIsObject($dateTime); + } + + public function testBuildFromTime(): void + { + $dateTime = new DateTime(-491311504); + + $this->assertIsObject($dateTime); + + $this->assertIsString($dateTime->value); + $this->assertEquals('1954-06-07 12:34:56', $dateTime->value); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new DateTime('wingardium leviosa'); + } } diff --git a/test/Time/DurationTest.php b/test/Time/DurationTest.php index 531ed11..34a4484 100644 --- a/test/Time/DurationTest.php +++ b/test/Time/DurationTest.php @@ -1,4 +1,5 @@ assertEquals('1 year, 1 month, 1 day, 1 h, 1 min, 1 s', (string)$duration); - - $this->assertIsInt($duration->get()); - $this->assertEquals($time, $duration->get()); - - $this->assertIsString($duration->getLabel()); - $this->assertEquals('1 year, 1 month, 1 day, 1 h, 1 min, 1 s', $duration->getLabel()); - } + public function testInterface(): void + { + $time = Duration::YEAR + Duration::MONTH + Duration::DAY + Duration::HOUR + Duration::MINUTE + 1; + $duration = new Duration($time); + + $this->assertEquals('1 year, 1 month, 1 day, 1 h, 1 min, 1 s', (string)$duration); + + $this->assertIsInt($duration->value); + $this->assertEquals($time, $duration->value); + + $this->assertIsString($duration->getLabel()); + $this->assertEquals('1 year, 1 month, 1 day, 1 h, 1 min, 1 s', $duration->getLabel()); + } } diff --git a/test/Token/JwtTest.php b/test/Token/JwtTest.php index 036d996..9574970 100644 --- a/test/Token/JwtTest.php +++ b/test/Token/JwtTest.php @@ -1,4 +1,5 @@ 'bar', - ], - 1 - ); - - $this->assertIsString((string)$jwt); - $this->assertIsString($jwt->get()); - - $payload = $jwt->decode(self::PUBLIC_KEY); - - $this->assertIsArray($payload); - $this->assertArrayHasKey('foo', $payload); - $this->assertEquals('bar', $payload['foo']); - } - - public function testNotCompliantExpiration(): void - { - $this->expectException(NotCompliant::class); - - (new Jwt('eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE2NTAwMTU2OTUsImV4cCI6MTY1MDAxNTY5Nn0.dAGjGeS5iCgYplBsp-ES2xu6fNwUyv3JAxSso6IpJlFSgx7r_d9mvkkPdeOAArMGVbcwCk6MUEqa32cyh_0TB8kse49DJ58m4-xfNazc7fkF9WwqrKA08CqBsDfALIsrqsaHkSqvRAgn-UZkWVKp1SoCN9xFLNiYl2_LUJvwwFQ')) - ->decode(self::PUBLIC_KEY); - } - - public function testNotCompliantSignature(): void - { - $this->expectException(NotCompliant::class); - - (new Jwt('eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE2NTAwMTYyOTgsImV4cCI6MTY1MDAxNjI5OX0.fiyJAOgDqx3t5825xyndVDj04j-REJh9JtvhMMS7bUpyX7MuKdekkzKVe65Vzo6U0rGkTiTAD3_q1G1OfhlXXlHPWtmYd3AHhGNMclP_uVszSx2XUK9_0kKF0rw5BU7I4JVVXDyAM9OovDlWJ9DZlXGLYRjadSvSzzroRhLGWEI')) - ->decode(self::PUBLIC_KEY); - } + + public function testInterface(): void + { + $jwt = Jwt::encode( + self::PRIVATE_KEY, + [ + 'foo' => 'bar', + ], + 1 + ); + + $this->assertIsString((string)$jwt); + $this->assertIsString($jwt->value); + + $payload = $jwt->decode(self::PUBLIC_KEY); + + $this->assertIsArray($payload); + $this->assertArrayHasKey('foo', $payload); + $this->assertEquals('bar', $payload['foo']); + } + + public function testNotCompliantExpiration(): void + { + $this->expectException(NotCompliant::class); + + (new Jwt('eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE2NTAwMTU2OTUsImV4cCI6MTY1MDAxNTY5Nn0.dAGjGeS5iCgYplBsp-ES2xu6fNwUyv3JAxSso6IpJlFSgx7r_d9mvkkPdeOAArMGVbcwCk6MUEqa32cyh_0TB8kse49DJ58m4-xfNazc7fkF9WwqrKA08CqBsDfALIsrqsaHkSqvRAgn-UZkWVKp1SoCN9xFLNiYl2_LUJvwwFQ')) + ->decode(self::PUBLIC_KEY); + } + + public function testNotCompliantSignature(): void + { + $this->expectException(NotCompliant::class); + + (new Jwt('eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE2NTAwMTYyOTgsImV4cCI6MTY1MDAxNjI5OX0.fiyJAOgDqx3t5825xyndVDj04j-REJh9JtvhMMS7bUpyX7MuKdekkzKVe65Vzo6U0rGkTiTAD3_q1G1OfhlXXlHPWtmYd3AHhGNMclP_uVszSx2XUK9_0kKF0rw5BU7I4JVVXDyAM9OovDlWJ9DZlXGLYRjadSvSzzroRhLGWEI')) + ->decode(self::PUBLIC_KEY); + } } diff --git a/test/Web/DomainNameTest.php b/test/Web/DomainNameTest.php index 1d6ef4e..53351e2 100644 --- a/test/Web/DomainNameTest.php +++ b/test/Web/DomainNameTest.php @@ -1,4 +1,5 @@ assertEquals('www.domain.ext', (string)$domainName); - - $this->assertIsString($domainName->get()); - $this->assertEquals('www.domain.ext', $domainName->get()); - - $this->assertIsString($domainName->getName()); - $this->assertEquals('www.domain', $domainName->getName()); - - $this->assertIsObject($domainName->getPretty()); - $this->assertEquals('domain.ext', $domainName->getPretty()->get()); - - $this->assertIsString($domainName->getExtension()); - $this->assertEquals('ext', $domainName->getExtension()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new DomainName('domain'); - } + public function testInterface(): void + { + $domainName = new DomainName('www.domain.ext'); + + $this->assertEquals('www.domain.ext', (string)$domainName); + + $this->assertIsString($domainName->value); + $this->assertEquals('www.domain.ext', $domainName->value); + + $this->assertIsString($domainName->getName()); + $this->assertEquals('www.domain', $domainName->getName()); + + $this->assertIsObject($domainName->getPretty()); + $this->assertEquals('domain.ext', $domainName->getPretty()->value); + + $this->assertIsString($domainName->getExtension()); + $this->assertEquals('ext', $domainName->getExtension()); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new DomainName('domain'); + } } diff --git a/test/Web/EmailAddressAndNameTest.php b/test/Web/EmailAddressAndNameTest.php index 15b232f..299798c 100644 --- a/test/Web/EmailAddressAndNameTest.php +++ b/test/Web/EmailAddressAndNameTest.php @@ -1,4 +1,5 @@ assertIsObject($emailAddressAndName->getEmailAddress()); - $this->assertEquals('john.doe@domain.ext', (string)$emailAddressAndName->getEmailAddress()); - - $this->assertIsString($emailAddressAndName->getName()); - $this->assertEquals('John DOE', $emailAddressAndName->getName()); - } + public function testInterface(): void + { + $emailAddressAndName = new EmailAddressAndName( + 'john.doe@domain.ext', + 'John DOE' + ); + + $this->assertIsObject($emailAddressAndName->getEmailAddress()); + $this->assertEquals('john.doe@domain.ext', (string)$emailAddressAndName->getEmailAddress()); + + $this->assertIsString($emailAddressAndName->getName()); + $this->assertEquals('John DOE', $emailAddressAndName->getName()); + } } diff --git a/test/Web/EmailAddressTest.php b/test/Web/EmailAddressTest.php index b563ea1..9fcabdc 100644 --- a/test/Web/EmailAddressTest.php +++ b/test/Web/EmailAddressTest.php @@ -1,49 +1,50 @@ assertEquals('username@domain.ext', (string)$emailAddress); - - $this->assertIsString($emailAddress->get()); - $this->assertEquals('username@domain.ext', $emailAddress->get()); - - $this->assertIsObject($emailAddress->getUserName()); - $this->assertEquals('username', (string)$emailAddress->getUserName()); - - $this->assertIsObject($emailAddress->getDomainName()); - $this->assertEquals('domain.ext', (string)$emailAddress->getDomainName()); - } - - public function testBuild(): void - { - $emailAddress = EmailAddress::build( - new UserName('username'), - new DomainName('domain.ext') - ); - - $this->assertIsObject($emailAddress); - $this->assertEquals('username@domain.ext', (string)$emailAddress); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new EmailAddress('username@domain-ext'); - } + public function testInterface(): void + { + $emailAddress = new EmailAddress('username@domain.ext'); + + $this->assertEquals('username@domain.ext', (string)$emailAddress); + + $this->assertIsString($emailAddress->value); + $this->assertEquals('username@domain.ext', $emailAddress->value); + + $this->assertIsObject($emailAddress->getUserName()); + $this->assertEquals('username', (string)$emailAddress->getUserName()); + + $this->assertIsObject($emailAddress->getDomainName()); + $this->assertEquals('domain.ext', (string)$emailAddress->getDomainName()); + } + + public function testMake(): void + { + $emailAddress = EmailAddress::make( + new UserName('username'), + new DomainName('domain.ext') + ); + + $this->assertIsObject($emailAddress); + $this->assertEquals('username@domain.ext', (string)$emailAddress); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new EmailAddress('username@domain-ext'); + } } diff --git a/test/Web/EmailTest.php b/test/Web/EmailTest.php index 40114a1..c8ab4b0 100644 --- a/test/Web/EmailTest.php +++ b/test/Web/EmailTest.php @@ -1,81 +1,58 @@ Message

', - new EmailAddressAndName( - new EmailAddress('contact@acme.ext'), - 'Acme' - ), - new EmailAddressAndName( - new EmailAddress('john.doe@domain.ext'), - 'John DOE' - ), - new EmailAddressAndName( - new EmailAddress('no-reply@acme.ext'), - 'No reply' - ) - ); - - $this->assertIsString($email->subject); - $this->assertEquals('Subject', $email->subject); - - $this->assertIsString($email->messageTxt); - $this->assertEquals('Message', $email->messageTxt); - - $this->assertIsString($email->messageHtml); - $this->assertEquals('

Message

', $email->messageHtml); - - $this->assertIsObject($email->from); - $this->assertEquals('contact@acme.ext', (string)$email->from->getEmailAddress()); - $this->assertEquals('Acme', $email->from->getName()); - - $this->assertIsObject($email->to); - $this->assertEquals('john.doe@domain.ext', (string)$email->to->getEmailAddress()); - $this->assertEquals('John DOE', $email->to->getName()); - - $this->assertIsObject($email->replyTo); - $this->assertEquals('no-reply@acme.ext', (string)$email->replyTo->getEmailAddress()); - $this->assertEquals('No reply', $email->replyTo->getName()); - } - - public function testConstructAlt(): void - { - $email = new Email( - 'Subject', - 'Message', - '

Message

', - 'contact@acme.ext', - 'john.doe@domain.ext', - 'no-reply@acme.ext' - ); - - $this->assertIsObject($email->from); - $this->assertEquals('contact@acme.ext', (string)$email->from->getEmailAddress()); - $this->assertEquals(null, $email->from->getName()); - - $this->assertIsObject($email->to); - $this->assertEquals('john.doe@domain.ext', (string)$email->to->getEmailAddress()); - $this->assertEquals(null, $email->to->getName()); - - $this->assertIsObject($email->replyTo); - $this->assertEquals('no-reply@acme.ext', (string)$email->replyTo->getEmailAddress()); - $this->assertEquals(null, $email->replyTo->getName()); - } + public function testInterface(): void + { + $email = new Email( + 'Subject', + 'Message', + '

Message

', + new EmailAddressAndName( + new EmailAddress('contact@acme.ext'), + 'Acme' + ), + new EmailAddressAndName( + new EmailAddress('john.doe@domain.ext'), + 'John DOE' + ), + new EmailAddressAndName( + new EmailAddress('no-reply@acme.ext'), + 'No reply' + ) + ); + + $this->assertIsString($email->subject); + $this->assertEquals('Subject', $email->subject); + + $this->assertIsString($email->messageTxt); + $this->assertEquals('Message', $email->messageTxt); + + $this->assertIsString($email->messageHtml); + $this->assertEquals('

Message

', $email->messageHtml); + + $this->assertIsObject($email->from); + $this->assertEquals('contact@acme.ext', (string)$email->from->getEmailAddress()); + $this->assertEquals('Acme', $email->from->getName()); + + $this->assertIsObject($email->to); + $this->assertEquals('john.doe@domain.ext', (string)$email->to->getEmailAddress()); + $this->assertEquals('John DOE', $email->to->getName()); + + $this->assertIsObject($email->replyTo); + $this->assertEquals('no-reply@acme.ext', (string)$email->replyTo->getEmailAddress()); + $this->assertEquals('No reply', $email->replyTo->getName()); + } } diff --git a/test/Web/UrlTest.php b/test/Web/UrlTest.php index b0b7def..33310a1 100644 --- a/test/Web/UrlTest.php +++ b/test/Web/UrlTest.php @@ -1,4 +1,5 @@ assertEquals('http://username:password@hostname:9090/path?arg=value#anchor', (string)$url); - - $this->assertIsString($url->get()); - $this->assertEquals('http://username:password@hostname:9090/path?arg=value#anchor', $url->get()); - - $this->assertIsString($url->getScheme()); - $this->assertEquals('http', $url->getScheme()); - $url = $url->setScheme('https'); - $this->assertEquals('https', $url->getScheme()); - - $this->assertIsString($url->getUser()); - $this->assertEquals('username', $url->getUser()); - $url = $url->setUser('john.doe'); - $this->assertEquals('john.doe', $url->getUser()); - - $this->assertIsString($url->getPass()); - $this->assertEquals('password', $url->getPass()); - $url = $url->setPass('qwerty'); - $this->assertEquals('qwerty', $url->getPass()); - - $this->assertIsString($url->getHost()); - $this->assertEquals('hostname', $url->getHost()); - $url = $url->setHost('acme'); - $this->assertEquals('acme', $url->getHost()); - - $this->assertIsInt($url->getPort()); - $this->assertEquals(9090, $url->getPort()); - $url = $url->setPort(8080); - $this->assertEquals(8080, $url->getPort()); - - $this->assertIsString($url->getPath()); - $this->assertEquals('/path', $url->getPath()); - $url = $url->setPath('home'); - $this->assertEquals('/home', $url->getPath()); - - $this->assertIsArray($url->getQuery()); - $this->assertEquals(['arg' => 'value'], $url->getQuery()); - $url = $url->removeQueryParameter('arg'); - $url = $url->addQueryParameter('foo', 'bar'); - $this->assertEquals(['foo' => 'bar'], $url->getQuery()); - - $this->assertIsString($url->getFragment()); - $this->assertEquals('anchor', $url->getFragment()); - $url = $url->setFragment('title'); - $this->assertEquals('title', $url->getFragment()); - - $this->assertEquals('https://john.doe:qwerty@acme:8080/home?foo=bar#title', (string)$url); - - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new Url('http//username:password@hostname:9090/path?arg=value#anchor'); - } + public function testInterface(): void + { + $url = new Url('http://username:password@hostname:9090/path?arg=value#anchor'); + + $this->assertEquals('http://username:password@hostname:9090/path?arg=value#anchor', (string)$url); + + $this->assertIsString($url->value); + $this->assertEquals('http://username:password@hostname:9090/path?arg=value#anchor', $url->value); + + $this->assertIsString($url->getScheme()); + $this->assertEquals('http', $url->getScheme()); + $url = $url->setScheme('https'); + $this->assertEquals('https', $url->getScheme()); + + $this->assertIsString($url->getUser()); + $this->assertEquals('username', $url->getUser()); + $url = $url->setUser('john.doe'); + $this->assertEquals('john.doe', $url->getUser()); + + $this->assertIsString($url->getPass()); + $this->assertEquals('password', $url->getPass()); + $url = $url->setPass('qwerty'); + $this->assertEquals('qwerty', $url->getPass()); + + $this->assertIsString($url->getHost()); + $this->assertEquals('hostname', $url->getHost()); + $url = $url->setHost('acme'); + $this->assertEquals('acme', $url->getHost()); + + $this->assertIsInt($url->getPort()); + $this->assertEquals(9090, $url->getPort()); + $url = $url->setPort(8080); + $this->assertEquals(8080, $url->getPort()); + + $this->assertIsString($url->getPath()); + $this->assertEquals('/path', $url->getPath()); + $url = $url->setPath('home'); + $this->assertEquals('/home', $url->getPath()); + + $this->assertIsArray($url->getQuery()); + $this->assertEquals(['arg' => 'value'], $url->getQuery()); + $url = $url->removeQueryParameter('arg'); + $url = $url->addQueryParameter('foo', 'bar'); + $this->assertEquals(['foo' => 'bar'], $url->getQuery()); + + $this->assertIsString($url->getFragment()); + $this->assertEquals('anchor', $url->getFragment()); + $url = $url->setFragment('title'); + $this->assertEquals('title', $url->getFragment()); + + $this->assertEquals('https://john.doe:qwerty@acme:8080/home?foo=bar#title', (string)$url); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new Url('http//username:password@hostname:9090/path?arg=value#anchor'); + } } diff --git a/test/Web/UserNameTest.php b/test/Web/UserNameTest.php index 890e52c..bbc9418 100644 --- a/test/Web/UserNameTest.php +++ b/test/Web/UserNameTest.php @@ -1,4 +1,5 @@ assertEquals('username', (string)$userName); - - $this->assertIsString($userName->get()); - $this->assertEquals('username', $userName->get()); - } - - public function testNotCompliant(): void - { - $this->expectException(NotCompliant::class); - - new UserName('user@name'); - } + public function testInterface(): void + { + $userName = new UserName('john.doe'); + + $this->assertEquals('john.doe', (string)$userName); + + $this->assertIsString($userName->value); + $this->assertEquals('john.doe', $userName->value); + } + + public function testNotCompliant(): void + { + $this->expectException(NotCompliant::class); + + new UserName('user@name'); + } } From 12a51820e93eae070a9331526e8f454a42fec87e Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Fri, 21 Oct 2022 19:32:23 +0200 Subject: [PATCH 5/7] Tests --- component/Geography/GpsCoordinates.php | 2 +- component/Time/Date.php | 5 +++ component/Time/Duration.php | 2 +- test/Geography/GpsCoordinatesTest.php | 17 +++++++-- test/Money/CurrencyTest.php | 17 +++++++-- test/Number/GradeTest.php | 13 +++++++ test/Time/DateTest.php | 8 ++--- test/Time/DateTimeTest.php | 8 ++--- test/Time/DurationTest.php | 4 +-- test/Web/EmailAddressAndNameTest.php | 29 +++++++++++---- test/Web/EmailAddressTest.php | 2 ++ test/Web/EmailTest.php | 50 ++++++++++++++++++++++---- test/Web/UrlTest.php | 2 ++ 13 files changed, 131 insertions(+), 28 deletions(-) diff --git a/component/Geography/GpsCoordinates.php b/component/Geography/GpsCoordinates.php index 2df30bf..cdd1770 100644 --- a/component/Geography/GpsCoordinates.php +++ b/component/Geography/GpsCoordinates.php @@ -25,7 +25,7 @@ public function __toString(): string public static function make(string $coordinates): self { - $parts = explode(';', $coordinates); + $parts = explode(';', str_replace(',', '.', $coordinates)); return new static( (float) $parts[0], diff --git a/component/Time/Date.php b/component/Time/Date.php index d150304..8dc2e37 100644 --- a/component/Time/Date.php +++ b/component/Time/Date.php @@ -30,4 +30,9 @@ public function __construct( $this->format = $format; $this->date = date($format, $this->time); } + + public function __toString() + { + return $this->date; + } } diff --git a/component/Time/Duration.php b/component/Time/Duration.php index 10af8b2..862aeb9 100644 --- a/component/Time/Duration.php +++ b/component/Time/Duration.php @@ -28,7 +28,7 @@ class Duration public const YEAR_LABEL = 'year'; public const YEAR_LABEL_PLURAL = 'years'; - protected readonly string $label; + public readonly string $label; public function __construct( public readonly int $value diff --git a/test/Geography/GpsCoordinatesTest.php b/test/Geography/GpsCoordinatesTest.php index a39be41..1c27f88 100644 --- a/test/Geography/GpsCoordinatesTest.php +++ b/test/Geography/GpsCoordinatesTest.php @@ -22,8 +22,21 @@ public function testInterface(): void $this->assertIsFloat($gpsCoordinates->longitude); $this->assertEquals(-2.2117116, $gpsCoordinates->longitude); } + + public function testMake(): void + { + $gpsCoordinates = GpsCoordinates::make(' 53,3284723 ; -2,2117116 '); + + $this->assertInstanceOf(GpsCoordinates::class, $gpsCoordinates); + + $this->assertIsFloat($gpsCoordinates->latitude); + $this->assertEquals(53.3284723, $gpsCoordinates->latitude); + + $this->assertIsFloat($gpsCoordinates->longitude); + $this->assertEquals(-2.2117116, $gpsCoordinates->longitude); + } - public function testCreateFromLambert93(): void + public function testMakeFromLambert93(): void { $gpsCoordinates = GpsCoordinates::makeFromLambert93(350772.53, 7372163.87); @@ -31,7 +44,7 @@ public function testCreateFromLambert93(): void $this->assertEquals(-2.2117116, $gpsCoordinates->longitude); } - public function testCreateFromUtm(): void + public function testMakeFromUtm(): void { $gpsCoordinates = GpsCoordinates::makeFromUtm(153037.74, 5921483.19, 31); diff --git a/test/Money/CurrencyTest.php b/test/Money/CurrencyTest.php index 1c5d421..f3cc542 100644 --- a/test/Money/CurrencyTest.php +++ b/test/Money/CurrencyTest.php @@ -11,11 +11,24 @@ final class CurrencyTest extends \PHPUnit\Framework\TestCase public function testInterface(): void { $currency = Currency::EUR; - + $this->assertIsString($currency->value); $this->assertEquals('EUR', $currency->value); - + $this->assertIsString($currency->getLabel()); $this->assertEquals('€', $currency->getLabel()); } + + public function testGetLabel(): void + { + $this->assertIsString(Currency::AUD->getLabel()); + $this->assertIsString(Currency::CAD->getLabel()); + $this->assertIsString(Currency::CHF->getLabel()); + $this->assertIsString(Currency::DKK->getLabel()); + $this->assertIsString(Currency::EUR->getLabel()); + $this->assertIsString(Currency::GBP->getLabel()); + $this->assertIsString(Currency::SEK->getLabel()); + $this->assertIsString(Currency::USD->getLabel()); + $this->assertIsString(Currency::ZSD->getLabel()); + } } diff --git a/test/Number/GradeTest.php b/test/Number/GradeTest.php index 43ed1f3..3dc5fe2 100644 --- a/test/Number/GradeTest.php +++ b/test/Number/GradeTest.php @@ -22,6 +22,19 @@ public function testInterface(): void $this->assertIsInt($grade->scale); $this->assertEquals(10, $grade->scale); } + + public function testMake(): void + { + $grade = Grade::make(' 8 / 10 '); + + $this->assertInstanceOf(Grade::class, $grade); + + $this->assertIsInt($grade->position); + $this->assertEquals(8, $grade->position); + + $this->assertIsInt($grade->scale); + $this->assertEquals(10, $grade->scale); + } public function testNotCompliantGrade(): void { diff --git a/test/Time/DateTest.php b/test/Time/DateTest.php index 0188516..170619d 100644 --- a/test/Time/DateTest.php +++ b/test/Time/DateTest.php @@ -16,8 +16,8 @@ public function testInterface(): void $this->assertEquals('1954-06-07', (string)$date); - $this->assertIsString($date->value); - $this->assertEquals('1954-06-07', $date->value); + $this->assertIsString($date->date); + $this->assertEquals('1954-06-07', $date->date); $this->assertIsInt($date->time); $this->assertEquals(-491356800, $date->time); @@ -36,8 +36,8 @@ public function testBuildFromTime(): void $this->assertIsObject($date); - $this->assertIsString($date->value); - $this->assertEquals('1954-06-07', $date->value); + $this->assertIsString($date->date); + $this->assertEquals('1954-06-07', $date->date); } public function testNotCompliant(): void diff --git a/test/Time/DateTimeTest.php b/test/Time/DateTimeTest.php index 681b687..131fae1 100644 --- a/test/Time/DateTimeTest.php +++ b/test/Time/DateTimeTest.php @@ -16,8 +16,8 @@ public function testInterface(): void $this->assertEquals('1954-06-07 12:34:56', (string)$dateTime); - $this->assertIsString($dateTime->value); - $this->assertEquals('1954-06-07 12:34:56', $dateTime->value); + $this->assertIsString($dateTime->date); + $this->assertEquals('1954-06-07 12:34:56', $dateTime->date); $this->assertIsInt($dateTime->time); $this->assertEquals(-491311504, $dateTime->time); @@ -39,8 +39,8 @@ public function testBuildFromTime(): void $this->assertIsObject($dateTime); - $this->assertIsString($dateTime->value); - $this->assertEquals('1954-06-07 12:34:56', $dateTime->value); + $this->assertIsString($dateTime->date); + $this->assertEquals('1954-06-07 12:34:56', $dateTime->date); } public function testNotCompliant(): void diff --git a/test/Time/DurationTest.php b/test/Time/DurationTest.php index 34a4484..f8333a7 100644 --- a/test/Time/DurationTest.php +++ b/test/Time/DurationTest.php @@ -20,7 +20,7 @@ public function testInterface(): void $this->assertIsInt($duration->value); $this->assertEquals($time, $duration->value); - $this->assertIsString($duration->getLabel()); - $this->assertEquals('1 year, 1 month, 1 day, 1 h, 1 min, 1 s', $duration->getLabel()); + $this->assertIsString($duration->label); + $this->assertEquals('1 year, 1 month, 1 day, 1 h, 1 min, 1 s', $duration->label); } } diff --git a/test/Web/EmailAddressAndNameTest.php b/test/Web/EmailAddressAndNameTest.php index 299798c..911a240 100644 --- a/test/Web/EmailAddressAndNameTest.php +++ b/test/Web/EmailAddressAndNameTest.php @@ -5,6 +5,7 @@ namespace Test\Web; use Phant\DataStructure\Web\EmailAddressAndName; +use Phant\DataStructure\Web\EmailAddress; use Phant\Error\NotCompliant; @@ -13,14 +14,30 @@ final class EmailAddressAndNameTest extends \PHPUnit\Framework\TestCase public function testInterface(): void { $emailAddressAndName = new EmailAddressAndName( + new EmailAddress('john.doe@domain.ext'), + 'John DOE' + ); + + $this->assertIsObject($emailAddressAndName->emailAddress); + $this->assertEquals('john.doe@domain.ext', (string)$emailAddressAndName->emailAddress); + + $this->assertIsString($emailAddressAndName->name); + $this->assertEquals('John DOE', $emailAddressAndName->name); + } + + public function testMake(): void + { + $emailAddressAndName = EmailAddressAndName::make( 'john.doe@domain.ext', 'John DOE' ); - - $this->assertIsObject($emailAddressAndName->getEmailAddress()); - $this->assertEquals('john.doe@domain.ext', (string)$emailAddressAndName->getEmailAddress()); - - $this->assertIsString($emailAddressAndName->getName()); - $this->assertEquals('John DOE', $emailAddressAndName->getName()); + + $this->assertInstanceOf(EmailAddressAndName::class, $emailAddressAndName); + + $this->assertIsObject($emailAddressAndName->emailAddress); + $this->assertEquals('john.doe@domain.ext', (string)$emailAddressAndName->emailAddress); + + $this->assertIsString($emailAddressAndName->name); + $this->assertEquals('John DOE', $emailAddressAndName->name); } } diff --git a/test/Web/EmailAddressTest.php b/test/Web/EmailAddressTest.php index 9fcabdc..17f69e4 100644 --- a/test/Web/EmailAddressTest.php +++ b/test/Web/EmailAddressTest.php @@ -36,6 +36,8 @@ public function testMake(): void new UserName('username'), new DomainName('domain.ext') ); + + $this->assertInstanceOf(EmailAddress::class, $emailAddress); $this->assertIsObject($emailAddress); $this->assertEquals('username@domain.ext', (string)$emailAddress); diff --git a/test/Web/EmailTest.php b/test/Web/EmailTest.php index c8ab4b0..af2f3a7 100644 --- a/test/Web/EmailTest.php +++ b/test/Web/EmailTest.php @@ -44,15 +44,53 @@ public function testInterface(): void $this->assertEquals('

Message

', $email->messageHtml); $this->assertIsObject($email->from); - $this->assertEquals('contact@acme.ext', (string)$email->from->getEmailAddress()); - $this->assertEquals('Acme', $email->from->getName()); + $this->assertEquals('contact@acme.ext', (string)$email->from->emailAddress); + $this->assertEquals('Acme', $email->from->name); $this->assertIsObject($email->to); - $this->assertEquals('john.doe@domain.ext', (string)$email->to->getEmailAddress()); - $this->assertEquals('John DOE', $email->to->getName()); + $this->assertEquals('john.doe@domain.ext', (string)$email->to->emailAddress); + $this->assertEquals('John DOE', $email->to->name); $this->assertIsObject($email->replyTo); - $this->assertEquals('no-reply@acme.ext', (string)$email->replyTo->getEmailAddress()); - $this->assertEquals('No reply', $email->replyTo->getName()); + $this->assertEquals('no-reply@acme.ext', (string)$email->replyTo->emailAddress); + $this->assertEquals('No reply', $email->replyTo->name); + } + + public function testMake(): void + { + $email = Email::make( + 'Subject', + 'Message', + '

Message

', + 'contact@acme.ext', + 'Acme', + 'john.doe@domain.ext', + 'John DOE', + 'no-reply@acme.ext', + 'No reply' + ); + + $this->assertInstanceOf(Email::class, $email); + + $this->assertIsString($email->subject); + $this->assertEquals('Subject', $email->subject); + + $this->assertIsString($email->messageTxt); + $this->assertEquals('Message', $email->messageTxt); + + $this->assertIsString($email->messageHtml); + $this->assertEquals('

Message

', $email->messageHtml); + + $this->assertIsObject($email->from); + $this->assertEquals('contact@acme.ext', (string)$email->from->emailAddress); + $this->assertEquals('Acme', $email->from->name); + + $this->assertIsObject($email->to); + $this->assertEquals('john.doe@domain.ext', (string)$email->to->emailAddress); + $this->assertEquals('John DOE', $email->to->name); + + $this->assertIsObject($email->replyTo); + $this->assertEquals('no-reply@acme.ext', (string)$email->replyTo->emailAddress); + $this->assertEquals('No reply', $email->replyTo->name); } } diff --git a/test/Web/UrlTest.php b/test/Web/UrlTest.php index 33310a1..8a2c047 100644 --- a/test/Web/UrlTest.php +++ b/test/Web/UrlTest.php @@ -52,6 +52,8 @@ public function testInterface(): void $this->assertIsArray($url->getQuery()); $this->assertEquals(['arg' => 'value'], $url->getQuery()); $url = $url->removeQueryParameter('arg'); + $url = $url->removeQueryParameter('foo'); + $this->assertNull($url->getQuery()); $url = $url->addQueryParameter('foo', 'bar'); $this->assertEquals(['foo' => 'bar'], $url->getQuery()); From 34ee571b79fe31bca67152671ec7d52269c78717 Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Fri, 21 Oct 2022 19:34:10 +0200 Subject: [PATCH 6/7] Doc --- README.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 39030e0..79284b8 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,8 @@ PHP >= 8.0 ## Abstract -- Aggregate - Collection -- Entity -- Enum +- CollectionPaginated - Value - Boolean - Decimal @@ -38,6 +36,12 @@ PHP >= 8.0 - GpsCoordinates +### Fr + +- CodeCommune +- CodePostal +- NumeroDepartement + ## Id @@ -52,16 +56,16 @@ PHP >= 8.0 ## Number -- Note +- Grade - Rate ## Person +- Birthday - Firstname - Gender - Lastname -- Person ## Time From bed221030a13e8c5c6d9d2eb40603ff0d082d0aa Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Fri, 21 Oct 2022 19:34:28 +0200 Subject: [PATCH 7/7] CS --- component/Time/Date.php | 2 +- component/Web/EmailAddress.php | 5 ++--- test/Geography/GpsCoordinatesTest.php | 8 ++++---- test/Money/CurrencyTest.php | 6 +++--- test/Number/GradeTest.php | 8 ++++---- test/Web/EmailAddressAndNameTest.php | 12 ++++++------ test/Web/EmailAddressTest.php | 2 +- test/Web/EmailTest.php | 16 ++++++++-------- 8 files changed, 29 insertions(+), 30 deletions(-) diff --git a/component/Time/Date.php b/component/Time/Date.php index 8dc2e37..2407112 100644 --- a/component/Time/Date.php +++ b/component/Time/Date.php @@ -30,7 +30,7 @@ public function __construct( $this->format = $format; $this->date = date($format, $this->time); } - + public function __toString() { return $this->date; diff --git a/component/Web/EmailAddress.php b/component/Web/EmailAddress.php index 7491ad2..6d0fba6 100644 --- a/component/Web/EmailAddress.php +++ b/component/Web/EmailAddress.php @@ -12,7 +12,7 @@ class EmailAddress extends \Phant\DataStructure\Abstract\Value\Varchar { public const PATTERN = '/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/'; - + public function __construct(string $emailAddress) { $emailAddress = preg_replace('/ /', '', $emailAddress); @@ -38,8 +38,7 @@ public function getDomainName(): DomainName public static function make( UserName $userName, DomainName $domainName - ): self - { + ): self { return new static($userName . '@' . $domainName); } } diff --git a/test/Geography/GpsCoordinatesTest.php b/test/Geography/GpsCoordinatesTest.php index 1c27f88..77836cd 100644 --- a/test/Geography/GpsCoordinatesTest.php +++ b/test/Geography/GpsCoordinatesTest.php @@ -22,16 +22,16 @@ public function testInterface(): void $this->assertIsFloat($gpsCoordinates->longitude); $this->assertEquals(-2.2117116, $gpsCoordinates->longitude); } - + public function testMake(): void { $gpsCoordinates = GpsCoordinates::make(' 53,3284723 ; -2,2117116 '); - + $this->assertInstanceOf(GpsCoordinates::class, $gpsCoordinates); - + $this->assertIsFloat($gpsCoordinates->latitude); $this->assertEquals(53.3284723, $gpsCoordinates->latitude); - + $this->assertIsFloat($gpsCoordinates->longitude); $this->assertEquals(-2.2117116, $gpsCoordinates->longitude); } diff --git a/test/Money/CurrencyTest.php b/test/Money/CurrencyTest.php index f3cc542..5ffb83d 100644 --- a/test/Money/CurrencyTest.php +++ b/test/Money/CurrencyTest.php @@ -11,14 +11,14 @@ final class CurrencyTest extends \PHPUnit\Framework\TestCase public function testInterface(): void { $currency = Currency::EUR; - + $this->assertIsString($currency->value); $this->assertEquals('EUR', $currency->value); - + $this->assertIsString($currency->getLabel()); $this->assertEquals('€', $currency->getLabel()); } - + public function testGetLabel(): void { $this->assertIsString(Currency::AUD->getLabel()); diff --git a/test/Number/GradeTest.php b/test/Number/GradeTest.php index 3dc5fe2..f6bac8e 100644 --- a/test/Number/GradeTest.php +++ b/test/Number/GradeTest.php @@ -22,16 +22,16 @@ public function testInterface(): void $this->assertIsInt($grade->scale); $this->assertEquals(10, $grade->scale); } - + public function testMake(): void { $grade = Grade::make(' 8 / 10 '); - + $this->assertInstanceOf(Grade::class, $grade); - + $this->assertIsInt($grade->position); $this->assertEquals(8, $grade->position); - + $this->assertIsInt($grade->scale); $this->assertEquals(10, $grade->scale); } diff --git a/test/Web/EmailAddressAndNameTest.php b/test/Web/EmailAddressAndNameTest.php index 911a240..b5b9bf3 100644 --- a/test/Web/EmailAddressAndNameTest.php +++ b/test/Web/EmailAddressAndNameTest.php @@ -17,26 +17,26 @@ public function testInterface(): void new EmailAddress('john.doe@domain.ext'), 'John DOE' ); - + $this->assertIsObject($emailAddressAndName->emailAddress); $this->assertEquals('john.doe@domain.ext', (string)$emailAddressAndName->emailAddress); - + $this->assertIsString($emailAddressAndName->name); $this->assertEquals('John DOE', $emailAddressAndName->name); } - + public function testMake(): void { $emailAddressAndName = EmailAddressAndName::make( 'john.doe@domain.ext', 'John DOE' ); - + $this->assertInstanceOf(EmailAddressAndName::class, $emailAddressAndName); - + $this->assertIsObject($emailAddressAndName->emailAddress); $this->assertEquals('john.doe@domain.ext', (string)$emailAddressAndName->emailAddress); - + $this->assertIsString($emailAddressAndName->name); $this->assertEquals('John DOE', $emailAddressAndName->name); } diff --git a/test/Web/EmailAddressTest.php b/test/Web/EmailAddressTest.php index 17f69e4..fafcc6d 100644 --- a/test/Web/EmailAddressTest.php +++ b/test/Web/EmailAddressTest.php @@ -36,7 +36,7 @@ public function testMake(): void new UserName('username'), new DomainName('domain.ext') ); - + $this->assertInstanceOf(EmailAddress::class, $emailAddress); $this->assertIsObject($emailAddress); diff --git a/test/Web/EmailTest.php b/test/Web/EmailTest.php index af2f3a7..7154e42 100644 --- a/test/Web/EmailTest.php +++ b/test/Web/EmailTest.php @@ -55,7 +55,7 @@ public function testInterface(): void $this->assertEquals('no-reply@acme.ext', (string)$email->replyTo->emailAddress); $this->assertEquals('No reply', $email->replyTo->name); } - + public function testMake(): void { $email = Email::make( @@ -69,26 +69,26 @@ public function testMake(): void 'no-reply@acme.ext', 'No reply' ); - + $this->assertInstanceOf(Email::class, $email); - + $this->assertIsString($email->subject); $this->assertEquals('Subject', $email->subject); - + $this->assertIsString($email->messageTxt); $this->assertEquals('Message', $email->messageTxt); - + $this->assertIsString($email->messageHtml); $this->assertEquals('

Message

', $email->messageHtml); - + $this->assertIsObject($email->from); $this->assertEquals('contact@acme.ext', (string)$email->from->emailAddress); $this->assertEquals('Acme', $email->from->name); - + $this->assertIsObject($email->to); $this->assertEquals('john.doe@domain.ext', (string)$email->to->emailAddress); $this->assertEquals('John DOE', $email->to->name); - + $this->assertIsObject($email->replyTo); $this->assertEquals('no-reply@acme.ext', (string)$email->replyTo->emailAddress); $this->assertEquals('No reply', $email->replyTo->name);