From 72e9cc4dc921052d29709f5b1aa2413740215a6e Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Mon, 16 Jan 2023 15:09:49 +0400 Subject: [PATCH 01/19] fix XXXX humanization --- .../Humanizer/InternationalizedHumanizer.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php index 66add18..f8a9bd7 100644 --- a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php +++ b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php @@ -217,7 +217,7 @@ private function humanizeDateWithoutUncertainty( ExtDate $date ): string { if ( $year !== null ) { $year = $this->humanizeYear( $year, - $date->getUnspecifiedDigit() + $date ); } @@ -247,7 +247,17 @@ private function humanizeYearMonthDay( ?string $year, ?string $month, ?string $d ); } - private function humanizeYear( int $year, UnspecifiedDigit $unspecifiedDigit ): string { + private function humanizeYear( int $year, ExtDate $date ): string { + $unspecifiedDigit = $date->getUnspecifiedDigit(); + + // *** if the expression $date->getDay() === null && $date->getMonth() === null + // is superfluous as it seems the function can be restored to its + // original declaration ( int $year, UnspecifiedDigit $unspecifiedDigit ) + + if ( $unspecifiedDigit->getYear() === 4 && $date->getDay() === null && $date->getMonth() === null ) { + return $this->message( 'edtf-year-unspecified' ); + } + $yearStr = (string)abs( $year ); if ( $year <= -1000 ) { From 5ffc831fe86a9ac1b2ea603cd94691962d19e17f Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Mon, 16 Jan 2023 15:10:35 +0400 Subject: [PATCH 02/19] fix XXXX humanization --- i18n/en.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/i18n/en.json b/i18n/en.json index 1ebc3ce..4a0e6fb 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -17,6 +17,8 @@ "edtf-month": "month", "edtf-year": "year", + "edtf-year-unspecified": "some year", + "edtf-spring": "Spring", "edtf-summer": "Summer", "edtf-autumn": "Autumn", From 6199d5431474149f7b33186fae2a52ae522b2e66 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Mon, 16 Jan 2023 15:11:26 +0400 Subject: [PATCH 03/19] XXXX humanization --- tests/Functional/EnglishHumanizationTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Functional/EnglishHumanizationTest.php b/tests/Functional/EnglishHumanizationTest.php index 7135520..5c121fc 100644 --- a/tests/Functional/EnglishHumanizationTest.php +++ b/tests/Functional/EnglishHumanizationTest.php @@ -33,6 +33,10 @@ public function humanizationProvider(): Generator { yield 'Month only' => [ 'XXXX-12-XX', 'December' ]; yield 'Day only' => [ 'XXXX-XX-12', '12th' ]; + + // https://github.com/ProfessionalWiki/EDTF/issues/80 + yield 'Some year' => [ 'XXXX', 'some year' ]; + yield 'Month and day' => [ 'XXXX-12-11', 'December 11th' ]; yield 'Year and day' => [ '2020-XX-11', '11th of unknown month, 2020' ]; yield 'Unspecified year decade' => [ '197X', '1970s' ]; From 80bee4ee04449786f2318e844b46e69785c431ec Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 00:36:12 +0400 Subject: [PATCH 04/19] implements scales --- src/Model/ExtDate.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Model/ExtDate.php b/src/Model/ExtDate.php index 74ff27d..ba68b51 100644 --- a/src/Model/ExtDate.php +++ b/src/Model/ExtDate.php @@ -175,6 +175,23 @@ private function resolveMaxDay( $year, $month ): int { return null === $this->day ? $lastDayOfMonth : $this->day; } + public function getUnspecifiedYearScale() : int { + if ( $this->unspecifiedDigit->unspecified( 'year' ) ) { + $ret = $this->unspecifiedDigit->getYear(); + + if ( $this->getYear() === 0 ) { + return $ret; + } + + return $ret + 1; + } + return 0; + } + + public function getSpecifiedYears() : int { + return $this->getYear() / ( pow( 10, $this->unspecifiedDigit->getYear() ) ); + } + /** * This function is applicable for 2-digits placeholders (month, day). * Means that decimal: 0 < n < 10 From 57252e18ccd819c88c32e82bf2d0858a7773a612 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 00:38:47 +0400 Subject: [PATCH 05/19] implement scales --- .../Humanizer/InternationalizedHumanizer.php | 53 ++++++++++++------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php index f8a9bd7..9be744a 100644 --- a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php +++ b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php @@ -247,34 +247,51 @@ private function humanizeYearMonthDay( ?string $year, ?string $month, ?string $d ); } + private function scaleToMessageKey( int $scale ): string { + switch( $scale ) { + case 1 : return 'edtf-year'; // X + case 2 : return 'edtf-decade'; // XX + case 3 : return 'edtf-century'; // XXX + case 4 : return 'edtf-millennium'; // XXXX + case 5 : return 'edtf-decem-millennium'; // XXXXX + case 6 : return 'edtf-hundreds-of-thousands'; // XXX XXX + case 7 : return 'edtf-millions'; // XXX XXX + case 8 : return 'edtf-tens-of-millions'; // XXXXXXX + case 9 : return 'edtf-hundreds-of-millions'; // XXXXXXXX + case 10 : return 'edtf-billions'; // XXXXXXXXXX + case 11 : return 'edtf-tens-of-billions'; // XXXXXXXXXXX + case 12 : return 'edtf-hundreds-of-billions'; // XXXXXXXXXXXX + case 13 : return 'edtf-trillions'; // XXXXXXXXXXXXX + } + + // FIXME: reuse recursively the scale with trillions + // e.g. tens-of-trillions etc., + return 'edtf-tens-of-trillions'; + } + private function humanizeYear( int $year, ExtDate $date ): string { - $unspecifiedDigit = $date->getUnspecifiedDigit(); + $unspecifiedYearScale = $date->getUnspecifiedYearScale(); + $specifiedYears = $date->getSpecifiedYears(); - // *** if the expression $date->getDay() === null && $date->getMonth() === null - // is superfluous as it seems the function can be restored to its - // original declaration ( int $year, UnspecifiedDigit $unspecifiedDigit ) + $yearStr = (string)abs( $year ); - if ( $unspecifiedDigit->getYear() === 4 && $date->getDay() === null && $date->getMonth() === null ) { - return $this->message( 'edtf-year-unspecified' ); - } + $specifiedYearsStr = (string)abs( $specifiedYears ); - $yearStr = (string)abs( $year ); + $ret = ''; - if ( $year <= -1000 ) { - return $this->message( 'edtf-bc-year', $yearStr ); - } + $ret .= ( $specifiedYears === 0 ? $this->message( "edtf-date-unspecified" ) + : $specifiedYearsStr ); - if ( $year < 0 ) { - return $this->message( 'edtf-bc-year-short', $yearStr ); - } - $endingChar = $this->needsYearEndingChar( $unspecifiedDigit ) ? 's' : ''; + if ( $unspecifiedYearScale > 0 ) { + $ret .= " " . $this->message( $this->scaleToMessageKey( $unspecifiedYearScale ) ); + } - if ( $year < 1000 ) { - return $this->message( 'edtf-year-short', $yearStr . $endingChar ); + if ( $specifiedYears < 0 ) { + $ret .= " " . $this->message( "edtf-date-BC" ); } - return $yearStr . $endingChar; + return $ret; } /** From 35ee9fcde05282fc1cf2c5f70d41d0b77b71d931 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 00:40:14 +0400 Subject: [PATCH 06/19] implements scales --- i18n/en.json | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/i18n/en.json b/i18n/en.json index 4a0e6fb..582a3db 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -24,6 +24,23 @@ "edtf-autumn": "Autumn", "edtf-winter": "Winter", + "edtf-date-unspecified": "some", + "edtf-date-BC": "BC", + + "edtf-decade": "decade", + "edtf-century": "century", + "edtf-millennium": "millennium", + "edtf-decem-millennium": "decem millennium", + "edtf-hundreds-of-thousands": "hundreds of thousands", + "edtf-millions": "millions", + "edtf-tens-of-millions": "tens of million", + "edtf-hundreds-of-millions": "hundreds of million", + "edtf-billions": "billions", + "edtf-tens-of-billions": "tens of billions", + "edtf-hundreds-of-billions": "hundreds of billions", + "edtf-thousands-of-billions": "thousands of billions", + "edtf-trillions": "trillions", + "edtf-spring-north": "Spring (Northern Hemisphere)", "edtf-summer-north": "Summer (Northern Hemisphere)", "edtf-autumn-north": "Autumn (Northern Hemisphere)", From ee0bc9bc8b96ea3f3f9e6c84e0b896d5c5b1a50a Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 00:41:34 +0400 Subject: [PATCH 07/19] added comment --- src/PackagePrivate/Parser/RegexMatchesMapper.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/PackagePrivate/Parser/RegexMatchesMapper.php b/src/PackagePrivate/Parser/RegexMatchesMapper.php index 15e6fae..a768f79 100644 --- a/src/PackagePrivate/Parser/RegexMatchesMapper.php +++ b/src/PackagePrivate/Parser/RegexMatchesMapper.php @@ -42,6 +42,8 @@ private function mapDate( array $rawDateMatches ): Date { ); } + // FIXME: ensure that -XXXXXXX4 or -XXXXX4XX throws an error + // see https://github.com/ProfessionalWiki/EDTF/pull/88 private function prepareNumValue( string $str ): ?int { $value = (int)str_replace( 'X', '0', $str ); return $value !== 0 ? $value : null; @@ -89,4 +91,4 @@ private function regroupMatches( array $matches ): array { return $regrouped; } -} \ No newline at end of file +} From 95e812bb7a6802e3a01f0f54d34178e630f8c1ef Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 00:48:06 +0400 Subject: [PATCH 08/19] psalm error --- .../Humanizer/InternationalizedHumanizer.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php index 9be744a..e2aca83 100644 --- a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php +++ b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php @@ -270,11 +270,7 @@ private function scaleToMessageKey( int $scale ): string { } private function humanizeYear( int $year, ExtDate $date ): string { - $unspecifiedYearScale = $date->getUnspecifiedYearScale(); $specifiedYears = $date->getSpecifiedYears(); - - $yearStr = (string)abs( $year ); - $specifiedYearsStr = (string)abs( $specifiedYears ); $ret = ''; @@ -282,11 +278,14 @@ private function humanizeYear( int $year, ExtDate $date ): string { $ret .= ( $specifiedYears === 0 ? $this->message( "edtf-date-unspecified" ) : $specifiedYearsStr ); + $unspecifiedYearScale = $date->getUnspecifiedYearScale(); if ( $unspecifiedYearScale > 0 ) { $ret .= " " . $this->message( $this->scaleToMessageKey( $unspecifiedYearScale ) ); } + // TODO: retrieve negative values also for $specifiedYears === 0 + // so that -XXX is "some century BC" if ( $specifiedYears < 0 ) { $ret .= " " . $this->message( "edtf-date-BC" ); } From b5ca5fc18d26e9ddf645addc40aaa1ba38bbfabc Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 00:56:03 +0400 Subject: [PATCH 09/19] fix static error --- .../Humanizer/InternationalizedHumanizer.php | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php index e2aca83..f7cbb73 100644 --- a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php +++ b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php @@ -215,10 +215,7 @@ private function humanizeDateWithoutUncertainty( ExtDate $date ): string { $day = $date->getDay(); if ( $year !== null ) { - $year = $this->humanizeYear( - $year, - $date - ); + $year = $this->humanizeYear( $date ); } if ( $month !== null ) { @@ -254,10 +251,10 @@ private function scaleToMessageKey( int $scale ): string { case 3 : return 'edtf-century'; // XXX case 4 : return 'edtf-millennium'; // XXXX case 5 : return 'edtf-decem-millennium'; // XXXXX - case 6 : return 'edtf-hundreds-of-thousands'; // XXX XXX - case 7 : return 'edtf-millions'; // XXX XXX - case 8 : return 'edtf-tens-of-millions'; // XXXXXXX - case 9 : return 'edtf-hundreds-of-millions'; // XXXXXXXX + case 6 : return 'edtf-hundreds-of-thousands'; // XXXXXX + case 7 : return 'edtf-millions'; // XXXXXXX + case 8 : return 'edtf-tens-of-millions'; // XXXXXXXX + case 9 : return 'edtf-hundreds-of-millions'; // XXXXXXXXX case 10 : return 'edtf-billions'; // XXXXXXXXXX case 11 : return 'edtf-tens-of-billions'; // XXXXXXXXXXX case 12 : return 'edtf-hundreds-of-billions'; // XXXXXXXXXXXX @@ -269,13 +266,11 @@ private function scaleToMessageKey( int $scale ): string { return 'edtf-tens-of-trillions'; } - private function humanizeYear( int $year, ExtDate $date ): string { + private function humanizeYear( ExtDate $date ): string { $specifiedYears = $date->getSpecifiedYears(); $specifiedYearsStr = (string)abs( $specifiedYears ); - $ret = ''; - - $ret .= ( $specifiedYears === 0 ? $this->message( "edtf-date-unspecified" ) + $ret = ( $specifiedYears === 0 ? $this->message( "edtf-date-unspecified" ) : $specifiedYearsStr ); $unspecifiedYearScale = $date->getUnspecifiedYearScale(); From 35ad309b30f9f05c94dfe3fe6bdee111548a6562 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 01:35:04 +0400 Subject: [PATCH 10/19] test scales --- tests/Functional/EnglishHumanizationTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Functional/EnglishHumanizationTest.php b/tests/Functional/EnglishHumanizationTest.php index 5c121fc..03d831d 100644 --- a/tests/Functional/EnglishHumanizationTest.php +++ b/tests/Functional/EnglishHumanizationTest.php @@ -36,6 +36,22 @@ public function humanizationProvider(): Generator { // https://github.com/ProfessionalWiki/EDTF/issues/80 yield 'Some year' => [ 'XXXX', 'some year' ]; + yield 'Some year (4 digits)' => [ 'XXXX', 'some millennium' ]; + + yield 'Some year (1 digit)' => [ 'X', 'some year' ]; + yield 'Some year (3 digits)' => [ 'XXX', 'some century' ]; + yield 'Scales (4 digits minus)' => [ '-5XXXX', '5 decem millenniums BC' ]; + yield 'Scales (5 digits)' => [ '5XXXXX', '5 hundreds of thousands' ]; + yield 'Scales (6 digits)' => [ '5XXXXXX', '5 millions' ]; + yield 'Scales (7 digits)' => [ '5XXXXXXX', '5 tens of millions' ]; + yield 'Scales (8 digits)' => [ '5XXXXXXXX', '5 hundreds of millions' ]; + yield 'Scales (9 digits)' => [ '5XXXXXXXXX', '5 billions' ]; + yield 'Scales (10 digits)' => [ '5XXXXXXXXXX', '5 tens of billions' ]; + yield 'Scales (11 digits)' => [ '5XXXXXXXXXXX', '5 hundreds of billions' ]; + + // TODO throw error + // yield 'Scales (throw error 1)' => [ 'XXXXXXXXXX4', '' ]; + // yield 'Scales (throw error 1)' => [ 'XXXXXX4XXXX', '' ]; yield 'Month and day' => [ 'XXXX-12-11', 'December 11th' ]; yield 'Year and day' => [ '2020-XX-11', '11th of unknown month, 2020' ]; From 7d400663cdc9691c31545ed6154d9aa2a00a2add Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 01:47:27 +0400 Subject: [PATCH 11/19] fix scales --- i18n/en.json | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/i18n/en.json b/i18n/en.json index 582a3db..2d41424 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -27,19 +27,19 @@ "edtf-date-unspecified": "some", "edtf-date-BC": "BC", - "edtf-decade": "decade", - "edtf-century": "century", - "edtf-millennium": "millennium", - "edtf-decem-millennium": "decem millennium", - "edtf-hundreds-of-thousands": "hundreds of thousands", - "edtf-millions": "millions", - "edtf-tens-of-millions": "tens of million", - "edtf-hundreds-of-millions": "hundreds of million", - "edtf-billions": "billions", - "edtf-tens-of-billions": "tens of billions", - "edtf-hundreds-of-billions": "hundreds of billions", - "edtf-thousands-of-billions": "thousands of billions", - "edtf-trillions": "trillions", + "edtf-decade": "{{PLURAL:$1|decade|decades}}", + "edtf-century": "{{PLURAL:$1|century|centuries}}", + "edtf-millennium": "{{PLURAL:$1|millennium|millennia}}", + "edtf-decem-millennium": "decem {{PLURAL:$1|millennium|millennia}}", + "edtf-hundreds-of-thousands": "{{PLURAL:$1|hundred|hundreds}} of thousands", + "edtf-million": "{{PLURAL:$1|million|millions}}", + "edtf-tens-of-millions": "{{PLURAL:$1|ten|tens}} of millions", + "edtf-hundreds-of-millions": "{{PLURAL:$1|hundred|hundreds}} of millions", + "edtf-billion": "{{PLURAL:$1|billion|billions}}", + "edtf-tens-of-billions": "{{PLURAL:$1|ten|tens}} of billions", + "edtf-hundreds-of-billions": "{{PLURAL:$1|hundred|hundreds}} of billions", + "edtf-thousands-of-billions": "{{PLURAL:$1|thousand|thousands}} of billions", + "edtf-trillion": "{{PLURAL:$1|trillion|trillions}}", "edtf-spring-north": "Spring (Northern Hemisphere)", "edtf-summer-north": "Summer (Northern Hemisphere)", From bf5dbb423d85c0702aea2b3090a8e7d36551d290 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 01:48:02 +0400 Subject: [PATCH 12/19] fix scales test --- tests/Functional/EnglishHumanizationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Functional/EnglishHumanizationTest.php b/tests/Functional/EnglishHumanizationTest.php index 03d831d..635ef61 100644 --- a/tests/Functional/EnglishHumanizationTest.php +++ b/tests/Functional/EnglishHumanizationTest.php @@ -35,12 +35,12 @@ public function humanizationProvider(): Generator { yield 'Day only' => [ 'XXXX-XX-12', '12th' ]; // https://github.com/ProfessionalWiki/EDTF/issues/80 - yield 'Some year' => [ 'XXXX', 'some year' ]; yield 'Some year (4 digits)' => [ 'XXXX', 'some millennium' ]; yield 'Some year (1 digit)' => [ 'X', 'some year' ]; + yield 'Some year (2 digit)' => [ 'XX', 'some decade' ]; yield 'Some year (3 digits)' => [ 'XXX', 'some century' ]; - yield 'Scales (4 digits minus)' => [ '-5XXXX', '5 decem millenniums BC' ]; + yield 'Scales (4 digits minus)' => [ '-5XXXX', '5 decem millennia BC' ]; yield 'Scales (5 digits)' => [ '5XXXXX', '5 hundreds of thousands' ]; yield 'Scales (6 digits)' => [ '5XXXXXX', '5 millions' ]; yield 'Scales (7 digits)' => [ '5XXXXXXX', '5 tens of millions' ]; From bd86be438b5b87bc2f6955d580c152281d6e7a26 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 01:48:45 +0400 Subject: [PATCH 13/19] fix scales plural --- .../Humanizer/InternationalizedHumanizer.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php index f7cbb73..a490851 100644 --- a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php +++ b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php @@ -252,13 +252,13 @@ private function scaleToMessageKey( int $scale ): string { case 4 : return 'edtf-millennium'; // XXXX case 5 : return 'edtf-decem-millennium'; // XXXXX case 6 : return 'edtf-hundreds-of-thousands'; // XXXXXX - case 7 : return 'edtf-millions'; // XXXXXXX + case 7 : return 'edtf-million'; // XXXXXXX case 8 : return 'edtf-tens-of-millions'; // XXXXXXXX case 9 : return 'edtf-hundreds-of-millions'; // XXXXXXXXX - case 10 : return 'edtf-billions'; // XXXXXXXXXX + case 10 : return 'edtf-billion'; // XXXXXXXXXX case 11 : return 'edtf-tens-of-billions'; // XXXXXXXXXXX case 12 : return 'edtf-hundreds-of-billions'; // XXXXXXXXXXXX - case 13 : return 'edtf-trillions'; // XXXXXXXXXXXXX + case 13 : return 'edtf-trillion'; // XXXXXXXXXXXXX } // FIXME: reuse recursively the scale with trillions @@ -276,7 +276,7 @@ private function humanizeYear( ExtDate $date ): string { $unspecifiedYearScale = $date->getUnspecifiedYearScale(); if ( $unspecifiedYearScale > 0 ) { - $ret .= " " . $this->message( $this->scaleToMessageKey( $unspecifiedYearScale ) ); + $ret .= " " . $this->message( $this->scaleToMessageKey( $unspecifiedYearScale ), $specifiedYearsStr ); } // TODO: retrieve negative values also for $specifiedYears === 0 From 2daa0a351e17f482c151ec296fb9ee6c62859cca Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 02:12:17 +0400 Subject: [PATCH 14/19] fix BC value --- src/Model/ExtDate.php | 47 +++++-------------------------------------- 1 file changed, 5 insertions(+), 42 deletions(-) diff --git a/src/Model/ExtDate.php b/src/Model/ExtDate.php index ba68b51..76d0783 100644 --- a/src/Model/ExtDate.php +++ b/src/Model/ExtDate.php @@ -38,7 +38,7 @@ public function __construct( ?int $year = null, $this->month = $month; $this->day = $day; $this->year = $this->fixedYear( $year ); - $this->qualification = $qualification ?? Qualification::newFullyKnown(); + $this->qualification = $qualification ?? new Qualification(); $this->unspecifiedDigit = $unspecified ?? $this->newUnspecifiedDigit( $year, $month, $day ); $this->datetimeFactory = new CarbonFactory(); @@ -175,23 +175,6 @@ private function resolveMaxDay( $year, $month ): int { return null === $this->day ? $lastDayOfMonth : $this->day; } - public function getUnspecifiedYearScale() : int { - if ( $this->unspecifiedDigit->unspecified( 'year' ) ) { - $ret = $this->unspecifiedDigit->getYear(); - - if ( $this->getYear() === 0 ) { - return $ret; - } - - return $ret + 1; - } - return 0; - } - - public function getSpecifiedYears() : int { - return $this->getYear() / ( pow( 10, $this->unspecifiedDigit->getYear() ) ); - } - /** * This function is applicable for 2-digits placeholders (month, day). * Means that decimal: 0 < n < 10 @@ -268,26 +251,12 @@ private function maxDaysInMonth( int $year, int $month ): int { return $c->lastOfMonth()->day; } - /** - * @deprecated - */ - public function uncertain(): bool { - return $this->isUncertain(); - } - - public function isUncertain(): bool { - return $this->qualification->isUncertain(); + public function uncertain( ?string $part = null ): bool { + return $this->qualification->uncertain( $part ); } - /** - * @deprecated - */ - public function approximate(): bool { - return $this->isApproximate(); - } - - public function isApproximate(): bool { - return $this->qualification->isApproximate(); + public function approximate( ?string $part = null ): bool { + return $this->qualification->approximate( $part ); } public function unspecified( ?string $part = null ): bool { @@ -298,12 +267,6 @@ public function getQualification(): Qualification { return $this->qualification; } - public function isUniformlyQualified(): bool { - return $this->qualification->isUniform() // 2004-06-11? and ?2004-?06-?11 - || ( $this->qualification->monthAndYearHaveTheSameQualification() && $this->day === null ) // 2004-06? and ?2004-?06 - || ( $this->month === null && $this->day === null ); // 1984? - } - public function getUnspecifiedDigit(): UnspecifiedDigit { return $this->unspecifiedDigit; } From 0e58e88082b859ef112381562d7f93354b23e754 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 02:14:02 +0400 Subject: [PATCH 15/19] fix BC value (revert previous commit) --- src/Model/ExtDate.php | 51 ++++++++++++++++++++++++++++++---- src/Model/UnspecifiedDigit.php | 6 +++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/Model/ExtDate.php b/src/Model/ExtDate.php index 76d0783..f5292d8 100644 --- a/src/Model/ExtDate.php +++ b/src/Model/ExtDate.php @@ -38,7 +38,7 @@ public function __construct( ?int $year = null, $this->month = $month; $this->day = $day; $this->year = $this->fixedYear( $year ); - $this->qualification = $qualification ?? new Qualification(); + $this->qualification = $qualification ?? Qualification::newFullyKnown(); $this->unspecifiedDigit = $unspecified ?? $this->newUnspecifiedDigit( $year, $month, $day ); $this->datetimeFactory = new CarbonFactory(); @@ -175,6 +175,27 @@ private function resolveMaxDay( $year, $month ): int { return null === $this->day ? $lastDayOfMonth : $this->day; } + public function getUnspecifiedYearScale() : int { + if ( $this->unspecifiedDigit->unspecified( 'year' ) ) { + $ret = $this->unspecifiedDigit->getYear(); + + if ( $this->getYear() === 0 ) { + return $ret; + } + + return $ret + 1; + } + return 0; + } + + public function getSpecifiedYears() : int { + return $this->getYear() / ( pow( 10, $this->unspecifiedDigit->getYear() ) ); + } + + public function isBC(): bool { + return $this->unspecifiedDigit->isBC(); + } + /** * This function is applicable for 2-digits placeholders (month, day). * Means that decimal: 0 < n < 10 @@ -251,12 +272,26 @@ private function maxDaysInMonth( int $year, int $month ): int { return $c->lastOfMonth()->day; } - public function uncertain( ?string $part = null ): bool { - return $this->qualification->uncertain( $part ); + /** + * @deprecated + */ + public function uncertain(): bool { + return $this->isUncertain(); + } + + public function isUncertain(): bool { + return $this->qualification->isUncertain(); + } + + /** + * @deprecated + */ + public function approximate(): bool { + return $this->isApproximate(); } - public function approximate( ?string $part = null ): bool { - return $this->qualification->approximate( $part ); + public function isApproximate(): bool { + return $this->qualification->isApproximate(); } public function unspecified( ?string $part = null ): bool { @@ -267,6 +302,12 @@ public function getQualification(): Qualification { return $this->qualification; } + public function isUniformlyQualified(): bool { + return $this->qualification->isUniform() // 2004-06-11? and ?2004-?06-?11 + || ( $this->qualification->monthAndYearHaveTheSameQualification() && $this->day === null ) // 2004-06? and ?2004-?06 + || ( $this->month === null && $this->day === null ); // 1984? + } + public function getUnspecifiedDigit(): UnspecifiedDigit { return $this->unspecifiedDigit; } diff --git a/src/Model/UnspecifiedDigit.php b/src/Model/UnspecifiedDigit.php index 50ec03f..eba8314 100644 --- a/src/Model/UnspecifiedDigit.php +++ b/src/Model/UnspecifiedDigit.php @@ -76,6 +76,10 @@ public function getDay(): int { return $this->day; } + public function isBC(): bool { + return ( (int)str_replace( "X", 1, $this->rawYear ) < 0 ); + } + public function century(): bool { if ( $this->year == 2 && substr( $this->rawYear, -2 ) == "XX" ) { return true; @@ -91,4 +95,4 @@ public function decade(): bool { return false; } -} \ No newline at end of file +} From 234ebb4d17800dce2e016f186e6e4a17f63bbbf0 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 02:15:27 +0400 Subject: [PATCH 16/19] fix BC --- src/PackagePrivate/Humanizer/InternationalizedHumanizer.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php index a490851..421a0c4 100644 --- a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php +++ b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php @@ -279,9 +279,7 @@ private function humanizeYear( ExtDate $date ): string { $ret .= " " . $this->message( $this->scaleToMessageKey( $unspecifiedYearScale ), $specifiedYearsStr ); } - // TODO: retrieve negative values also for $specifiedYears === 0 - // so that -XXX is "some century BC" - if ( $specifiedYears < 0 ) { + if ( $date->isBC() ) { $ret .= " " . $this->message( "edtf-date-BC" ); } From d5697147b49cd59109d57bbc5b09ca5dd7a17479 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 02:25:06 +0400 Subject: [PATCH 17/19] fix static --- src/Model/UnspecifiedDigit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/UnspecifiedDigit.php b/src/Model/UnspecifiedDigit.php index eba8314..1745ce1 100644 --- a/src/Model/UnspecifiedDigit.php +++ b/src/Model/UnspecifiedDigit.php @@ -77,7 +77,7 @@ public function getDay(): int { } public function isBC(): bool { - return ( (int)str_replace( "X", 1, $this->rawYear ) < 0 ); + return ( (int)str_replace( "X", "1", $this->rawYear ) < 0 ); } public function century(): bool { From 0d6a4e6a8d2d0bf5f849eb5fc32dbaabe1bd830b Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 02:48:13 +0400 Subject: [PATCH 18/19] fix tests --- .../Humanizer/InternationalizedHumanizer.php | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php index 421a0c4..0a7d0cb 100644 --- a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php +++ b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php @@ -266,15 +266,21 @@ private function scaleToMessageKey( int $scale ): string { return 'edtf-tens-of-trillions'; } - private function humanizeYear( ExtDate $date ): string { + private function humanizeYear( ExtDate $date ): string { + $unspecifiedYearScale = $date->getUnspecifiedYearScale(); + $unspecifiedDigit = $date->getUnspecifiedDigit(); $specifiedYears = $date->getSpecifiedYears(); + + if ( $unspecifiedYearScale === 0 || + ( $this->needsYearEndingChar( $unspecifiedDigit ) && $specifiedYears !== 0 ) ) { + return $this->humanizeYearSpecified( $date->getYear(), $unspecifiedDigit ); + } + $specifiedYearsStr = (string)abs( $specifiedYears ); - $ret = ( $specifiedYears === 0 ? $this->message( "edtf-date-unspecified" ) + $ret = ( $specifiedYears === 0 && $unspecifiedYearScale != 0 ? $this->message( "edtf-date-unspecified" ) : $specifiedYearsStr ); - $unspecifiedYearScale = $date->getUnspecifiedYearScale(); - if ( $unspecifiedYearScale > 0 ) { $ret .= " " . $this->message( $this->scaleToMessageKey( $unspecifiedYearScale ), $specifiedYearsStr ); } @@ -286,6 +292,26 @@ private function humanizeYear( ExtDate $date ): string { return $ret; } + private function humanizeYearSpecified( int $year, UnspecifiedDigit $unspecifiedDigit ): string { + $yearStr = (string)abs( $year ); + + if ( $year <= -1000 ) { + return $this->message( 'edtf-bc-year', $yearStr ); + } + + if ( $year < 0 ) { + return $this->message( 'edtf-bc-year-short', $yearStr ); + } + + $endingChar = $this->needsYearEndingChar( $unspecifiedDigit ) ? 's' : ''; + + if ( $year < 1000 ) { + return $this->message( 'edtf-year-short', $yearStr . $endingChar ); + } + + return $yearStr . $endingChar; + } + /** * Check, do we need to add 's' char to humanized year representation * This can be applicable to unspecified years i.e. 197X or 19XX From edb98e45c219de611a6652bd93a6a087fd16b52e Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Wed, 18 Jan 2023 02:54:17 +0400 Subject: [PATCH 19/19] fix static --- src/PackagePrivate/Humanizer/InternationalizedHumanizer.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php index 0a7d0cb..da7aad2 100644 --- a/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php +++ b/src/PackagePrivate/Humanizer/InternationalizedHumanizer.php @@ -215,7 +215,7 @@ private function humanizeDateWithoutUncertainty( ExtDate $date ): string { $day = $date->getDay(); if ( $year !== null ) { - $year = $this->humanizeYear( $date ); + $year = $this->humanizeYear( $year, $date ); } if ( $month !== null ) { @@ -266,14 +266,14 @@ private function scaleToMessageKey( int $scale ): string { return 'edtf-tens-of-trillions'; } - private function humanizeYear( ExtDate $date ): string { + private function humanizeYear( int $year, ExtDate $date ): string { $unspecifiedYearScale = $date->getUnspecifiedYearScale(); $unspecifiedDigit = $date->getUnspecifiedDigit(); $specifiedYears = $date->getSpecifiedYears(); if ( $unspecifiedYearScale === 0 || ( $this->needsYearEndingChar( $unspecifiedDigit ) && $specifiedYears !== 0 ) ) { - return $this->humanizeYearSpecified( $date->getYear(), $unspecifiedDigit ); + return $this->humanizeYearSpecified( $year, $unspecifiedDigit ); } $specifiedYearsStr = (string)abs( $specifiedYears );