From 7f5423f5f23e8c6ca31ce12a26ff7a38a22c7b2d Mon Sep 17 00:00:00 2001 From: David Weichert Date: Sat, 20 Jul 2024 16:02:01 +0200 Subject: [PATCH 01/10] :see_no_evil: Add caches to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 24e3df6..3d268dc 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ createPhar createPhar.asc composer.lock phpunit.xml +/.phpunit.cache/ +/.php-cs-fixer.cache +/.phpunit.result.cache From 47b8a8229830f10089d5f93ac23ca04ede945c24 Mon Sep 17 00:00:00 2001 From: David Weichert Date: Sat, 20 Jul 2024 16:10:01 +0200 Subject: [PATCH 02/10] :recycle: Refa tests, fixes #58 - add test case for locale 'de' to FormatterFactoryTest - use short array syntax - add type hints for parameters in tests - add return type hints in tests --- .../tests/lib/DdateHolydayTest.php | 237 +++++++++--------- .../tests/lib/DdateLocalizedTest.php | 94 +++---- .../tests/lib/DdateTest.php | 235 ++++++++--------- .../tests/lib/FormatterFactoryTest.php | 51 ++-- .../tests/lib/Holydays/EristerTest.php | 26 +- 5 files changed, 290 insertions(+), 353 deletions(-) diff --git a/src/EmperorNortonCommands/tests/lib/DdateHolydayTest.php b/src/EmperorNortonCommands/tests/lib/DdateHolydayTest.php index e7a02c6..0a510c9 100644 --- a/src/EmperorNortonCommands/tests/lib/DdateHolydayTest.php +++ b/src/EmperorNortonCommands/tests/lib/DdateHolydayTest.php @@ -11,157 +11,162 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -/** - * Class DdateLocalizedTest. - * - * @package EmperorNortonCommands\tests\lib - */ -class DdateHolydayTest extends TestCase +final class DdateHolydayTest extends TestCase { - /** - * @var Ddate - */ - private $object; - - protected function setUp(): void - { - $this->object = new Ddate(); - } - #[DataProvider('turnOffStandardHolydaysProvider')] - public function testTurnOffStandardHolydays($gregorian, $discordian, $format, $locale) - { - $actual = $this->object->ddate($format, $gregorian, $locale); + public function testTurnOffStandardHolydays( + string $gregorian, + string $discordian, + string|null $format, + string|null $locale, + ): void { + $actual = (new Ddate())->ddate($format, $gregorian, $locale); self::assertEquals($discordian, $actual); } - public static function turnOffStandardHolydaysProvider() + public static function turnOffStandardHolydaysProvider(): array { - return array( - 'no format' => array('26092013', "Prickle-Prickle, Bureaucracy 50, 3179 YOLD", null, null), - 'display conflux' => array('15072013', "It's Sweetmorn, the 50th of Confusion, 3179. \nCelebrate Conflux", "It's %{%A, the %e of %B%}, %Y. %N%nCelebrate %H", ""), - 'standard holydays turned off' => array('15072013', "It's Sweetmorn, the 50th of Confusion, 3179.", "It's %{%A, the %e of %B%}, %Y.%N%nCelebrate %H %1", ""), - 'display conflux German' => array('15072013', "Heute ist Süßmorgen, der 50. der Unordnung, 3179. \nFeiert Unfluss", "Heute ist %{%A, der %e der %B%}, %Y. %N%nFeiert %H", "de"), - 'standard holydays turned off German' => array('15072013', "Heute ist Süßmorgen, der 50. der Unordnung, 3179.", "%1Heute ist %{%A, der %e der %B%}, %Y.%N%nFeiert %H", "de"), - ); + return [ + 'no format' => ['26092013', "Prickle-Prickle, Bureaucracy 50, 3179 YOLD", null, null], + 'display conflux' => ['15072013', "It's Sweetmorn, the 50th of Confusion, 3179. \nCelebrate Conflux", "It's %{%A, the %e of %B%}, %Y. %N%nCelebrate %H", ""], + 'standard holydays turned off' => ['15072013', "It's Sweetmorn, the 50th of Confusion, 3179.", "It's %{%A, the %e of %B%}, %Y.%N%nCelebrate %H %1", ""], + 'display conflux German' => ['15072013', "Heute ist Süßmorgen, der 50. der Unordnung, 3179. \nFeiert Unfluss", "Heute ist %{%A, der %e der %B%}, %Y. %N%nFeiert %H", "de"], + 'standard holydays turned off German' => ['15072013', "Heute ist Süßmorgen, der 50. der Unordnung, 3179.", "%1Heute ist %{%A, der %e der %B%}, %Y.%N%nFeiert %H", "de"], + ]; } #[DataProvider('funFridayProvider')] - public function testFunFriday($gregorian, $discordian, $format) - { - self::assertEquals($discordian, $this->object->ddate($format, $gregorian)); + public function testFunFriday( + string $gregorian, + string $discordian, + string $format, + ): void { + self::assertEquals($discordian, (new Ddate())->ddate($format, $gregorian)); } - public static function funFridayProvider() + public static function funFridayProvider(): array { - return array( - array('31032017',"It's Setting Orange, the 17th of Discord, 3183.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('30062017',"It's Sweetmorn, the 35th of Confusion, 3183.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('29092017',"It's Boomtime, the 53rd of Bureaucracy, 3183.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('29122017',"It's Pungenday, the 71st of The Aftermath, 3183.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('29032013',"It's Pungenday, the 15th of Discord, 3179.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('31052013',"It's Sweetmorn, the 5th of Confusion, 3179.\nCelebrate Syaday and Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('30082013',"It's Boomtime, the 23rd of Bureaucracy, 3179.\nCelebrate Fun Friday and St. Scrivener’s Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('29112013',"It's Pungenday, the 41st of The Aftermath, 3179.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('31032000',"It's Setting Orange, the 17th of Discord, 3166.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('30062000',"It's Sweetmorn, the 35th of Confusion, 3166.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('29092000',"It's Boomtime, the 53rd of Bureaucracy, 3166.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('29122000',"It's Pungenday, the 71st of The Aftermath, 3166.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('31012020',"It's Sweetmorn, the 31st of Chaos, 3186.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('29052020',"It's Prickle-Prickle, the 3rd of Confusion, 3186.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('31072020',"It's Boomtime, the 66th of Confusion, 3186.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('30102020',"It's Pungenday, the 11th of The Aftermath, 3186.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - ); + return [ + ['31032017',"It's Setting Orange, the 17th of Discord, 3183.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['30062017',"It's Sweetmorn, the 35th of Confusion, 3183.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['29092017',"It's Boomtime, the 53rd of Bureaucracy, 3183.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['29122017',"It's Pungenday, the 71st of The Aftermath, 3183.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['29032013',"It's Pungenday, the 15th of Discord, 3179.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['31052013',"It's Sweetmorn, the 5th of Confusion, 3179.\nCelebrate Syaday and Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['30082013',"It's Boomtime, the 23rd of Bureaucracy, 3179.\nCelebrate Fun Friday and St. Scrivener’s Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['29112013',"It's Pungenday, the 41st of The Aftermath, 3179.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['31032000',"It's Setting Orange, the 17th of Discord, 3166.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['30062000',"It's Sweetmorn, the 35th of Confusion, 3166.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['29092000',"It's Boomtime, the 53rd of Bureaucracy, 3166.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['29122000',"It's Pungenday, the 71st of The Aftermath, 3166.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['31012020',"It's Sweetmorn, the 31st of Chaos, 3186.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['29052020',"It's Prickle-Prickle, the 3rd of Confusion, 3186.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['31072020',"It's Boomtime, the 66th of Confusion, 3186.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['30102020',"It's Pungenday, the 11th of The Aftermath, 3186.\nCelebrate Fun Friday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ]; } #[DataProvider('camdenBenaresHolidaysProvider')] - public function testCamdenBenaresHolidays($gregorian, $discordian, $format) - { - self::assertEquals($discordian, $this->object->ddate($format, $gregorian)); + public function testCamdenBenaresHolidays( + string $gregorian, + string $discordian, + string $format, + ): void { + self::assertEquals($discordian, (new Ddate())->ddate($format, $gregorian)); } - public static function camdenBenaresHolidaysProvider() + public static function camdenBenaresHolidaysProvider(): array { - return array( - array('01011997', "It's Sweetmorn, the 1st of Chaos, 3163.\nCelebrate Bogey's Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('15011915', "It's Setting Orange, the 15th of Chaos, 3081.\nCelebrate St. Afrodite's Day", "%2It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('23011928', "It's Pungenday, the 23rd of Chaos, 3094.\nCelebrate St. Bobcat's Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('03031999', "It's Boomtime, the 62nd of Chaos, 3165.\nCelebrate Pass Day", "It's %{%A, the %e of %B%}, %Y.%N%nCelebrate%2 %H"), - array('04043000', "It's Prickle-Prickle, the 21st of Discord, 4166.\nCelebrate Square Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('01052016', "It's Sweetmorn, the 48th of Discord, 3182.\nCelebrate Adam Weishaupt’s Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('23052000', "It's Pungenday, the 70th of Discord, 3166.\nCelebrate Buddha’s Birthday", "It%2's %{%A, the %e of %B%}, %Y.%N%nCelebrate %H"), - array('05062020', "It's Sweetmorn, the 10th of Confusion, 3186.\nCelebrate Golden Apple Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('14072019', "It's Setting Orange, the 49th of Confusion, 3185.\nCelebrate St. Merde’s Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('23082018', "It's Setting Orange, the 16th of Bureaucracy, 3184.\nCelebrate Nancy Fancymanner’s Birthday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - array('17092017', "It's Setting Orange, the 41st of Bureaucracy, 3183.\nCelebrate Emperor Norton’s Day", "It's %{%A, %2the %e of %B%}, %Y.%N%nCelebrate %H"), - array('30112016', "It's Prickle-Prickle, the 42nd of The Aftermath, 3182.\nCelebrate Early Lunch Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"), - ); + return [ + ['01011997', "It's Sweetmorn, the 1st of Chaos, 3163.\nCelebrate Bogey's Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['15011915', "It's Setting Orange, the 15th of Chaos, 3081.\nCelebrate St. Afrodite's Day", "%2It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['23011928', "It's Pungenday, the 23rd of Chaos, 3094.\nCelebrate St. Bobcat's Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['03031999', "It's Boomtime, the 62nd of Chaos, 3165.\nCelebrate Pass Day", "It's %{%A, the %e of %B%}, %Y.%N%nCelebrate%2 %H"], + ['04043000', "It's Prickle-Prickle, the 21st of Discord, 4166.\nCelebrate Square Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['01052016', "It's Sweetmorn, the 48th of Discord, 3182.\nCelebrate Adam Weishaupt’s Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['23052000', "It's Pungenday, the 70th of Discord, 3166.\nCelebrate Buddha’s Birthday", "It%2's %{%A, the %e of %B%}, %Y.%N%nCelebrate %H"], + ['05062020', "It's Sweetmorn, the 10th of Confusion, 3186.\nCelebrate Golden Apple Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['14072019', "It's Setting Orange, the 49th of Confusion, 3185.\nCelebrate St. Merde’s Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['23082018', "It's Setting Orange, the 16th of Bureaucracy, 3184.\nCelebrate Nancy Fancymanner’s Birthday", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ['17092017', "It's Setting Orange, the 41st of Bureaucracy, 3183.\nCelebrate Emperor Norton’s Day", "It's %{%A, %2the %e of %B%}, %Y.%N%nCelebrate %H"], + ['30112016', "It's Prickle-Prickle, the 42nd of The Aftermath, 3182.\nCelebrate Early Lunch Day", "It's %{%A, the %e of %B%},%2 %Y.%N%nCelebrate %H"], + ]; } #[DataProvider('funFridayDeProvider')] - public function testFunFridayDe($gregorian, $discordian, $format) - { - self::assertEquals($discordian, $this->object->ddate($format, $gregorian, 'de')); + public function testFunFridayDe( + string $gregorian, + string $discordian, + string $format): void { + self::assertEquals($discordian, (new Ddate())->ddate($format, $gregorian, 'de')); } - public static function funFridayDeProvider() + public static function funFridayDeProvider(): array { - return array( - array('30032323', "Heute ist Prickel-Prickel, 16. der Zweitracht 3489, heute ist: Vergnügungsfreitag.", "Heute ist %{%A, %e %C %Y%}%2%N, heute ist: %H."), - array('31082323', "Heute ist Stichtag, 24. der Beamtenherrschaft 3489, heute ist: Vergnügungsfreitag.", "Heute ist %{%A, %e %C %Y%}%2%N, heute ist: %H."), - array('30111990', "Heute ist Prickel-Prickel, 42. des Grummets 3156, heute ist: Vergnügungsfreitag und Tag des frühen Mittagessens.", "Heute ist %{%A, %e %C %Y%}%2%N, heute ist: %H."), - ); + return [ + ['30032323', "Heute ist Prickel-Prickel, 16. der Zweitracht 3489, heute ist: Vergnügungsfreitag.", "Heute ist %{%A, %e %C %Y%}%2%N, heute ist: %H."], + ['31082323', "Heute ist Stichtag, 24. der Beamtenherrschaft 3489, heute ist: Vergnügungsfreitag.", "Heute ist %{%A, %e %C %Y%}%2%N, heute ist: %H."], + ['30111990', "Heute ist Prickel-Prickel, 42. des Grummets 3156, heute ist: Vergnügungsfreitag und Tag des frühen Mittagessens.", "Heute ist %{%A, %e %C %Y%}%2%N, heute ist: %H."], + ]; } #[DataProvider('revDrJonSwabeysWhollydaysProvider')] - public function testRevDrJonSwabeysWhollydays($gregorian, $discordian, $format, $locale) - { - self::assertEquals($discordian, $this->object->ddate($format, $gregorian, $locale)); + public function testRevDrJonSwabeysWhollydays( + string $gregorian, + string $discordian, + string $format, + string $locale, + ): void { + self::assertEquals($discordian, (new Ddate())->ddate($format, $gregorian, $locale)); } - public static function revDrJonSwabeysWhollydaysProvider() + public static function revDrJonSwabeysWhollydaysProvider(): array { - return array( - array('27012017', "Heute ist Blütezeit, 27. der Verwirrung 3183, heute ist: Faultiertag.", "Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'), - array('27012017', "Heute ist Blütezeit, 27. der Verwirrung 3183, heute ist: Faultiertag.", "Heute ist %{%A, %e %C %Y%}%3%1%N, heute ist: %H.", 'de'), - array('07082017', "Heute ist Prickel-Prickel, 73. der Unordnung 3183, heute ist: Tag des Auges.", "Heute ist %{%A, %e %C %Y%}%3%1%N, heute ist: %H.", 'de'), - array('31052013', "It's Sweetmorn, the 5th of Confusion, 3179.\nCelebrate Syaday and Fun Friday", "It's %{%A, the %e of %B%},%1%2%3 %Y.%N%nCelebrate %H", 'en'), - array('03052013', "It's Pungenday, the 50th of Discord, 3179.\nCelebrate Discoflux", "It's %{%A, the %e of %B%},%1%2%3 %Y.%N%nCelebrate %H", 'en'), - array('31122004', "It's Setting Orange, the 73rd of The Aftermath, 3170.\nCelebrate Fun Friday and Eye Day", "It's %{%A, the %e of %B%},%1%2%3 %Y.%N%nCelebrate %H", 'en'), - array('03092017', "It's Sweetmorn, the 27th of Bureaucracy, 3183. Celebrate Slothday", "It's %{%A, the %e of %B%},%1%2%3 %Y.%N Celebrate %H", 'en'), - ); + return [ + ['27012017', "Heute ist Blütezeit, 27. der Verwirrung 3183, heute ist: Faultiertag.", "Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'], + ['27012017', "Heute ist Blütezeit, 27. der Verwirrung 3183, heute ist: Faultiertag.", "Heute ist %{%A, %e %C %Y%}%3%1%N, heute ist: %H.", 'de'], + ['07082017', "Heute ist Prickel-Prickel, 73. der Unordnung 3183, heute ist: Tag des Auges.", "Heute ist %{%A, %e %C %Y%}%3%1%N, heute ist: %H.", 'de'], + ['31052013', "It's Sweetmorn, the 5th of Confusion, 3179.\nCelebrate Syaday and Fun Friday", "It's %{%A, the %e of %B%},%1%2%3 %Y.%N%nCelebrate %H", 'en'], + ['03052013', "It's Pungenday, the 50th of Discord, 3179.\nCelebrate Discoflux", "It's %{%A, the %e of %B%},%1%2%3 %Y.%N%nCelebrate %H", 'en'], + ['31122004', "It's Setting Orange, the 73rd of The Aftermath, 3170.\nCelebrate Fun Friday and Eye Day", "It's %{%A, the %e of %B%},%1%2%3 %Y.%N%nCelebrate %H", 'en'], + ['03092017', "It's Sweetmorn, the 27th of Bureaucracy, 3183. Celebrate Slothday", "It's %{%A, the %e of %B%},%1%2%3 %Y.%N Celebrate %H", 'en'], + ]; } #[DataProvider('revLoveshadesWhollydaysProvider')] - public function testRevLoveshadesWhollydays($gregorian, $discordian, $format, $locale) - { - self::assertEquals($discordian, $this->object->ddate($format, $gregorian, $locale)); + public function testRevLoveshadesWhollydays( + string $gregorian, + string $discordian, + string $format, + string $locale, + ): void { + self::assertEquals($discordian, (new Ddate())->ddate($format, $gregorian, $locale)); } - public static function revLoveshadesWhollydaysProvider() + public static function revLoveshadesWhollydaysProvider(): array { - return array( - array('01011995', "It's Sweetmorn, the 1st of Chaos, 3161. Celebrate Bogey's Day and Nude Year's Day", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N Celebrate %H", 'en'), - array('10011996', "It's Setting Orange, the 10th of Chaos, 3162. Celebrate Backwards Day (Reformed) and Binary Day", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N Celebrate %H", 'en'), - array('30031997', "It's Prickle-Prickle, the 16th of Discord, 3163.\nCelebrate Erister", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N%nCelebrate %H", 'en'), - array('23042000', "It's Pungenday, the 40th of Discord, 3166.\nCelebrate Erister", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N%nCelebrate %H", 'en'), - array('16042006', "It's Sweetmorn, the 33rd of Discord, 3172.\nCelebrate Erister", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N%nCelebrate %H", 'en'), - array('05042015', "It's Setting Orange, the 22nd of Discord, 3181.\nCelebrate Be Kind To Tourists Day and Erister", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N%nCelebrate %H", 'en'), - array('31052013', "It's Sweetmorn, the 5th of Confusion, 3179.\nCelebrate Syaday, Fun Friday and Gulikday/Fearless Fred Day", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N%nCelebrate %H", 'en'), - array('30122017', "Heute ist Prickel-Prickel, 72. des Grummets 3183, heute ist: Vorabend des Neujahrsabends.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'), - array('10011901', "Heute ist Orangewerdend, 10. der Verwirrung 3067, heute ist: Rückwärtstag (reformiert) und Binärtag.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'), - array('07041901', "Heute ist Blütezeit, 24. der Zweitracht 3067, heute ist: Eristern.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'), - array('27082017', "Today is Prickle-Prickle, 20th Bureaucracy 3183, celebrate: Go Topless Day.", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'), - array('27082017', "Heute ist Prickel-Prickel, 20. der Beamtenherrschaft 3183, heute ist: Obenohnetag.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'), - array('26082018', "Heute ist Stichtag, 19. der Beamtenherrschaft 3184, heute ist: Obenohnetag.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'), - array('23082026', "Today is Setting Orange, 16th Bureaucracy 3192, celebrate: Go Topless Day.", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'), - array('23082027', "Today is Setting Orange, 16th Bureaucracy 3193", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'), - array('30082026', "Today is Boomtime, 23rd Bureaucracy 3192", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'), - array('16082026', "Today is Pungenday, 9th Bureaucracy 3192", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'), - array('06051910', "Today is Sweetmorn, 53rd Discord 3076, celebrate: No Pants Day.", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'), - array('03051912', "Today is Pungenday, 50th Discord 3078, celebrate: Discoflux and No Pants Day.", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'), - array('03052041', "Heute ist Stichtag, 50. der Zweitracht 3207, heute ist: Zweifluss und Untenohnetag.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'), - array('03052042', "Heute ist Stichtag, 50. der Zweitracht 3208, heute ist: Zweifluss.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'), - ); + return [ + ['01011995', "It's Sweetmorn, the 1st of Chaos, 3161. Celebrate Bogey's Day and Nude Year's Day", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N Celebrate %H", 'en'], + ['10011996', "It's Setting Orange, the 10th of Chaos, 3162. Celebrate Backwards Day (Reformed) and Binary Day", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N Celebrate %H", 'en'], + ['30031997', "It's Prickle-Prickle, the 16th of Discord, 3163.\nCelebrate Erister", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N%nCelebrate %H", 'en'], + ['23042000', "It's Pungenday, the 40th of Discord, 3166.\nCelebrate Erister", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N%nCelebrate %H", 'en'], + ['16042006', "It's Sweetmorn, the 33rd of Discord, 3172.\nCelebrate Erister", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N%nCelebrate %H", 'en'], + ['05042015', "It's Setting Orange, the 22nd of Discord, 3181.\nCelebrate Be Kind To Tourists Day and Erister", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N%nCelebrate %H", 'en'], + ['31052013', "It's Sweetmorn, the 5th of Confusion, 3179.\nCelebrate Syaday, Fun Friday and Gulikday/Fearless Fred Day", "It's %{%A, the %e of %B%},%1%2%3%4 %Y.%N%nCelebrate %H", 'en'], + ['30122017', "Heute ist Prickel-Prickel, 72. des Grummets 3183, heute ist: Vorabend des Neujahrsabends.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'], + ['10011901', "Heute ist Orangewerdend, 10. der Verwirrung 3067, heute ist: Rückwärtstag (reformiert) und Binärtag.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'], + ['07041901', "Heute ist Blütezeit, 24. der Zweitracht 3067, heute ist: Eristern.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'], + ['27082017', "Today is Prickle-Prickle, 20th Bureaucracy 3183, celebrate: Go Topless Day.", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'], + ['27082017', "Heute ist Prickel-Prickel, 20. der Beamtenherrschaft 3183, heute ist: Obenohnetag.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'], + ['26082018', "Heute ist Stichtag, 19. der Beamtenherrschaft 3184, heute ist: Obenohnetag.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'], + ['23082026', "Today is Setting Orange, 16th Bureaucracy 3192, celebrate: Go Topless Day.", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'], + ['23082027', "Today is Setting Orange, 16th Bureaucracy 3193", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'], + ['30082026', "Today is Boomtime, 23rd Bureaucracy 3192", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'], + ['16082026', "Today is Pungenday, 9th Bureaucracy 3192", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'], + ['06051910', "Today is Sweetmorn, 53rd Discord 3076, celebrate: No Pants Day.", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'], + ['03051912', "Today is Pungenday, 50th Discord 3078, celebrate: Discoflux and No Pants Day.", "%4Today is %{%A, %e %B %Y%}%3%N, celebrate: %H.", 'en'], + ['03052041', "Heute ist Stichtag, 50. der Zweitracht 3207, heute ist: Zweifluss und Untenohnetag.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'], + ['03052042', "Heute ist Stichtag, 50. der Zweitracht 3208, heute ist: Zweifluss.", "%4Heute ist %{%A, %e %C %Y%}%3%N, heute ist: %H.", 'de'], + ]; } } diff --git a/src/EmperorNortonCommands/tests/lib/DdateLocalizedTest.php b/src/EmperorNortonCommands/tests/lib/DdateLocalizedTest.php index 2f5a512..35d4c60 100644 --- a/src/EmperorNortonCommands/tests/lib/DdateLocalizedTest.php +++ b/src/EmperorNortonCommands/tests/lib/DdateLocalizedTest.php @@ -13,81 +13,57 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -/** - * Class DdateLocalizedTest. - * - * @package EmperorNortonCommands\tests\lib - */ -class DdateLocalizedTest extends TestCase +final class DdateLocalizedTest extends TestCase { - /** - * Object instance to be tested. - * - * @var Ddate - */ - protected $_object; - - /** - * Set up. - */ - public function setUp(): void - { - $this->_object = new Ddate(); - } - - /** - * Data Provider to test ddate() method. - * - * @return array - */ - public static function dataProvider() - { - return array( - array('03051998', 'Pungenday, Discord 50, 3164 YOLD', null, 'en'), - array('29021996', 'St. Tibs Tag 3162 n. Gre.', null, 'de'), - array('07021974', 'Stichtag, Verwirrung 38, 3140 n. Gre.', null, 'de'), - array('03012013', 'ST, Verwirrung 3, 3179', '%{%a, %B %d,%} %Y', 'de'), - array('08122014', 'Heute ist Blütezeit, der Fünfzigster Grummet 3180 n. Gre.' . "\n\t" . 'Wir feiern Ausfluss.', 'Heute ist %{%A, der %E %B %Y n. Gre.%N%n%tWir feiern %H.', 'de'), - array('14031999', 'ST, Ve Dreiundsiebzigster 3165', '%a, %b %E %Y', 'de'), - array('29022012', "Heute ist St. Tibs Tag 3178, noch 2428624 Tage bis zum Tag X", "Heute ist %{%A, the %E of %B,%} %Y, noch %X Tage bis zum Tag X", 'de'), - array('28022016', 'Heute ist Prickel-Prickel, Neunundfünfzigster d. Verwirrung im JUHD 3182.', 'Heute ist %{%A, %E d. %B%} im JUHD %Y.%N Wir feiern %H.', 'de'), - array('28022016', 'Heute ist Prickel-Prickel, Neunundfünfzigster der Verwirrung im JUHD 3182.', 'Heute ist %{%A, %E %C%} im JUHD %Y.%N Wir feiern %H.', 'de'), - array('05122015', 'Heute ist Prickel-Prickel, Siebenundvierzigster des Grummets im JUHD 3181.', 'Heute ist %{%A, %E %C%} im JUHD %Y.%N Wir feiern %H.', 'de'), - array('08122015', 'Heute ist Blütezeit, Fünfzigster des Grummets im JUHD 3181. Wir feiern Ausfluss.', 'Heute ist %{%A, %E %C%} im JUHD %Y.%N Wir feiern %H.', 'de'), - array('05122015', 'Heute ist Prickel-Prickel, 47. des Grummets im JUHD 3181.', 'Heute ist %{%A, %e %C%} im JUHD %Y.%N Wir feiern %H.', 'de'), - array('11112015', 'Heute ist Orangewerdend, 23. des Grummets im JUHD 3181.', 'Heute ist %{%A, %e %C%} im JUHD %Y.%N Wir feiern %H.', 'de'), - array('26092015', 'Heute ist Prickel-Prickel, 50. der Beamtenherrschaft im JUHD 3181. Wir feiern Beamtenfluss.', 'Heute ist %{%A, %e %C%} im JUHD %Y.%N Wir feiern %H.', 'de'), - array('20102015', 'Heute ist Stichtag, 1. des Grummets im JUHD 3181.', 'Heute ist %{%A, %e %C%} im JUHD %Y.%N Wir feiern %H.', 'de'), - ); - } - - /** - * Test ddate() with locale. - */ #[DataProvider('dataProvider')] - public function testDdateLocalized($gregorian, $discordian, $format, $locale) - { + public function testDdateLocalized( + string $gregorian, + string $discordian, + string|null $format, + string $locale, + ): void { $expected = $discordian; - $actual = $this->_object->ddate($format, $gregorian, $locale); + $actual = (new Ddate())->ddate($format, $gregorian, $locale); self::assertEquals($expected, $actual); } + public static function dataProvider(): array + { + return [ + ['03051998', 'Pungenday, Discord 50, 3164 YOLD', null, 'en'], + ['29021996', 'St. Tibs Tag 3162 n. Gre.', null, 'de'], + ['07021974', 'Stichtag, Verwirrung 38, 3140 n. Gre.', null, 'de'], + ['03012013', 'ST, Verwirrung 3, 3179', '%{%a, %B %d,%} %Y', 'de'], + ['08122014', 'Heute ist Blütezeit, der Fünfzigster Grummet 3180 n. Gre.' . "\n\t" . 'Wir feiern Ausfluss.', 'Heute ist %{%A, der %E %B %Y n. Gre.%N%n%tWir feiern %H.', 'de'], + ['14031999', 'ST, Ve Dreiundsiebzigster 3165', '%a, %b %E %Y', 'de'], + ['29022012', "Heute ist St. Tibs Tag 3178, noch 2428624 Tage bis zum Tag X", "Heute ist %{%A, the %E of %B,%} %Y, noch %X Tage bis zum Tag X", 'de'], + ['28022016', 'Heute ist Prickel-Prickel, Neunundfünfzigster d. Verwirrung im JUHD 3182.', 'Heute ist %{%A, %E d. %B%} im JUHD %Y.%N Wir feiern %H.', 'de'], + ['28022016', 'Heute ist Prickel-Prickel, Neunundfünfzigster der Verwirrung im JUHD 3182.', 'Heute ist %{%A, %E %C%} im JUHD %Y.%N Wir feiern %H.', 'de'], + ['05122015', 'Heute ist Prickel-Prickel, Siebenundvierzigster des Grummets im JUHD 3181.', 'Heute ist %{%A, %E %C%} im JUHD %Y.%N Wir feiern %H.', 'de'], + ['08122015', 'Heute ist Blütezeit, Fünfzigster des Grummets im JUHD 3181. Wir feiern Ausfluss.', 'Heute ist %{%A, %E %C%} im JUHD %Y.%N Wir feiern %H.', 'de'], + ['05122015', 'Heute ist Prickel-Prickel, 47. des Grummets im JUHD 3181.', 'Heute ist %{%A, %e %C%} im JUHD %Y.%N Wir feiern %H.', 'de'], + ['11112015', 'Heute ist Orangewerdend, 23. des Grummets im JUHD 3181.', 'Heute ist %{%A, %e %C%} im JUHD %Y.%N Wir feiern %H.', 'de'], + ['26092015', 'Heute ist Prickel-Prickel, 50. der Beamtenherrschaft im JUHD 3181. Wir feiern Beamtenfluss.', 'Heute ist %{%A, %e %C%} im JUHD %Y.%N Wir feiern %H.', 'de'], + ['20102015', 'Heute ist Stichtag, 1. des Grummets im JUHD 3181.', 'Heute ist %{%A, %e %C%} im JUHD %Y.%N Wir feiern %H.', 'de'], + ]; + } + /** * Test makes sure that output with locale 'en' matches default output. */ - public function testLocaleEn() + public function testLocaleEn(): void { - self::assertEquals($this->_object->ddate(), $this->_object->ddate(null, null, 'en')); + self::assertEquals((new Ddate())->ddate(), (new Ddate())->ddate(null, null, 'en')); } /** * Test getSupportedFormatFields('de'). */ - public function testGetSupportedFormatFieldsLocaleDe() + public function testGetSupportedFormatFieldsLocaleDe(): void { $formatter = new GermanStandardFormatter(); $expected = $formatter->getSupportedFormatStringFields(); - $actual = $this->_object->getSupportedFormatStringFields('de'); + $actual = (new Ddate())->getSupportedFormatStringFields('de'); self::assertEquals($expected, $actual); } @@ -96,11 +72,11 @@ public function testGetSupportedFormatFieldsLocaleDe() * * Unsupported locale, should default to English standard formatter. */ - public function testGetSupportedFormatFieldsLocaleZz() + public function testGetSupportedFormatFieldsLocaleZz(): void { $formatter = new EnglishStandardFormatter(); $expected = $formatter->getSupportedFormatStringFields(); - $actual = $this->_object->getSupportedFormatStringFields('zz'); + $actual = (new Ddate())->getSupportedFormatStringFields('zz'); self::assertEquals($expected, $actual); } } diff --git a/src/EmperorNortonCommands/tests/lib/DdateTest.php b/src/EmperorNortonCommands/tests/lib/DdateTest.php index 1948b30..2ebddb8 100644 --- a/src/EmperorNortonCommands/tests/lib/DdateTest.php +++ b/src/EmperorNortonCommands/tests/lib/DdateTest.php @@ -12,156 +12,131 @@ use EmperorNortonCommands\lib\Converter; use EmperorNortonCommands\lib\locale\en\StandardFormatter as EnglishStandardFormatter; use EmperorNortonCommands\lib\FormatterFactory; +use InvalidArgumentException; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use SimpleXMLElement; -/** - * Class DdateTest. - * - * @package EmperorNortonCommands - */ -class DdateTest extends TestCase +final class DdateTest extends TestCase { - /** - * Object instance to be tested. - * - * @var Ddate - */ - protected $_object; - - /** - * Set up. - */ - public function setUp(): void - { - $this->_object = new Ddate(); + #[DataProvider('ddateDataProvider')] + public function testDdate( + string|int $gregorian, + string $discordian, + object|string|null $format, + ): void { + $actual = (new Ddate())->ddate($format, $gregorian); + self::assertEquals($discordian, $actual); } - /** - * Data Provider to test ddate() method. - * - * @return array - */ - public static function ddateDataProvider() + public static function ddateDataProvider(): array { - return array( - array('03051998', 'Pungenday, Discord 50, 3164 YOLD', null), - array('29021996', 'St. Tib\'s Day 3162 YOLD', null), - array('07021974', 'Pungenday, Chaos 38, 3140 YOLD', null), - array('31011973', 'Sweetmorn, Chaos 31, 3139 YOLD', null), - array('16022008', 'Boomtime, Chaos 47, 3174 YOLD', null), - array('24011948', 'Prickle-Prickle, Chaos 24, 3114 YOLD', null), - array('25091944', 'Pungenday, Bureaucracy 49, 3110 YOLD', null), - array('04091920', 'Boomtime, Bureaucracy 28, 3086 YOLD', null), - array('20091928', 'Pungenday, Bureaucracy 44, 3094 YOLD', null), - array('30011901', 'Setting Orange, Chaos 30, 3067 YOLD', null), - array('29022012', 'St. Tib\'s Day 3178 YOLD', null), - array('01032012', 'Setting Orange, Chaos 60, 3178 YOLD', null), - array('17091859', 'Setting Orange, Bureaucracy 41, 3025 YOLD', null), - array('12122012', 'Sweetmorn, The Aftermath 54, 3178 YOLD', null), - array('07072007', 'Pungenday, Confusion 42, 3173 YOLD', null), - array('29022012', "Today's St. Tib's Day of FNORD, 3178 YOLD", "Today's %{%A, the %e%} of %B, %Y YOLD"), - array('29022012', "Today's St. Tib's Day FNORD (FNORD), the FNORD (FNORD) of FNORD (FNORD), 3178 YOLD", "Today's %{%} %A (%a), the %e (%d) of %B (%b), %Y YOLD"), - array('01012013', 'SM, Chaos 1, 3179', '%{%a, %B %d,%} %Y'), - array('02012013', 'BT, Chaos 2, 3179', '%{%a, %B %d,%} %Y'), - array('03012013', 'PD, Chaos 3, 3179', '%{%a, %B %d,%} %Y'), - array('04012013', 'PP, Chaos 4, 3179', '%{%a, %B %d,%} %Y'), - array('05012013', 'SO, Chaos 5, 3179', '%{%a, %B %d,%} %Y'), - array('05012014', 'Today is Setting Orange, the 5th of Chaos, 3180 YOLD' . "\n\t" . 'We celebrate Mungday.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'), - array('06012014', 'Today is Sweetmorn, the 6th of Chaos, 3180 YOLD', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'), - array('19022014', 'Today is Setting Orange, the 50th of Chaos, 3180 YOLD' . "\n\t" . 'We celebrate Chaoflux.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'), - array('19032014', 'Today is Pungenday, the 5th of Discord, 3180 YOLD' . "\n\t" . 'We celebrate Mojoday.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'), - array('03052014', 'Today is Pungenday, the 50th of Discord, 3180 YOLD' . "\n\t" . 'We celebrate Discoflux.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'), - array('31052014', 'Today is Sweetmorn, the 5th of Confusion, 3180 YOLD' . "\n\t" . 'We celebrate Syaday.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'), - array('15072014', 'Today is Sweetmorn, the 50th of Confusion, 3180 YOLD' . "\n\t" . 'We celebrate Conflux.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'), - array('12082014', 'Today is Prickle-Prickle, the 5th of Bureaucracy, 3180 YOLD' . "\n\t" . 'We celebrate Zaraday.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'), - array('26092014', 'Today is Prickle-Prickle, the 50th of Bureaucracy, 3180 YOLD' . "\n\t" . 'We celebrate Bureflux.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'), - array('24102014', 'Today is Boomtime, the 5th of The Aftermath, 3180 YOLD' . "\n\t" . 'We celebrate Maladay.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'), - array('08122014', 'Today is Boomtime, the 50th of The Aftermath, 3180 YOLD' . "\n\t" . 'We celebrate Afflux.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'), - array('19101999', 'Boomtime, Bureaucracy 73, 3165 YOLD', null), - array('13032000', 'Boomtime, Chaos 72, 3166 YOLD', null), - array('14032000', 'Pungenday, Chaos 73, 3166 YOLD', null), - array('15032000', 'Prickle-Prickle, Discord 1, 3166 YOLD', null), - array('30121999', 'Prickle-Prickle, The Aftermath 72, 3165 YOLD', new \stdClass()), - array('31121999', 'Setting Orange, The Aftermath 73, 3165 YOLD', new \stdClass()), - array('01012000', '', new SimpleXMLElement('')), - array('14031999', 'PD, Chs 73rd 3165', '%a, %b %e %Y'), - array('15031999', 'PP, Dsc 1st 3165', '%a, %b %e %Y'), - array('26051999', 'SM, Dsc 73rd 3165', '%a, %b %e %Y'), - array('27051999', 'BT, Cfn 1st 3165', '%a, %b %e %Y'), - array('07081999', 'PP, Cfn 73rd 3165', '%a, %b %e %Y'), - array('08081999', 'SO, Bcy 1st 3165', '%a, %b %e %Y'), - array('19101999', 'BT, Bcy 73rd 3165', '%a, %b %e %Y'), - array('20101999', 'PD, Afm 1st 3165', '%a, %b %e %Y'), - array('31011999', 'SM, Chs 31st 3165', '%a, %b %e %Y'), - array('01012000', 'SM, Chs 1st 3166', '%a, %b %e %Y'), - array('02012000', 'BT, Chs 2nd 3166', '%a, %b %e %Y'), - array('03012000', 'PD, Chs 3rd 3166', '%a, %b %e %Y'), - array('04012000', 'PP, Chs 4th 3166', '%a, %b %e %Y'), - array('05012000', 'SO, Chs 5th 3166', '%a, %b %e %Y'), - array('10012000', 'SO, Chs 10th 3166', '%a, %b %e %Y'), - array('11012000', 'SM, Chs 11th 3166', '%a, %b %e %Y'), - array('12012000', 'BT, Chs 12th 3166', '%a, %b %e %Y'), - array('13012000', 'PD, Chs 13th 3166', '%a, %b %e %Y'), - array('14012000', 'PP, Chs 14th 3166', '%a, %b %e %Y'), - array('14032000', 'PD, Chs 73rd 3166', '%a, %b %e %Y'), - array('15032000', 'PP, Dsc 1st 3166', '%a, %b %e %Y'), - array('26052000', 'SM, Dsc 73rd 3166', '%a, %b %e %Y'), - array('27052000', 'BT, Cfn 1st 3166', '%a, %b %e %Y'), - array('07082000', 'PP, Cfn 73rd 3166', '%a, %b %e %Y'), - array('08082000', 'SO, Bcy 1st 3166', '%a, %b %e %Y'), - array('19102000', 'BT, Bcy 73rd 3166', '%a, %b %e %Y'), - array('20102000', 'PD, Afm 1st 3166', '%a, %b %e %Y'), - array('31012000', 'SM, Chs 31st 3166', '%a, %b %e %Y'), - array('01012001', 'SM, Chs 1st 3167', '%a, %b %e %Y'), - array('29022012', "Today's St. Tib's Day 3178 YOLD 2428624 days 'til X-Day", "Today's %{%A, the %e of %B,%} %Y YOLD %X days 'til X-Day"), - array(18092013, 'Today is Sweetmorn, the 42nd of Bureaucracy, 3179.', 'Today is %{%A, the %e of %B%}, %Y.%N %nCelebrate %H'), - array(26092013, "It's Prickle-Prickle, the 50th of Bureaucracy, 3179. \nCelebrate Bureflux", "It's %{%A, the %e of %B%}, %Y. %N%nCelebrate %H"), - array(29022016, "Today's St. Tib's Day, 3182.", "Today's %{%A, the %e of %B%}, %Y.%N Celebrate %H"), + return [ + ['03051998', 'Pungenday, Discord 50, 3164 YOLD', null], + ['29021996', 'St. Tib\'s Day 3162 YOLD', null], + ['07021974', 'Pungenday, Chaos 38, 3140 YOLD', null], + ['31011973', 'Sweetmorn, Chaos 31, 3139 YOLD', null], + ['16022008', 'Boomtime, Chaos 47, 3174 YOLD', null], + ['24011948', 'Prickle-Prickle, Chaos 24, 3114 YOLD', null], + ['25091944', 'Pungenday, Bureaucracy 49, 3110 YOLD', null], + ['04091920', 'Boomtime, Bureaucracy 28, 3086 YOLD', null], + ['20091928', 'Pungenday, Bureaucracy 44, 3094 YOLD', null], + ['30011901', 'Setting Orange, Chaos 30, 3067 YOLD', null], + ['29022012', 'St. Tib\'s Day 3178 YOLD', null], + ['01032012', 'Setting Orange, Chaos 60, 3178 YOLD', null], + ['17091859', 'Setting Orange, Bureaucracy 41, 3025 YOLD', null], + ['12122012', 'Sweetmorn, The Aftermath 54, 3178 YOLD', null], + ['07072007', 'Pungenday, Confusion 42, 3173 YOLD', null], + ['29022012', "Today's St. Tib's Day of FNORD, 3178 YOLD", "Today's %{%A, the %e%} of %B, %Y YOLD"], + ['29022012', "Today's St. Tib's Day FNORD (FNORD), the FNORD (FNORD) of FNORD (FNORD), 3178 YOLD", "Today's %{%} %A (%a), the %e (%d) of %B (%b), %Y YOLD"], + ['01012013', 'SM, Chaos 1, 3179', '%{%a, %B %d,%} %Y'], + ['02012013', 'BT, Chaos 2, 3179', '%{%a, %B %d,%} %Y'], + ['03012013', 'PD, Chaos 3, 3179', '%{%a, %B %d,%} %Y'], + ['04012013', 'PP, Chaos 4, 3179', '%{%a, %B %d,%} %Y'], + ['05012013', 'SO, Chaos 5, 3179', '%{%a, %B %d,%} %Y'], + ['05012014', 'Today is Setting Orange, the 5th of Chaos, 3180 YOLD' . "\n\t" . 'We celebrate Mungday.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'], + ['06012014', 'Today is Sweetmorn, the 6th of Chaos, 3180 YOLD', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'], + ['19022014', 'Today is Setting Orange, the 50th of Chaos, 3180 YOLD' . "\n\t" . 'We celebrate Chaoflux.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'], + ['19032014', 'Today is Pungenday, the 5th of Discord, 3180 YOLD' . "\n\t" . 'We celebrate Mojoday.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'], + ['03052014', 'Today is Pungenday, the 50th of Discord, 3180 YOLD' . "\n\t" . 'We celebrate Discoflux.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'], + ['31052014', 'Today is Sweetmorn, the 5th of Confusion, 3180 YOLD' . "\n\t" . 'We celebrate Syaday.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'], + ['15072014', 'Today is Sweetmorn, the 50th of Confusion, 3180 YOLD' . "\n\t" . 'We celebrate Conflux.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'], + ['12082014', 'Today is Prickle-Prickle, the 5th of Bureaucracy, 3180 YOLD' . "\n\t" . 'We celebrate Zaraday.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'], + ['26092014', 'Today is Prickle-Prickle, the 50th of Bureaucracy, 3180 YOLD' . "\n\t" . 'We celebrate Bureflux.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'], + ['24102014', 'Today is Boomtime, the 5th of The Aftermath, 3180 YOLD' . "\n\t" . 'We celebrate Maladay.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'], + ['08122014', 'Today is Boomtime, the 50th of The Aftermath, 3180 YOLD' . "\n\t" . 'We celebrate Afflux.', 'Today is %{%A, the %e of %B, %Y YOLD%N%n%tWe celebrate %H.'], + ['19101999', 'Boomtime, Bureaucracy 73, 3165 YOLD', null], + ['13032000', 'Boomtime, Chaos 72, 3166 YOLD', null], + ['14032000', 'Pungenday, Chaos 73, 3166 YOLD', null], + ['15032000', 'Prickle-Prickle, Discord 1, 3166 YOLD', null], + ['30121999', 'Prickle-Prickle, The Aftermath 72, 3165 YOLD', new \stdClass()], + ['31121999', 'Setting Orange, The Aftermath 73, 3165 YOLD', new \stdClass()], + ['01012000', '', new SimpleXMLElement('')], + ['14031999', 'PD, Chs 73rd 3165', '%a, %b %e %Y'], + ['15031999', 'PP, Dsc 1st 3165', '%a, %b %e %Y'], + ['26051999', 'SM, Dsc 73rd 3165', '%a, %b %e %Y'], + ['27051999', 'BT, Cfn 1st 3165', '%a, %b %e %Y'], + ['07081999', 'PP, Cfn 73rd 3165', '%a, %b %e %Y'], + ['08081999', 'SO, Bcy 1st 3165', '%a, %b %e %Y'], + ['19101999', 'BT, Bcy 73rd 3165', '%a, %b %e %Y'], + ['20101999', 'PD, Afm 1st 3165', '%a, %b %e %Y'], + ['31011999', 'SM, Chs 31st 3165', '%a, %b %e %Y'], + ['01012000', 'SM, Chs 1st 3166', '%a, %b %e %Y'], + ['02012000', 'BT, Chs 2nd 3166', '%a, %b %e %Y'], + ['03012000', 'PD, Chs 3rd 3166', '%a, %b %e %Y'], + ['04012000', 'PP, Chs 4th 3166', '%a, %b %e %Y'], + ['05012000', 'SO, Chs 5th 3166', '%a, %b %e %Y'], + ['10012000', 'SO, Chs 10th 3166', '%a, %b %e %Y'], + ['11012000', 'SM, Chs 11th 3166', '%a, %b %e %Y'], + ['12012000', 'BT, Chs 12th 3166', '%a, %b %e %Y'], + ['13012000', 'PD, Chs 13th 3166', '%a, %b %e %Y'], + ['14012000', 'PP, Chs 14th 3166', '%a, %b %e %Y'], + ['14032000', 'PD, Chs 73rd 3166', '%a, %b %e %Y'], + ['15032000', 'PP, Dsc 1st 3166', '%a, %b %e %Y'], + ['26052000', 'SM, Dsc 73rd 3166', '%a, %b %e %Y'], + ['27052000', 'BT, Cfn 1st 3166', '%a, %b %e %Y'], + ['07082000', 'PP, Cfn 73rd 3166', '%a, %b %e %Y'], + ['08082000', 'SO, Bcy 1st 3166', '%a, %b %e %Y'], + ['19102000', 'BT, Bcy 73rd 3166', '%a, %b %e %Y'], + ['20102000', 'PD, Afm 1st 3166', '%a, %b %e %Y'], + ['31012000', 'SM, Chs 31st 3166', '%a, %b %e %Y'], + ['01012001', 'SM, Chs 1st 3167', '%a, %b %e %Y'], + ['29022012', "Today's St. Tib's Day 3178 YOLD 2428624 days 'til X-Day", "Today's %{%A, the %e of %B,%} %Y YOLD %X days 'til X-Day"], + [18092013, 'Today is Sweetmorn, the 42nd of Bureaucracy, 3179.', 'Today is %{%A, the %e of %B%}, %Y.%N %nCelebrate %H'], + [26092013, "It's Prickle-Prickle, the 50th of Bureaucracy, 3179. \nCelebrate Bureflux", "It's %{%A, the %e of %B%}, %Y. %N%nCelebrate %H"], + [29022016, "Today's St. Tib's Day, 3182.", "Today's %{%A, the %e of %B%}, %Y.%N Celebrate %H"], // Although X-Day happens on 5th of July, it takes place 11 am UTC/ // 7 am EDT. Hence there is still one day (37 hours) diff and not // two days (48 hours) for July 7th 12am UTC). - array('05071998', "0 days 'til X-Day", "%x days 'til X-Day"), - array('07021974', "8914 days 'til X-Day", "%x days 'til X-Day"), - array('07071998', "-1 days 'til X-Day", "%x days 'til X-Day"), - array('27062016', "-6566 days 'til X-Day", "%x days 'til X-Day"), - array('05078661', "0 days 'til X-Day", "%X days 'til X-Day"), - array('07078661', "-1 days 'til X-Day", "%X days 'til X-Day") - ); - } - - /** - * Test ddate(). - */ - #[DataProvider('ddateDataProvider')] - public function testDdate($gregorian, $discordian, $format) - { - $expected = $discordian; - $actual = $this->_object->ddate($format, $gregorian); - self::assertEquals($expected, $actual); + ['05071998', "0 days 'til X-Day", "%x days 'til X-Day"], + ['07021974', "8914 days 'til X-Day", "%x days 'til X-Day"], + ['07071998', "-1 days 'til X-Day", "%x days 'til X-Day"], + ['27062016', "-6566 days 'til X-Day", "%x days 'til X-Day"], + ['05078661', "0 days 'til X-Day", "%X days 'til X-Day"], + ['07078661', "-1 days 'til X-Day", "%X days 'til X-Day"] + ]; } /** * Test ddate() with invalid argument (wrong type). */ - public function testInvalidDateWrongType() + public function testInvalidDateWrongType(): void { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Second argument expected to be a Gregorian date (dmY).'); - $this->_object->ddate(null, 'Lorem ipsum dolor sit amet.'); + (new Ddate())->ddate(null, 'Lorem ipsum dolor sit amet.'); } /** * Test ddate() with invalid argument (not a valid Gregorian date). */ - public function testInvalidDate() + public function testInvalidDate(): void { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Second argument expected to be a Gregorian date (dmY).'); - $this->_object->ddate(null, 29021997); + (new Ddate())->ddate(null, 29021997); } /** @@ -170,33 +145,33 @@ public function testInvalidDate() * Test default behaviour uses expected format and is the same as if * today's day month year were given. */ - public function testDdateNoArgs() + public function testDdateNoArgs(): void { $date = new DateTime(); - $actual = $this->_object->ddate(); - $expected = $this->_object->ddate('%{%A, %B %d,%} %Y YOLD', $date->format('dmY')); + $actual = (new Ddate())->ddate(); + $expected = (new Ddate())->ddate('%{%A, %B %d,%} %Y YOLD', $date->format('dmY')); self::assertEquals($expected, $actual); } /** * Test getSupportedFormatFields(). */ - public function testGetSupportedFormatFields() + public function testGetSupportedFormatFields(): void { $formatter = new EnglishStandardFormatter(); $expected = $formatter->getSupportedFormatStringFields(); - $actual = $this->_object->getSupportedFormatStringFields(); + $actual = (new Ddate())->getSupportedFormatStringFields(); self::assertEquals($expected, $actual); } /** * Test getSupportedFormatFields('en'). */ - public function testGetSupportedFormatFieldsLocaleEn() + public function testGetSupportedFormatFieldsLocaleEn(): void { $formatter = new EnglishStandardFormatter(); $expected = $formatter->getSupportedFormatStringFields(); - $actual = $this->_object->getSupportedFormatStringFields('en'); + $actual = (new Ddate())->getSupportedFormatStringFields('en'); self::assertEquals($expected, $actual); } } diff --git a/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php b/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php index 39817ad..4acd71f 100644 --- a/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php +++ b/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php @@ -7,52 +7,29 @@ namespace EmperorNortonCommands\tests\lib; -use EmperorNortonCommands\lib\locale\en\StandardFormatter as EnglishStandardFormatter; use EmperorNortonCommands\lib\FormatterFactory; +use EmperorNortonCommands\lib\locale\de\StandardFormatter as GermanStandardFormatter; +use EmperorNortonCommands\lib\locale\en\StandardFormatter as EnglishStandardFormatter; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use stdClass; -/** - * Class FormatterFactoryTest. - * - * @package EmperorNortonCommands\tests\lib - */ class FormatterFactoryTest extends TestCase { - /** - * Object instance to be tested. - * - * @var FormatterFactory - */ - protected $_object; - - /** - * Set up. - */ - public function setUp(): void - { - $this->_object = new FormatterFactory(); - } - - /** - * Data provider for getFormatter(). - * - * @return array - */ - public static function dataProvider() - { - return array( - array('Foo', 'EmperorNortonCommands\lib\locale\en\StandardFormatter'), - array(new stdClass(), 'EmperorNortonCommands\lib\locale\en\StandardFormatter') - ); + #[DataProvider('dataProvider')] + public function testGetFormatter( + string $expected, + object|string $locale, + ): void { + self::assertTrue((new FormatterFactory())->getFormatter($locale) instanceof $expected); } - /** - * Test getFormatter() - */ - #[DataProvider('dataProvider')] public function testGetFormatter($locale, $expected) + public static function dataProvider(): array { - self::assertTrue($this->_object->getFormatter($locale) instanceof $expected); + return [ + [GermanStandardFormatter::class, 'de'], + [EnglishStandardFormatter::class, 'Foo'], + [EnglishStandardFormatter::class, new stdClass()], + ]; } } diff --git a/src/EmperorNortonCommands/tests/lib/Holydays/EristerTest.php b/src/EmperorNortonCommands/tests/lib/Holydays/EristerTest.php index bba2573..5406951 100644 --- a/src/EmperorNortonCommands/tests/lib/Holydays/EristerTest.php +++ b/src/EmperorNortonCommands/tests/lib/Holydays/EristerTest.php @@ -11,21 +11,25 @@ use EmperorNortonCommands\lib\Holydays\Erister; use EmperorNortonCommands\lib\Value; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -class EristerTest extends TestCase +final class EristerTest extends TestCase { - #[DataProvider('isEristerProvider')] public function testIsErister($expectedTrue, $gregorian, $usePhpCalendarExt) - { - $object = new Erister($usePhpCalendarExt); - if ($expectedTrue) { - self::assertTrue($object->is($this->getMockValue($gregorian))); - } else { - self::assertFalse($object->is($this->getMockValue($gregorian))); - } + #[DataProvider('isEristerProvider')] + public function testIsErister( + bool $expected, + string $gregorian, + bool $usePhpCalendarExt + ): void { + $sut = new Erister($usePhpCalendarExt); + match ($expected) { + true => self::assertTrue($sut->is($this->getMockValue($gregorian))), + false => self::assertFalse($sut->is($this->getMockValue($gregorian))), + }; } - public static function isEristerProvider() + public static function isEristerProvider(): array { return [ '27.03.354' => [true, '27030354', true], @@ -42,7 +46,7 @@ public static function isEristerProvider() ]; } - private function getMockValue($gregorian) + private function getMockValue($gregorian): MockObject&Value { $mock = $this->getMockBuilder(Value::class) ->onlyMethods(['getGregorian']) From 1e24b134191faba80db06c0c29942542150dde33 Mon Sep 17 00:00:00 2001 From: David Weichert Date: Sat, 20 Jul 2024 16:20:43 +0200 Subject: [PATCH 03/10] :recycle: Refa mostly style - add constant visibility and type - fix code style (DdateHolydayTest) - use shorter type cast keywords --- src/EmperorNortonCommands/lib/Converter.php | 6 +++--- src/EmperorNortonCommands/lib/Ddate.php | 6 +++--- src/EmperorNortonCommands/lib/Formatter.php | 2 +- src/EmperorNortonCommands/lib/Holydays.php | 2 +- .../lib/Holydays/CamdenBenaresHolidays.php | 2 +- src/EmperorNortonCommands/lib/Holydays/Erister.php | 4 ++-- .../lib/Holydays/RevDrJonSwabeyWhollydays.php | 3 ++- .../lib/Holydays/RevLoveshadeWhollydays.php | 3 ++- src/EmperorNortonCommands/lib/Holydays/StandardHolydays.php | 2 +- src/EmperorNortonCommands/lib/Value.php | 2 +- src/EmperorNortonCommands/tests/lib/DdateHolydayTest.php | 3 ++- 11 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/EmperorNortonCommands/lib/Converter.php b/src/EmperorNortonCommands/lib/Converter.php index 01e48b3..f029086 100644 --- a/src/EmperorNortonCommands/lib/Converter.php +++ b/src/EmperorNortonCommands/lib/Converter.php @@ -155,7 +155,7 @@ private function calculateSeason(DateTime $date): int */ private function calculateYear(DateTime $date): int { - return (integer)$date->format('Y') + self::CURSE_OF_GREYFACE; + return (int)$date->format('Y') + self::CURSE_OF_GREYFACE; } /** @@ -193,9 +193,9 @@ private function dateDiffInDays(DateTime $date, $iso8601Date): int $diff = $xDay->diff($date); $daysUntilXday = $diff->days; if ($date < $xDay) { - return (integer)$daysUntilXday; + return (int)$daysUntilXday; } - return (integer)$daysUntilXday * -1; + return (int)$daysUntilXday * -1; } /** diff --git a/src/EmperorNortonCommands/lib/Ddate.php b/src/EmperorNortonCommands/lib/Ddate.php index 41b1d6a..1a002d3 100644 --- a/src/EmperorNortonCommands/lib/Ddate.php +++ b/src/EmperorNortonCommands/lib/Ddate.php @@ -126,9 +126,9 @@ private function getDateObject($date) */ private function splitIntoParts($date) { - $year = (integer)substr($date, 4, 4); - $month = (integer)substr($date, 2, 2); - $day = (integer)substr($date, 0, 2); + $year = (int)substr($date, 4, 4); + $month = (int)substr($date, 2, 2); + $day = (int)substr($date, 0, 2); return array($year, $month, $day); } } diff --git a/src/EmperorNortonCommands/lib/Formatter.php b/src/EmperorNortonCommands/lib/Formatter.php index df9acf7..b070b1c 100644 --- a/src/EmperorNortonCommands/lib/Formatter.php +++ b/src/EmperorNortonCommands/lib/Formatter.php @@ -91,7 +91,7 @@ public function setFormat($format = null) * @return string */ abstract public function format(Value $ddate); - + /** * Get Holyday value. * diff --git a/src/EmperorNortonCommands/lib/Holydays.php b/src/EmperorNortonCommands/lib/Holydays.php index 161661f..6fa33b4 100644 --- a/src/EmperorNortonCommands/lib/Holydays.php +++ b/src/EmperorNortonCommands/lib/Holydays.php @@ -16,7 +16,7 @@ */ abstract class Holydays { - const KEY = 'holydays'; + public const string KEY = 'holydays'; /** * Get Holyday. diff --git a/src/EmperorNortonCommands/lib/Holydays/CamdenBenaresHolidays.php b/src/EmperorNortonCommands/lib/Holydays/CamdenBenaresHolidays.php index d46cd07..ee2e4c5 100644 --- a/src/EmperorNortonCommands/lib/Holydays/CamdenBenaresHolidays.php +++ b/src/EmperorNortonCommands/lib/Holydays/CamdenBenaresHolidays.php @@ -16,7 +16,7 @@ */ class CamdenBenaresHolidays extends Holydays { - const KEY = 'camden_benares'; + public const string KEY = 'camden_benares'; /** * @inheritdoc diff --git a/src/EmperorNortonCommands/lib/Holydays/Erister.php b/src/EmperorNortonCommands/lib/Holydays/Erister.php index 6b44bdf..b73dd5e 100644 --- a/src/EmperorNortonCommands/lib/Holydays/Erister.php +++ b/src/EmperorNortonCommands/lib/Holydays/Erister.php @@ -122,14 +122,14 @@ private function getEristerNewYorkCorrespondent($year) $e = $b % 4; $f = floor(($b + 8) / 25); $g = floor(($b - $f + 1) / 3); - $h = (19 * $a + $b -$d -$g + 15) % 30; + $h = (19 * $a + $b - $d - $g + 15) % 30; $i = floor($c / 4); $k = $c % 4; $l = (32 + 2 * $e + 2 * $i - $h - $k) % 7; $m = (int)7 * floor(($a + 11 * $h + 22 * $l) / 451); $month = (int)floor(($h + $l - $m + 114) / 31); - $day = (int)(($h + $l -$m + 114) % 31) + 1; + $day = (int)(($h + $l - $m + 114) % 31) + 1; return array($month, $day); } diff --git a/src/EmperorNortonCommands/lib/Holydays/RevDrJonSwabeyWhollydays.php b/src/EmperorNortonCommands/lib/Holydays/RevDrJonSwabeyWhollydays.php index ba91c60..f999915 100644 --- a/src/EmperorNortonCommands/lib/Holydays/RevDrJonSwabeyWhollydays.php +++ b/src/EmperorNortonCommands/lib/Holydays/RevDrJonSwabeyWhollydays.php @@ -4,6 +4,7 @@ * * Public domain. All rites reversed. */ + namespace EmperorNortonCommands\lib\Holydays; use EmperorNortonCommands\lib\Holydays; @@ -14,7 +15,7 @@ */ class RevDrJonSwabeyWhollydays extends Holydays { - const KEY = 'rev_drjon_swabey'; + public const string KEY = 'rev_drjon_swabey'; /** * @inheritdoc diff --git a/src/EmperorNortonCommands/lib/Holydays/RevLoveshadeWhollydays.php b/src/EmperorNortonCommands/lib/Holydays/RevLoveshadeWhollydays.php index 3e0beb6..aa50e24 100644 --- a/src/EmperorNortonCommands/lib/Holydays/RevLoveshadeWhollydays.php +++ b/src/EmperorNortonCommands/lib/Holydays/RevLoveshadeWhollydays.php @@ -4,6 +4,7 @@ * * Public domain. All rites reversed. */ + namespace EmperorNortonCommands\lib\Holydays; use EmperorNortonCommands\lib\Holydays; @@ -15,7 +16,7 @@ */ class RevLoveshadeWhollydays extends Holydays { - const KEY = 'rev_loveshade'; + public const string KEY = 'rev_loveshade'; /** * @inheritdoc diff --git a/src/EmperorNortonCommands/lib/Holydays/StandardHolydays.php b/src/EmperorNortonCommands/lib/Holydays/StandardHolydays.php index d2e635f..0de6dbe 100644 --- a/src/EmperorNortonCommands/lib/Holydays/StandardHolydays.php +++ b/src/EmperorNortonCommands/lib/Holydays/StandardHolydays.php @@ -15,7 +15,7 @@ */ class StandardHolydays extends Holydays { - const KEY = 'standard'; + public const string KEY = 'standard'; /** * @inheritdoc diff --git a/src/EmperorNortonCommands/lib/Value.php b/src/EmperorNortonCommands/lib/Value.php index 43d418d..8f516e8 100644 --- a/src/EmperorNortonCommands/lib/Value.php +++ b/src/EmperorNortonCommands/lib/Value.php @@ -19,7 +19,7 @@ class Value /** * St. Tibs Day Constant: 23 * 23 = 529 */ - const ST_TIBS_DAY = 529; + public const int ST_TIBS_DAY = 529; /** * Discordian day of week. diff --git a/src/EmperorNortonCommands/tests/lib/DdateHolydayTest.php b/src/EmperorNortonCommands/tests/lib/DdateHolydayTest.php index 0a510c9..068faf6 100644 --- a/src/EmperorNortonCommands/tests/lib/DdateHolydayTest.php +++ b/src/EmperorNortonCommands/tests/lib/DdateHolydayTest.php @@ -97,7 +97,8 @@ public static function camdenBenaresHolidaysProvider(): array public function testFunFridayDe( string $gregorian, string $discordian, - string $format): void { + string $format, + ): void { self::assertEquals($discordian, (new Ddate())->ddate($format, $gregorian, 'de')); } From a4eb23f9f9678e03651913654eba19fc8fa6a2c6 Mon Sep 17 00:00:00 2001 From: David Weichert Date: Sat, 20 Jul 2024 16:32:47 +0200 Subject: [PATCH 04/10] :building_construction: Make test class final --- src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php b/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php index 4acd71f..602aab0 100644 --- a/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php +++ b/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php @@ -14,7 +14,7 @@ use PHPUnit\Framework\TestCase; use stdClass; -class FormatterFactoryTest extends TestCase +final class FormatterFactoryTest extends TestCase { #[DataProvider('dataProvider')] public function testGetFormatter( From 71c3f379a967f4d67a40708549338f522ef1eafd Mon Sep 17 00:00:00 2001 From: David Weichert Date: Sat, 20 Jul 2024 17:44:24 +0200 Subject: [PATCH 05/10] :recycle: Refa mark api and internal classes explicitly, Fixes #59 - remove class comments that convey no additional information - make sure package name is EmperorNortonCommands\lib\Ddate for all DDate internal classes --- src/EmperorNortonCommands/lib/Converter.php | 5 ++--- src/EmperorNortonCommands/lib/Ddate.php | 6 ++++-- src/EmperorNortonCommands/lib/Formatter.php | 4 ++-- src/EmperorNortonCommands/lib/FormatterFactory.php | 4 ++-- src/EmperorNortonCommands/lib/Holydays.php | 4 ++-- .../lib/Holydays/CamdenBenaresHolidays.php | 5 +++-- src/EmperorNortonCommands/lib/Holydays/Erister.php | 10 +++++----- src/EmperorNortonCommands/lib/Holydays/FunFriday.php | 5 ++--- .../lib/Holydays/GoToplessDay.php | 8 ++++---- .../lib/Holydays/NoFixedDateHolydayInterface.php | 7 +++++-- .../lib/Holydays/NoPantsDayInterface.php | 7 +++---- .../lib/Holydays/RevDrJonSwabeyWhollydays.php | 5 +++-- .../lib/Holydays/RevLoveshadeWhollydays.php | 5 +++-- .../lib/Holydays/StandardHolydays.php | 6 ++++-- src/EmperorNortonCommands/lib/Value.php | 5 ++--- .../lib/locale/de/StandardFormatter.php | 4 ++-- .../lib/locale/en/StandardFormatter.php | 4 ++-- 17 files changed, 50 insertions(+), 44 deletions(-) diff --git a/src/EmperorNortonCommands/lib/Converter.php b/src/EmperorNortonCommands/lib/Converter.php index f029086..276dcec 100644 --- a/src/EmperorNortonCommands/lib/Converter.php +++ b/src/EmperorNortonCommands/lib/Converter.php @@ -12,10 +12,9 @@ use DateTime; /** - * Class Converter. + * Converts Gregorian dates to Discordian dates * - * Converts Gregorian dates to Discordian dates. - * @package EmperorNortonCommands\lib + * @package EmperorNortonCommands\lib\Ddate * @internal */ class Converter diff --git a/src/EmperorNortonCommands/lib/Ddate.php b/src/EmperorNortonCommands/lib/Ddate.php index 1a002d3..2588fc2 100644 --- a/src/EmperorNortonCommands/lib/Ddate.php +++ b/src/EmperorNortonCommands/lib/Ddate.php @@ -12,9 +12,11 @@ use InvalidArgumentException; /** - * Class Ddate. + * Provides functionality to convert Gregorian into Discordian dates and format + * the output according to a given format string. * - * @package EmperorNortonCommands + * @package EmperorNortonCommands\lib\Ddate + * @api */ class Ddate { diff --git a/src/EmperorNortonCommands/lib/Formatter.php b/src/EmperorNortonCommands/lib/Formatter.php index b070b1c..d241a2e 100644 --- a/src/EmperorNortonCommands/lib/Formatter.php +++ b/src/EmperorNortonCommands/lib/Formatter.php @@ -10,8 +10,8 @@ use EmperorNortonCommands\lib\Holydays\StandardHolydays; /** - * Class Formatter. - * @package EmperorNortonCommands\lib + * @package EmperorNortonCommands\lib\Ddate + * @internal */ abstract class Formatter { diff --git a/src/EmperorNortonCommands/lib/FormatterFactory.php b/src/EmperorNortonCommands/lib/FormatterFactory.php index d556d0f..01d85d9 100644 --- a/src/EmperorNortonCommands/lib/FormatterFactory.php +++ b/src/EmperorNortonCommands/lib/FormatterFactory.php @@ -8,8 +8,8 @@ namespace EmperorNortonCommands\lib; /** - * Class FormatterFactory - * @package EmperorNortonCommands\lib + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class FormatterFactory { diff --git a/src/EmperorNortonCommands/lib/Holydays.php b/src/EmperorNortonCommands/lib/Holydays.php index 6fa33b4..d7258f6 100644 --- a/src/EmperorNortonCommands/lib/Holydays.php +++ b/src/EmperorNortonCommands/lib/Holydays.php @@ -11,8 +11,8 @@ use DOMXPath; /** - * Class Holydays - * @package EmperorNortonCommands\lib + * @package EmperorNortonCommands\lib\Ddate + * @internal */ abstract class Holydays { diff --git a/src/EmperorNortonCommands/lib/Holydays/CamdenBenaresHolidays.php b/src/EmperorNortonCommands/lib/Holydays/CamdenBenaresHolidays.php index ee2e4c5..15cb57c 100644 --- a/src/EmperorNortonCommands/lib/Holydays/CamdenBenaresHolidays.php +++ b/src/EmperorNortonCommands/lib/Holydays/CamdenBenaresHolidays.php @@ -11,8 +11,9 @@ use EmperorNortonCommands\lib\Value; /** - * Class CamdenBenaresHolidays - * @package EmperorNortonCommands\lib\Holydays + * @see src/EmperorNortonCommands/lib/locale/en/data/camden_benares_holidays.xml + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class CamdenBenaresHolidays extends Holydays { diff --git a/src/EmperorNortonCommands/lib/Holydays/Erister.php b/src/EmperorNortonCommands/lib/Holydays/Erister.php index b73dd5e..e75fbc6 100644 --- a/src/EmperorNortonCommands/lib/Holydays/Erister.php +++ b/src/EmperorNortonCommands/lib/Holydays/Erister.php @@ -10,12 +10,12 @@ use EmperorNortonCommands\lib\Value; /** - * Class Erister + * Erister is a Whollyday occurring on the first Sunday after the first full + * moon after the vernal equinox in the northern hemisphere. Or it happens on + * Easter. * - * A Whollyday occurring on the first Sunday after the first full moon after - * the vernal equinox in the northern hemisphere. Or it happens on Easter. - * - * @package EmperorNortonCommands\lib\Holydays + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class Erister implements NoFixedDateHolydayInterface { diff --git a/src/EmperorNortonCommands/lib/Holydays/FunFriday.php b/src/EmperorNortonCommands/lib/Holydays/FunFriday.php index 0f0abec..c584d0e 100644 --- a/src/EmperorNortonCommands/lib/Holydays/FunFriday.php +++ b/src/EmperorNortonCommands/lib/Holydays/FunFriday.php @@ -10,11 +10,10 @@ use EmperorNortonCommands\lib\Value; /** - * Class FunFriday - * * Fun Friday is the fifth Friday in a month that has five Fridays. * - * @package EmperorNortonCommands\lib\Holydays + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class FunFriday implements NoFixedDateHolydayInterface { diff --git a/src/EmperorNortonCommands/lib/Holydays/GoToplessDay.php b/src/EmperorNortonCommands/lib/Holydays/GoToplessDay.php index f9ce8f7..09ac95f 100644 --- a/src/EmperorNortonCommands/lib/Holydays/GoToplessDay.php +++ b/src/EmperorNortonCommands/lib/Holydays/GoToplessDay.php @@ -10,11 +10,11 @@ use EmperorNortonCommands\lib\Value; /** - * Class GoToplessDay + * Go Topless Day is the Sunday nearest 19th of Bureaucracy (26th of August), + * Women's Equality Day. * - * Sunday nearest 19th of Bureaucracy (26th of August), Women's Equality Day. - * - * @package EmperorNortonCommands\lib\Holydays + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class GoToplessDay implements NoFixedDateHolydayInterface { diff --git a/src/EmperorNortonCommands/lib/Holydays/NoFixedDateHolydayInterface.php b/src/EmperorNortonCommands/lib/Holydays/NoFixedDateHolydayInterface.php index a6b8efd..198cdb2 100644 --- a/src/EmperorNortonCommands/lib/Holydays/NoFixedDateHolydayInterface.php +++ b/src/EmperorNortonCommands/lib/Holydays/NoFixedDateHolydayInterface.php @@ -10,8 +10,11 @@ use EmperorNortonCommands\lib\Value; /** - * Interface NoFixedDateHolyday. - * @package EmperorNortonCommands\lib\Holydays + * Some Holydays occur on days that require calculation rather than on a fixed + * date of the year. + * + * @package EmperorNortonCommands\lib\Ddate + * @internal */ interface NoFixedDateHolydayInterface { diff --git a/src/EmperorNortonCommands/lib/Holydays/NoPantsDayInterface.php b/src/EmperorNortonCommands/lib/Holydays/NoPantsDayInterface.php index 1dbc728..db05d1f 100644 --- a/src/EmperorNortonCommands/lib/Holydays/NoPantsDayInterface.php +++ b/src/EmperorNortonCommands/lib/Holydays/NoPantsDayInterface.php @@ -10,11 +10,10 @@ use EmperorNortonCommands\lib\Value; /** - * Class NoPantsDay + * No Pants Day is the first Friday in May. * - * First Friday in May. - * - * @package EmperorNortonCommands\lib\Holydays + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class NoPantsDayInterface implements NoFixedDateHolydayInterface { diff --git a/src/EmperorNortonCommands/lib/Holydays/RevDrJonSwabeyWhollydays.php b/src/EmperorNortonCommands/lib/Holydays/RevDrJonSwabeyWhollydays.php index f999915..06f9c00 100644 --- a/src/EmperorNortonCommands/lib/Holydays/RevDrJonSwabeyWhollydays.php +++ b/src/EmperorNortonCommands/lib/Holydays/RevDrJonSwabeyWhollydays.php @@ -10,8 +10,9 @@ use EmperorNortonCommands\lib\Holydays; /** - * Class RevDrJonSwabeyHolydays - * @package EmperorNortonCommands\lib\Holydays + * @see src/EmperorNortonCommands/lib/locale/en/data/rev_drjon_swabey_whollydays.xml + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class RevDrJonSwabeyWhollydays extends Holydays { diff --git a/src/EmperorNortonCommands/lib/Holydays/RevLoveshadeWhollydays.php b/src/EmperorNortonCommands/lib/Holydays/RevLoveshadeWhollydays.php index aa50e24..838afaa 100644 --- a/src/EmperorNortonCommands/lib/Holydays/RevLoveshadeWhollydays.php +++ b/src/EmperorNortonCommands/lib/Holydays/RevLoveshadeWhollydays.php @@ -11,8 +11,9 @@ use EmperorNortonCommands\lib\Value; /** - * Class RevLoveshadeWhollydays - * @package EmperorNortonCommands\lib\Holydays + * @see src/EmperorNortonCommands/lib/locale/en/data/rev_loveshade_whollydays.xml + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class RevLoveshadeWhollydays extends Holydays { diff --git a/src/EmperorNortonCommands/lib/Holydays/StandardHolydays.php b/src/EmperorNortonCommands/lib/Holydays/StandardHolydays.php index 0de6dbe..c5d9adb 100644 --- a/src/EmperorNortonCommands/lib/Holydays/StandardHolydays.php +++ b/src/EmperorNortonCommands/lib/Holydays/StandardHolydays.php @@ -10,8 +10,10 @@ use EmperorNortonCommands\lib\Holydays; /** - * Class StandardHolydays - * @package EmperorNortonCommands\lib\Holydays + * The standard holydays defined in Principia Discordia, 00034. + * + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class StandardHolydays extends Holydays { diff --git a/src/EmperorNortonCommands/lib/Value.php b/src/EmperorNortonCommands/lib/Value.php index 8f516e8..d33d30e 100644 --- a/src/EmperorNortonCommands/lib/Value.php +++ b/src/EmperorNortonCommands/lib/Value.php @@ -8,11 +8,10 @@ namespace EmperorNortonCommands\lib; use DateTimeInterface; -use EmperorNortonCommands\lib\Holydays\Erister; /** - * Class Value - * @package EmperorNortonCommands\lib + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class Value { diff --git a/src/EmperorNortonCommands/lib/locale/de/StandardFormatter.php b/src/EmperorNortonCommands/lib/locale/de/StandardFormatter.php index 5af4e1d..7629131 100644 --- a/src/EmperorNortonCommands/lib/locale/de/StandardFormatter.php +++ b/src/EmperorNortonCommands/lib/locale/de/StandardFormatter.php @@ -11,8 +11,8 @@ use EmperorNortonCommands\lib\locale\en\StandardFormatter as EnglishStandardFormatter; /** - * Class StandardFormatter - * @package EmperorNortonCommands\lib\locale\de + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class StandardFormatter extends EnglishStandardFormatter { diff --git a/src/EmperorNortonCommands/lib/locale/en/StandardFormatter.php b/src/EmperorNortonCommands/lib/locale/en/StandardFormatter.php index 716b6a0..c69e2ab 100644 --- a/src/EmperorNortonCommands/lib/locale/en/StandardFormatter.php +++ b/src/EmperorNortonCommands/lib/locale/en/StandardFormatter.php @@ -15,8 +15,8 @@ use EmperorNortonCommands\lib\Value; /** - * Class StandardFormatter - * @package EmperorNortonCommands\lib\locale\en + * @package EmperorNortonCommands\lib\Ddate + * @internal */ class StandardFormatter extends Formatter { From 42d8258370f0bacff248136eec4791cfef4b7cfa Mon Sep 17 00:00:00 2001 From: David Weichert Date: Sun, 21 Jul 2024 12:46:02 +0200 Subject: [PATCH 06/10] :recycle: Refa replace class-name from literal with ::class --- src/EmperorNortonCommands/lib/FormatterFactory.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/EmperorNortonCommands/lib/FormatterFactory.php b/src/EmperorNortonCommands/lib/FormatterFactory.php index 01d85d9..8002312 100644 --- a/src/EmperorNortonCommands/lib/FormatterFactory.php +++ b/src/EmperorNortonCommands/lib/FormatterFactory.php @@ -7,6 +7,9 @@ namespace EmperorNortonCommands\lib; +use EmperorNortonCommands\lib\locale\de\StandardFormatter as GermanStandardFormatter; +use EmperorNortonCommands\lib\locale\en\StandardFormatter as EnglishStandardFormatter; + /** * @package EmperorNortonCommands\lib\Ddate * @internal @@ -21,12 +24,12 @@ class FormatterFactory private $availableFormatters = array( 'en' => array( 'lang' => 'English', - 'class' => 'EmperorNortonCommands\lib\locale\en\StandardFormatter', + 'class' => EnglishStandardFormatter::class, 'holydays' => array('Standard' => 'EmperorNortonCommands\lib\locale\en\StandardHolydays') ), 'de' => array( 'lang' => 'Deutsch', - 'class' => 'EmperorNortonCommands\lib\locale\de\StandardFormatter', + 'class' => GermanStandardFormatter::class, 'holydays' => array('Standard' => 'EmperorNortonCommands\lib\locale\de\StandardHolydays') ) ); From d17316ffc5415c42ef0c9dbbe9c80eef3f880ebc Mon Sep 17 00:00:00 2001 From: David Weichert Date: Sun, 21 Jul 2024 12:49:47 +0200 Subject: [PATCH 07/10] :recycle: Refa split converter functionality - class XDay provides public API for getting days until X-Day from DateTimeInterface - class DiscordianDate provides public API for getting Discordian Date from DateTimeInterface --- src/EmperorNortonCommands/lib/Converter.php | 249 ------------------ src/EmperorNortonCommands/lib/Ddate.php | 62 ++--- .../lib/DiscordianDate.php | 119 +++++++++ .../lib/DiscordianDay.php | 90 +++++++ .../lib/DiscordianSeason.php | 22 ++ .../lib/DiscordianWeekday.php | 22 ++ .../lib/DiscordianYear.php | 20 ++ src/EmperorNortonCommands/lib/StTibsDay.php | 17 ++ src/EmperorNortonCommands/lib/XDay.php | 68 +++++ .../tests/lib/XDayTest.php | 146 ++++++++++ 10 files changed, 524 insertions(+), 291 deletions(-) delete mode 100644 src/EmperorNortonCommands/lib/Converter.php create mode 100644 src/EmperorNortonCommands/lib/DiscordianDate.php create mode 100644 src/EmperorNortonCommands/lib/DiscordianDay.php create mode 100644 src/EmperorNortonCommands/lib/DiscordianSeason.php create mode 100644 src/EmperorNortonCommands/lib/DiscordianWeekday.php create mode 100644 src/EmperorNortonCommands/lib/DiscordianYear.php create mode 100644 src/EmperorNortonCommands/lib/StTibsDay.php create mode 100644 src/EmperorNortonCommands/lib/XDay.php create mode 100644 src/EmperorNortonCommands/tests/lib/XDayTest.php diff --git a/src/EmperorNortonCommands/lib/Converter.php b/src/EmperorNortonCommands/lib/Converter.php deleted file mode 100644 index 276dcec..0000000 --- a/src/EmperorNortonCommands/lib/Converter.php +++ /dev/null @@ -1,249 +0,0 @@ -isStTibsDay((int)$date->format('m'), (int)$date->format('d'))) { - return $this->calculateDdateStTibs($date); - } - return $this->calculateDdate($date); - } - - /** - * Conversion algorithm for St. Tibs Day. - * - * @param DateTime $date - * @return Value - */ - private function calculateDdateStTibs(DateTime $date): Value - { - $ddate = new Value( - Value::ST_TIBS_DAY, - Value::ST_TIBS_DAY, - Value::ST_TIBS_DAY, - $this->calculateYear($date), - $this->calculateDaysUntilXday($date), - $this->calculateDaysUntilOriginalXday($date), - $date - ); - return $ddate; - } - - /** - * Regular conversion algorithm. - * - * @param DateTime $date - * @return Value - */ - private function calculateDdate(DateTime $date): Value - { - $ddate = new Value( - $this->calculateDayofSeason($date), - $this->calculateSeason($date), - $this->calculateDayOfWeek($date), - $this->calculateYear($date), - $this->calculateDaysUntilXday($date), - $this->calculateDaysUntilOriginalXday($date), - $date - ); - - return $ddate; - } - - /** - * Calculate day of Discordian week. - * - * @param DateTime $date - * @return integer - */ - private function calculateDayOfWeek(DateTime $date): int - { - $dayOfYear = $this->getDayOfYear($date); - $leapYear = $this->isLeapYear($date); - $dayOfWeekIdx = ($dayOfYear - (1 + $this->getOffset($leapYear, $dayOfYear))) % 5; - return $dayOfWeekIdx + 1; - } - - /** - * Calculate day of Discordian season. - * - * @param DateTime $date - * @return integer - */ - private function calculateDayofSeason(DateTime $date): int - { - $dayOfYear = $this->getDayOfYear($date); - return (($dayOfYear - (1 + $this->getOffset($this->isLeapYear($date), $dayOfYear))) % 73) + 1; - } - - /** - * Calculate season of Discordian year. - * - * @param DateTime $date - * @return integer - */ - private function calculateSeason(DateTime $date): int - { - $seasonIdx = 0; - $dayOfYear = $this->getDayOfYear($date); - if ($dayOfYear > 59) { - $dayOfYearMinusStTibs = $dayOfYear - $this->getOffset($this->isLeapYear($date), $dayOfYear); - for ($i = 0; $i < 5; $i++) { - if ($dayOfYearMinusStTibs < (74 + $i * 73)) { - $seasonIdx = $i; - break; - } - } - } - return $seasonIdx + 1; - } - - /** - * Get Discordian year. - * - * @param DateTime $date - * @return integer - */ - private function calculateYear(DateTime $date): int - { - return (int)$date->format('Y') + self::CURSE_OF_GREYFACE; - } - - /** - * Calculate days until real X-Day. - * - * @param DateTime $date - * @return integer - */ - private function calculateDaysUntilXday(DateTime $date): int - { - return $this->dateDiffInDays($date, self::REAL_X_DAY); - } - - /** - * Calculate days until original X-Day. - * - * @param DateTime $date - * @return integer - */ - private function calculateDaysUntilOriginalXday(DateTime $date): int - { - return $this->dateDiffInDays($date, self::ORIGINAL_X_DAY); - } - - /** - * Calculate days until date given as ISO 8601 date. - * - * @param DateTime $date - * @param string $iso8601Date - * @return integer - */ - private function dateDiffInDays(DateTime $date, $iso8601Date): int - { - $xDay = new DateTime($iso8601Date); - $diff = $xDay->diff($date); - $daysUntilXday = $diff->days; - if ($date < $xDay) { - return (int)$daysUntilXday; - } - return (int)$daysUntilXday * -1; - } - - /** - * Get offset for leap years. - * - * In leap years for days after the 60th day of the year, i.e. 29th of - * February there is an offset of 1 day to be taken into account. - * - * @return integer - */ - private function getOffset(bool $leapYear, int $dayOfYear): int - { - if ($dayOfYear < 60) { - return 0; - } - return $leapYear ? 1 : 0; - } - - /** - * Returns true if it is St. Tib's Day. - * - * @param integer $monthGregorian - * @param integer $dayGregorian - * @return boolean - */ - private function isStTibsDay(int $monthGregorian, int $dayGregorian): bool - { - return 2 == $monthGregorian && 29 == $dayGregorian; - } - - /** - * Get days since 1st of Chaos. - * - * @param DateTime $date - * @return integer - */ - private function getDayOfYear(DateTime $date): int - { - return (int) $date->format('z') + 1; - } - - /** - * Get leap year. - * - * @param DateTime $date - * @return boolean - */ - private function isLeapYear(DateTime $date): bool - { - return (bool) $date->format('L'); - } -} diff --git a/src/EmperorNortonCommands/lib/Ddate.php b/src/EmperorNortonCommands/lib/Ddate.php index 2588fc2..3e664b4 100644 --- a/src/EmperorNortonCommands/lib/Ddate.php +++ b/src/EmperorNortonCommands/lib/Ddate.php @@ -4,6 +4,7 @@ * * Public domain. All rites reversed. */ +declare(strict_types=1); namespace EmperorNortonCommands\lib; @@ -18,42 +19,12 @@ * @package EmperorNortonCommands\lib\Ddate * @api */ -class Ddate +final readonly class Ddate { - /** - * Discordian date formatter factory. - * - * @var FormatterFactory - */ - private $formatterFactory; - - /** - * Discordian date converter. - * - * @var Converter - */ - private $converter; + private FormatterFactory $formatterFactory; - /** - * Constructor method. - * - * @param Converter $converter OPTIONAL Converts Gregorian to Discordian dates - * @param FormatterFactory $formatterFactory OPTIONAL Discordian date formatter - */ - public function __construct( - Converter $converter = null, - FormatterFactory $formatterFactory = null - ) { - if (is_null($formatterFactory)) { - $this->formatterFactory = new FormatterFactory(); - } else { - $this->formatterFactory = $formatterFactory; - } - if (is_null($converter)) { - $this->converter = new Converter(); - } else { - $this->converter = $converter; - } + public function __construct() { + $this->formatterFactory = new FormatterFactory(); } /** @@ -89,7 +60,16 @@ public function getSupportedFormatStringFields($locale = 'en') public function ddate($format = null, $date = null, $locale = 'en') { $dateObj = $this->getDateObject($date); - $ddate = $this->converter->convert($dateObj); + $discordianDate = DiscordianDate::fromDateTimeInterface($dateObj); + $ddate = new Value( + $discordianDate->day instanceof StTibsDay ? Value::ST_TIBS_DAY : $discordianDate->day->value, + $discordianDate->season instanceof StTibsDay ? Value::ST_TIBS_DAY : $discordianDate->season->value, + $discordianDate->weekday instanceof StTibsDay ? Value::ST_TIBS_DAY : $discordianDate->weekday->value, + $discordianDate->year->value, + XDay::daysUntilRealXDay($dateObj), + XDay::daysUntilOriginalXDay($dateObj), + $dateObj, + ); $formatter = $this->formatterFactory->getFormatter($locale); $formatter->setFormat($format); return $formatter->format($ddate); @@ -98,11 +78,9 @@ public function ddate($format = null, $date = null, $locale = 'en') /** * Get date object from input. * - * @param string $date Gregorian date (dmY) - * @return DateTime - * @throws InvalidArgumentException + * @throws InvalidArgumentException|\Exception */ - private function getDateObject($date) + private function getDateObject(string|int|null $date): DateTime { if (null === $date) { return new DateTime(); @@ -110,12 +88,12 @@ private function getDateObject($date) if (!is_numeric($date) && 8 !== strlen($date)) { throw new InvalidArgumentException('Second argument expected to be a Gregorian date (dmY).'); } + $date = (string) $date; list($year, $month, $day) = $this->splitIntoParts($date); if (!checkdate($month, $day, $year)) { throw new InvalidArgumentException('Second argument expected to be a Gregorian date (dmY).'); } - $dateObject = new DateTime($year . '-' . $month . '-' . $day, new DateTimeZone('UTC')); - return $dateObject; + return new DateTime($year . '-' . $month . '-' . $day, new DateTimeZone('UTC')); } /** @@ -126,7 +104,7 @@ private function getDateObject($date) * @param string $date Gregorian date (dmY) * @return array */ - private function splitIntoParts($date) + private function splitIntoParts(string $date): array { $year = (int)substr($date, 4, 4); $month = (int)substr($date, 2, 2); diff --git a/src/EmperorNortonCommands/lib/DiscordianDate.php b/src/EmperorNortonCommands/lib/DiscordianDate.php new file mode 100644 index 0000000..43c0483 --- /dev/null +++ b/src/EmperorNortonCommands/lib/DiscordianDate.php @@ -0,0 +1,119 @@ + DiscordianSeason::Chaos, + $dayOfYear <= 146 => DiscordianSeason::Discord, + $dayOfYear <= 219 => DiscordianSeason::Confusion, + $dayOfYear <= 292 => DiscordianSeason::Bureaucracy, + $dayOfYear > 292 => DiscordianSeason::The_Aftermath, + }; + } + + private static function getDiscordianYear(DateTimeInterface $dateTime): DiscordianYear + { + return new DiscordianYear((int) $dateTime->format('Y') + self::CURSE_OF_GREYFACE); + } + + /** + * Calculates current day of year in days from 1st of Chaos. St. Tibs Day + * does not count, i.e. it is ignored. + */ + private static function getDayOfYear(DateTimeInterface $dateTime): int + { + $dayOfYearGregorian = self::getDayOfYearGregorian($dateTime); + return $dayOfYearGregorian < self::SIXTIETH_DAY_OF_THE_YEAR + ? $dayOfYearGregorian + : (self::isLeapYear($dateTime) ? $dayOfYearGregorian - 1 : $dayOfYearGregorian); + } + + private static function isStTibsDay(DateTimeInterface $dateTime): bool + { + return (int) $dateTime->format('d') === 29 && (int) $dateTime->format('m') === 2; + } + + private static function isLeapYear(DateTimeInterface $dateTime): bool + { + return (bool) $dateTime->format('L'); + } + + private static function getDayOfYearGregorian(DateTimeInterface $dateTime): int + { + return ((int) $dateTime->format('z')) + 1; + } +} diff --git a/src/EmperorNortonCommands/lib/DiscordianDay.php b/src/EmperorNortonCommands/lib/DiscordianDay.php new file mode 100644 index 0000000..5351cdf --- /dev/null +++ b/src/EmperorNortonCommands/lib/DiscordianDay.php @@ -0,0 +1,90 @@ +diff($from); + if ($from < $until) { + return (int) $diff->days; + } + return (int) $diff->days * -1; + } +} diff --git a/src/EmperorNortonCommands/tests/lib/XDayTest.php b/src/EmperorNortonCommands/tests/lib/XDayTest.php new file mode 100644 index 0000000..1cecd19 --- /dev/null +++ b/src/EmperorNortonCommands/tests/lib/XDayTest.php @@ -0,0 +1,146 @@ + [ + 'expectedDaysUntilOriginalXDay' => 1, + 'expectedDaysUntilRealXDay' => 2433612, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '1998-07-04T10:00:00+0000' + ), + ], + '24 hours before original X-Day' => [ + 'expectedDaysUntilOriginalXDay' => 1, + 'expectedDaysUntilRealXDay' => 2433612, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '1998-07-04T11:00:00+0000' + ), + ], + '23 hours before original X-Day' => [ + 'expectedDaysUntilOriginalXDay' => 0, + 'expectedDaysUntilRealXDay' => 2433611, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '1998-07-04T12:00:00+0000' + ), + ], + 'Exactly original X-Day' => [ + 'expectedDaysUntilOriginalXDay' => 0, + 'expectedDaysUntilRealXDay' => 2433611, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '1998-07-05T11:00:00+0000' + ), + ], + '23 hours after original X-Day' => [ + 'expectedDaysUntilOriginalXDay' => 0, + 'expectedDaysUntilRealXDay' => 2433610, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '1998-07-06T10:00:00+0000' + ), + ], + '24 hours after original X-Day' => [ + 'expectedDaysUntilOriginalXDay' => -1, + 'expectedDaysUntilRealXDay' => 2433610, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '1998-07-06T11:00:00+0000' + ), + ], + '25 hours after original X-Day' => [ + 'expectedDaysUntilOriginalXDay' => -1, + 'expectedDaysUntilRealXDay' => 2433609, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '1998-07-06T12:00:00+0000' + ), + ], + '25 hours before real X-Day' => [ + 'expectedDaysUntilOriginalXDay' => -2433609, + 'expectedDaysUntilRealXDay' => 1, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '8661-07-04T10:00:00+0000' + ), + ], + '24 hours before real X-Day' => [ + 'expectedDaysUntilOriginalXDay' => -2433610, + 'expectedDaysUntilRealXDay' => 1, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '8661-07-04T11:00:00+0000' + ), + ], + '23 hours before real X-Day' => [ + 'expectedDaysUntilOriginalXDay' => -2433610, + 'expectedDaysUntilRealXDay' => 0, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '8661-07-04T12:00:00+0000' + ), + ], + 'Exactly real X-Day' => [ + 'expectedDaysUntilOriginalXDay' => -2433611, + 'expectedDaysUntilRealXDay' => 0, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '8661-07-05T11:00:00+0000' + ), + ], + '23 hours after real X-Day' => [ + 'expectedDaysUntilOriginalXDay' => -2433611, + 'expectedDaysUntilRealXDay' => 0, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '8661-07-06T10:00:00+0000' + ), + ], + '24 hours after real X-Day' => [ + 'expectedDaysUntilOriginalXDay' => -2433612, + 'expectedDaysUntilRealXDay' => -1, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '8661-07-06T11:00:00+0000' + ), + ], + '25 hours after real X-Day' => [ + 'expectedDaysUntilOriginalXDay' => -2433612, + 'expectedDaysUntilRealXDay' => -1, + 'dateTime' => DateTimeImmutable::createFromFormat( + format: DateTimeInterface::ATOM, + datetime: '8661-07-06T12:00:00+0000' + ), + ], + ]; + } + +} From 457654122a016a6eabd6224aec96532dc41ece56 Mon Sep 17 00:00:00 2001 From: David Weichert Date: Wed, 24 Jul 2024 00:31:15 +0200 Subject: [PATCH 08/10] :recycle: Refa Formatter and FormatterFactory - use Enum for locale parameter - array short syntax --- src/EmperorNortonCommands/lib/Ddate.php | 13 +++++---- src/EmperorNortonCommands/lib/Formatter.php | 6 ++--- .../lib/FormatterFactory.php | 14 ++-------- src/EmperorNortonCommands/lib/Locale.php | 27 +++++++++++++++++++ .../lib/locale/de/StandardFormatter.php | 20 +++++++------- .../lib/locale/en/StandardFormatter.php | 16 +++++------ .../tests/lib/FormatterFactoryTest.php | 16 ++++++----- 7 files changed, 65 insertions(+), 47 deletions(-) create mode 100644 src/EmperorNortonCommands/lib/Locale.php diff --git a/src/EmperorNortonCommands/lib/Ddate.php b/src/EmperorNortonCommands/lib/Ddate.php index 3e664b4..98f2270 100644 --- a/src/EmperorNortonCommands/lib/Ddate.php +++ b/src/EmperorNortonCommands/lib/Ddate.php @@ -30,12 +30,11 @@ public function __construct() { /** * Returns array of all supported format strings. * - * @param string $locale OPTIONAL e.g. en for English, de for German, ... - * @return string[] + * @return array */ - public function getSupportedFormatStringFields($locale = 'en') + public function getSupportedFormatStringFields(string|null $locale = null): array { - return $this->formatterFactory->getFormatter($locale)->getSupportedFormatStringFields(); + return $this->formatterFactory->getFormatter(Locale::fromStringOrNull($locale))->getSupportedFormatStringFields(); } /** @@ -53,11 +52,11 @@ public function getSupportedFormatStringFields($locale = 'en') * * @param string $format OPTIONAL format string * @param string $date OPTIONAL Gregorian date - * @param string $locale OPTIONAL e.g. en for English, de for German, ... + * @param string|null $locale OPTIONAL e.g. en for English, de for German, ... * @return string * @throws InvalidArgumentException */ - public function ddate($format = null, $date = null, $locale = 'en') + public function ddate($format = null, $date = null, string|null $locale = null) { $dateObj = $this->getDateObject($date); $discordianDate = DiscordianDate::fromDateTimeInterface($dateObj); @@ -70,7 +69,7 @@ public function ddate($format = null, $date = null, $locale = 'en') XDay::daysUntilOriginalXDay($dateObj), $dateObj, ); - $formatter = $this->formatterFactory->getFormatter($locale); + $formatter = $this->formatterFactory->getFormatter(Locale::fromStringOrNull($locale)); $formatter->setFormat($format); return $formatter->format($ddate); } diff --git a/src/EmperorNortonCommands/lib/Formatter.php b/src/EmperorNortonCommands/lib/Formatter.php index d241a2e..17532af 100644 --- a/src/EmperorNortonCommands/lib/Formatter.php +++ b/src/EmperorNortonCommands/lib/Formatter.php @@ -18,7 +18,7 @@ abstract class Formatter /** * Returns array of all supported format strings. * - * @var string[] + * @var array */ protected $supportedFormatStringFields = array(); @@ -62,9 +62,9 @@ public function __construct() /** * Get supported format string fields. * - * @return string[] + * @return array */ - public function getSupportedFormatStringFields() + public function getSupportedFormatStringFields(): array { return $this->supportedFormatStringFields; } diff --git a/src/EmperorNortonCommands/lib/FormatterFactory.php b/src/EmperorNortonCommands/lib/FormatterFactory.php index 8002312..1ea6afa 100644 --- a/src/EmperorNortonCommands/lib/FormatterFactory.php +++ b/src/EmperorNortonCommands/lib/FormatterFactory.php @@ -36,20 +36,10 @@ class FormatterFactory /** * Get Discordian date formatter. - * - * @param string $locale two-letter locale identifier, e.g. "en" for English - * @return Formatter */ - public function getFormatter($locale) + public function getFormatter(Locale $locale): Formatter { - if (is_object($locale) && !method_exists($locale, '__toString')) { - $locale = 'en'; - } - $locale = strtolower(substr($locale, 0, 2)); - if (!array_key_exists($locale, $this->availableFormatters)) { - $locale = 'en'; - } - $formatter = (string)$this->availableFormatters[$locale]['class']; + $formatter = (string)$this->availableFormatters[$locale->value]['class']; return new $formatter(); } diff --git a/src/EmperorNortonCommands/lib/Locale.php b/src/EmperorNortonCommands/lib/Locale.php new file mode 100644 index 0000000..2f94429 --- /dev/null +++ b/src/EmperorNortonCommands/lib/Locale.php @@ -0,0 +1,27 @@ + */ - protected $supportedFormatStringFields = array( + protected $supportedFormatStringFields = [ '%A' => 'Vollständiger Name des Woechentages (z.B. Süßmorgen)', '%a' => 'Abgekürzter Name des Wochentages (z.B. SM)', '%B' => 'Vollständiger Name der Saison (z.B. Verwirrung)', @@ -43,14 +43,14 @@ class StandardFormatter extends EnglishStandardFormatter '%2' => 'füge "Camden Beneres\' Holidays" zu den Holydays (Feiertagen) hinzu', '%3' => 'füge "Reverent DrJon Swabey\'s Whollydays" zu den Holydays (Feiertagen) hinzu', '%4' => 'füge "Reverent Loveshade\'s Whollydays" zu den Holydays (Feiertagen) hinzu', - ); + ]; /** * Cardinal numbers. * * @var string[] */ - protected $_cardinalNumbers = array( + protected $_cardinalNumbers = [ 'Erster', 'Zweiter', 'Dritter', 'Vierter', 'Fünfter', 'Sechster', 'Siebter', 'Achter', 'Neunter', 'Zehnter', 'Elfter', 'Zwölfter', 'Dreizehnter', 'Vierzehnter', 'Fünfzehnter', 'Sechzehnter', 'Siebzehnter', 'Achtzehnter', 'Neunzehnter', 'vierzigster', 'Einundfünfzigster', 'Zweiundzwanzigster', 'Dreiundzwanzigster', 'Vierundzwanzigster', 'Fünfundzwanzigster', 'Sechsundzwanzigster', 'Siebenundzwanzigster', 'Achtundzwanzigster', 'Neunundzwanzigster', 'Dreißigster', @@ -59,7 +59,7 @@ class StandardFormatter extends EnglishStandardFormatter 'Einundfünfzigster', 'Zweiundfünfzigster', 'Dreiundfünfzigster', 'Vierundfünfzigster', 'Fünfundfünfzigster', 'Sechsundfünfzigster', 'Siebenundfünfzigster', 'Achtundfünfzigster', 'Neunundfünfzigster', 'Sechzigster', 'Einundsechzigster', 'Zweiundsechzigster', 'Dreiundsechzigster', 'Vierundsechzigster', 'Fünfundsechzigster', 'Sechsundsechzigster', 'Siebenundsechzigster', 'Achtundsechzigster', 'Neunundsechzigster', 'Siebzigster', 'Einundsiebzigster', 'Zweiundsiebzigster', 'Dreiundsiebzigster' - ); + ]; /** * Default format. @@ -73,35 +73,35 @@ class StandardFormatter extends EnglishStandardFormatter * * @var string[] */ - protected $days = array('Süßmorgen', 'Blütezeit', 'Stichtag', 'Prickel-Prickel', 'Orangewerdend'); + protected $days = ['Süßmorgen', 'Blütezeit', 'Stichtag', 'Prickel-Prickel', 'Orangewerdend']; /** * Abbreviated names of the day of the week. * * @var string[] */ - protected $abbrevDays = array('SM', 'BZ', 'ST', 'PP', 'OW'); + protected $abbrevDays = ['SM', 'BZ', 'ST', 'PP', 'OW']; /** * Full names of the season. * * @var string[] */ - protected $seasons = array('Verwirrung', 'Zweitracht', 'Unordnung', 'Beamtenherrschaft', 'Grummet'); + protected $seasons = ['Verwirrung', 'Zweitracht', 'Unordnung', 'Beamtenherrschaft', 'Grummet']; /** * Full names of the season. * * @var string[] */ - protected $seasonsGenitive = array('der Verwirrung', 'der Zweitracht', 'der Unordnung', 'der Beamtenherrschaft', 'des Grummets'); + protected $seasonsGenitive = ['der Verwirrung', 'der Zweitracht', 'der Unordnung', 'der Beamtenherrschaft', 'des Grummets']; /** * Abbreviated names of the seasons. * * @var string[] */ - protected $abbrevSeasons = array('Ve', 'Zw', 'Un', 'Be', 'Au'); + protected $abbrevSeasons = ['Ve', 'Zw', 'Un', 'Be', 'Au']; /** * No Holyday (msgid) string. diff --git a/src/EmperorNortonCommands/lib/locale/en/StandardFormatter.php b/src/EmperorNortonCommands/lib/locale/en/StandardFormatter.php index c69e2ab..1b88ed1 100644 --- a/src/EmperorNortonCommands/lib/locale/en/StandardFormatter.php +++ b/src/EmperorNortonCommands/lib/locale/en/StandardFormatter.php @@ -23,9 +23,9 @@ class StandardFormatter extends Formatter /** * Supported format string fields and description. * - * @var string[] + * @var array */ - protected $supportedFormatStringFields = array( + protected $supportedFormatStringFields = [ '%A' => 'Full name of the day of the week (e.g. Sweetmorn)', '%a' => 'Abbreviated name of the day of the week (e.g. SM)', '%B' => 'Full name of the season (e.g. Chaos)', @@ -45,7 +45,7 @@ class StandardFormatter extends Formatter '%2' => 'add "Camden Beneres\' Holidays" to Holydays', '%3' => 'add "Reverent DrJon Swabey\'s Whollydays" to Holydays', '%4' => 'add "Reverent Loveshade\'s Whollydays" to Holydays', - ); + ]; /** * Default format. @@ -59,28 +59,28 @@ class StandardFormatter extends Formatter * * @var string[] */ - protected $days = array('Sweetmorn', 'Boomtime', 'Pungenday', 'Prickle-Prickle', 'Setting Orange'); + protected $days = ['Sweetmorn', 'Boomtime', 'Pungenday', 'Prickle-Prickle', 'Setting Orange']; /** * Abbreviated names of the day of the week. * * @var string[] */ - protected $abbrevDays = array('SM', 'BT', 'PD', 'PP', 'SO'); + protected $abbrevDays = ['SM', 'BT', 'PD', 'PP', 'SO']; /** * Full names of the season. * * @var string[] */ - protected $seasons = array('Chaos', 'Discord', 'Confusion', 'Bureaucracy', 'The Aftermath'); + protected $seasons = ['Chaos', 'Discord', 'Confusion', 'Bureaucracy', 'The Aftermath']; /** * Abbreviated names of the seasons. * * @var string[] */ - protected $abbrevSeasons = array('Chs', 'Dsc', 'Cfn', 'Bcy', 'Afm'); + protected $abbrevSeasons = ['Chs', 'Dsc', 'Cfn', 'Bcy', 'Afm']; /** * No Holyday (msgid) string. @@ -205,7 +205,7 @@ protected function getCardinalDay($day) return 'FNORD'; } $suffix = 'th'; - if (!in_array($day, array(11, 12, 13))) { + if (!in_array($day, [11, 12, 13])) { $lastDigitDay = substr($day, -1, 1); switch ($lastDigitDay) { case '1': diff --git a/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php b/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php index 602aab0..0238296 100644 --- a/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php +++ b/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php @@ -8,28 +8,30 @@ namespace EmperorNortonCommands\tests\lib; use EmperorNortonCommands\lib\FormatterFactory; +use EmperorNortonCommands\lib\Locale; use EmperorNortonCommands\lib\locale\de\StandardFormatter as GermanStandardFormatter; use EmperorNortonCommands\lib\locale\en\StandardFormatter as EnglishStandardFormatter; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -use stdClass; final class FormatterFactoryTest extends TestCase { - #[DataProvider('dataProvider')] + /** + * @param class-string $expected + */ + #[DataProvider('provideGetFormatter')] public function testGetFormatter( string $expected, - object|string $locale, + Locale $locale, ): void { self::assertTrue((new FormatterFactory())->getFormatter($locale) instanceof $expected); } - public static function dataProvider(): array + public static function provideGetFormatter(): array { return [ - [GermanStandardFormatter::class, 'de'], - [EnglishStandardFormatter::class, 'Foo'], - [EnglishStandardFormatter::class, new stdClass()], + 'German Locale' => [GermanStandardFormatter::class, Locale::German], + 'English Locale' => [EnglishStandardFormatter::class, Locale::English], ]; } } From 9bda744bdaa8115bff3ad6eae446a6f73fdbdc0a Mon Sep 17 00:00:00 2001 From: David Weichert Date: Mon, 5 Aug 2024 20:44:38 +0200 Subject: [PATCH 09/10] :recycle: Processing of date parameter - create DateTimeImmutable via factory - add type hint for public method --- .../lib/DateTimeFactory.php | 62 +++++++++++++++++++ src/EmperorNortonCommands/lib/Ddate.php | 47 ++------------ .../tests/lib/DdateTest.php | 8 +-- 3 files changed, 69 insertions(+), 48 deletions(-) create mode 100644 src/EmperorNortonCommands/lib/DateTimeFactory.php diff --git a/src/EmperorNortonCommands/lib/DateTimeFactory.php b/src/EmperorNortonCommands/lib/DateTimeFactory.php new file mode 100644 index 0000000..cedb56e --- /dev/null +++ b/src/EmperorNortonCommands/lib/DateTimeFactory.php @@ -0,0 +1,62 @@ +getDateObject($date); + $dateObj = DateTimeFactory::createFromStringOrNull($date); $discordianDate = DiscordianDate::fromDateTimeInterface($dateObj); $ddate = new Value( $discordianDate->day instanceof StTibsDay ? Value::ST_TIBS_DAY : $discordianDate->day->value, @@ -73,41 +71,4 @@ public function ddate($format = null, $date = null, string|null $locale = null) $formatter->setFormat($format); return $formatter->format($ddate); } - - /** - * Get date object from input. - * - * @throws InvalidArgumentException|\Exception - */ - private function getDateObject(string|int|null $date): DateTime - { - if (null === $date) { - return new DateTime(); - } - if (!is_numeric($date) && 8 !== strlen($date)) { - throw new InvalidArgumentException('Second argument expected to be a Gregorian date (dmY).'); - } - $date = (string) $date; - list($year, $month, $day) = $this->splitIntoParts($date); - if (!checkdate($month, $day, $year)) { - throw new InvalidArgumentException('Second argument expected to be a Gregorian date (dmY).'); - } - return new DateTime($year . '-' . $month . '-' . $day, new DateTimeZone('UTC')); - } - - /** - * Splits date string into parts. - * - * Returns array($day, $month, $year). - * - * @param string $date Gregorian date (dmY) - * @return array - */ - private function splitIntoParts(string $date): array - { - $year = (int)substr($date, 4, 4); - $month = (int)substr($date, 2, 2); - $day = (int)substr($date, 0, 2); - return array($year, $month, $day); - } } diff --git a/src/EmperorNortonCommands/tests/lib/DdateTest.php b/src/EmperorNortonCommands/tests/lib/DdateTest.php index 2ebddb8..c36b0ef 100644 --- a/src/EmperorNortonCommands/tests/lib/DdateTest.php +++ b/src/EmperorNortonCommands/tests/lib/DdateTest.php @@ -9,9 +9,7 @@ use DateTime; use EmperorNortonCommands\lib\Ddate; -use EmperorNortonCommands\lib\Converter; use EmperorNortonCommands\lib\locale\en\StandardFormatter as EnglishStandardFormatter; -use EmperorNortonCommands\lib\FormatterFactory; use InvalidArgumentException; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -118,12 +116,12 @@ public static function ddateDataProvider(): array } /** - * Test ddate() with invalid argument (wrong type). + * Test ddate() with invalid argument (string too long and not numeric). */ public function testInvalidDateWrongType(): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Second argument expected to be a Gregorian date (dmY).'); + $this->expectExceptionMessage('Given date value expected to be string (format dmY) or null, found "Lorem ipsum dolor sit amet.": string is not numeric or given length is not 8 characters'); (new Ddate())->ddate(null, 'Lorem ipsum dolor sit amet.'); } @@ -134,7 +132,7 @@ public function testInvalidDateWrongType(): void public function testInvalidDate(): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Second argument expected to be a Gregorian date (dmY).'); + $this->expectExceptionMessage('Given date value expected to be string (format dmY) or null, found "29021997": is not a valid Gregorian date'); (new Ddate())->ddate(null, 29021997); } From dcb76c05ba8af263e112c2212c7f710df6477473 Mon Sep 17 00:00:00 2001 From: David Weichert Date: Mon, 5 Aug 2024 21:52:31 +0200 Subject: [PATCH 10/10] :recycle: Refa Formatter and FormatterFactory - rename to method to create instance - make method to create instance static --- src/EmperorNortonCommands/lib/Ddate.php | 10 ++---- .../lib/FormatterFactory.php | 31 +++++-------------- .../tests/lib/FormatterFactoryTest.php | 2 +- 3 files changed, 11 insertions(+), 32 deletions(-) diff --git a/src/EmperorNortonCommands/lib/Ddate.php b/src/EmperorNortonCommands/lib/Ddate.php index 668ef0e..fd879ea 100644 --- a/src/EmperorNortonCommands/lib/Ddate.php +++ b/src/EmperorNortonCommands/lib/Ddate.php @@ -19,12 +19,6 @@ */ final readonly class Ddate { - private FormatterFactory $formatterFactory; - - public function __construct() { - $this->formatterFactory = new FormatterFactory(); - } - /** * Returns array of all supported format strings. * @@ -32,7 +26,7 @@ public function __construct() { */ public function getSupportedFormatStringFields(string|null $locale = null): array { - return $this->formatterFactory->getFormatter(Locale::fromStringOrNull($locale))->getSupportedFormatStringFields(); + return FormatterFactory::createFormatter(Locale::fromStringOrNull($locale))->getSupportedFormatStringFields(); } /** @@ -67,7 +61,7 @@ public function ddate($format = null, string|null $date = null, string|null $loc XDay::daysUntilOriginalXDay($dateObj), $dateObj, ); - $formatter = $this->formatterFactory->getFormatter(Locale::fromStringOrNull($locale)); + $formatter = FormatterFactory::createFormatter(Locale::fromStringOrNull($locale)); $formatter->setFormat($format); return $formatter->format($ddate); } diff --git a/src/EmperorNortonCommands/lib/FormatterFactory.php b/src/EmperorNortonCommands/lib/FormatterFactory.php index 1ea6afa..0f0018e 100644 --- a/src/EmperorNortonCommands/lib/FormatterFactory.php +++ b/src/EmperorNortonCommands/lib/FormatterFactory.php @@ -5,6 +5,8 @@ * Public domain. All rites reversed. */ +declare(strict_types=1); + namespace EmperorNortonCommands\lib; use EmperorNortonCommands\lib\locale\de\StandardFormatter as GermanStandardFormatter; @@ -17,30 +19,13 @@ class FormatterFactory { /** - * Available formatters. - * - * @var mixed[] + * Create DiscordianDateFormatter for given locale. */ - private $availableFormatters = array( - 'en' => array( - 'lang' => 'English', - 'class' => EnglishStandardFormatter::class, - 'holydays' => array('Standard' => 'EmperorNortonCommands\lib\locale\en\StandardHolydays') - ), - 'de' => array( - 'lang' => 'Deutsch', - 'class' => GermanStandardFormatter::class, - 'holydays' => array('Standard' => 'EmperorNortonCommands\lib\locale\de\StandardHolydays') - ) - ); - - /** - * Get Discordian date formatter. - */ - public function getFormatter(Locale $locale): Formatter + public static function createFormatter(Locale $locale): Formatter { - $formatter = (string)$this->availableFormatters[$locale->value]['class']; - - return new $formatter(); + return match ($locale) { + Locale::English => new EnglishStandardFormatter(), + Locale::German => new GermanStandardFormatter(), + }; } } diff --git a/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php b/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php index 0238296..223a96d 100644 --- a/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php +++ b/src/EmperorNortonCommands/tests/lib/FormatterFactoryTest.php @@ -24,7 +24,7 @@ public function testGetFormatter( string $expected, Locale $locale, ): void { - self::assertTrue((new FormatterFactory())->getFormatter($locale) instanceof $expected); + self::assertTrue(FormatterFactory::createFormatter($locale) instanceof $expected); } public static function provideGetFormatter(): array