From 074e13325e57abcbd8f9965320d12618206954e5 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Tue, 12 Jan 2021 00:51:43 +0200 Subject: [PATCH] Fixed `Inflector` methods when translation is missing `GRAV.INFLECTOR_*` translations --- CHANGELOG.md | 1 + system/src/Grav/Common/Inflector.php | 54 ++++++++++++++++++---------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adcc24ebb..068a1c2d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * Fixed Clockwork missing dumped arrays and objects * Fixed fatal error in PHP 8 when trying to access root page * Fixed Array->String conversion error when `languages:translations: false` [admin#1896](https://github.com/getgrav/grav-plugin-admin/issues/1896) + * Fixed `Inflector` methods when translation is missing `GRAV.INFLECTOR_*` translations # v1.7.0-rc.20 ## 12/15/2020 diff --git a/system/src/Grav/Common/Inflector.php b/system/src/Grav/Common/Inflector.php index 7bcc35b76..7757d538b 100644 --- a/system/src/Grav/Common/Inflector.php +++ b/system/src/Grav/Common/Inflector.php @@ -13,6 +13,7 @@ use DateInterval; use DateTime; use Grav\Common\Language\Language; use function in_array; +use function is_array; use function strlen; /** @@ -64,21 +65,27 @@ class Inflector $lowercased_word = strtolower($word); - foreach (static::$uncountable as $_uncountable) { - if (substr($lowercased_word, -1 * strlen($_uncountable)) === $_uncountable) { - return $word; + if (is_array(static::$uncountable)) { + foreach (static::$uncountable as $_uncountable) { + if (substr($lowercased_word, -1 * strlen($_uncountable)) === $_uncountable) { + return $word; + } } } - foreach (static::$irregular as $_plural => $_singular) { - if (preg_match('/(' . $_plural . ')$/i', $word, $arr)) { - return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word); + if (is_array(static::$irregular)) { + foreach (static::$irregular as $_plural => $_singular) { + if (preg_match('/(' . $_plural . ')$/i', $word, $arr)) { + return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word); + } } } - foreach (static::$plural as $rule => $replacement) { - if (preg_match($rule, $word)) { - return preg_replace($rule, $replacement, $word); + if (is_array(static::$plural)) { + foreach (static::$plural as $rule => $replacement) { + if (preg_match($rule, $word)) { + return preg_replace($rule, $replacement, $word); + } } } @@ -102,21 +109,28 @@ class Inflector } $lowercased_word = strtolower($word); - foreach (static::$uncountable as $_uncountable) { - if (substr($lowercased_word, -1 * strlen($_uncountable)) === $_uncountable) { - return $word; + + if (is_array(static::$uncountable)) { + foreach (static::$uncountable as $_uncountable) { + if (substr($lowercased_word, -1 * strlen($_uncountable)) === $_uncountable) { + return $word; + } } } - foreach (static::$irregular as $_plural => $_singular) { - if (preg_match('/(' . $_singular . ')$/i', $word, $arr)) { - return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word); + if (is_array(static::$irregular)) { + foreach (static::$irregular as $_plural => $_singular) { + if (preg_match('/(' . $_singular . ')$/i', $word, $arr)) { + return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word); + } } } - foreach (static::$singular as $rule => $replacement) { - if (preg_match($rule, $word)) { - return preg_replace($rule, $replacement, $word); + if (is_array(static::$singular)) { + foreach (static::$singular as $rule => $replacement) { + if (preg_match($rule, $word)) { + return preg_replace($rule, $replacement, $word); + } } } @@ -291,6 +305,10 @@ class Inflector */ public static function ordinalize($number) { + if (!is_array(static::$ordinals)) { + return (string)$number; + } + static::init(); if (in_array($number % 100, range(11, 13), true)) {