From d933c9eb04752496124ef4a5f5df6ffbaf0a1d87 Mon Sep 17 00:00:00 2001 From: George Petculescu Date: Thu, 20 Oct 2016 00:52:50 +0300 Subject: added ordinal_format() to Number helper; added to docs the info. --- system/helpers/number_helper.php | 29 +++++++++++++++++++++++++ user_guide_src/source/helpers/number_helper.rst | 17 ++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/system/helpers/number_helper.php b/system/helpers/number_helper.php index e7810c706..8e77c91ab 100644 --- a/system/helpers/number_helper.php +++ b/system/helpers/number_helper.php @@ -92,3 +92,32 @@ if ( ! function_exists('byte_format')) return number_format($num, $precision).' '.$unit; } } + +// ------------------------------------------------------------------------ + +if ( ! function_exists('ordinal_format')) +{ + /** + * Returns the English ordinal numeral for a given number + * + * @param int $number + * @return string + */ + function ordinal_format($number) + { + if ( ! is_int($number) OR $number < 1) + { + return FALSE; + } + + $ends = array('th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'); + if ((($number % 100) >= 11) && (($number % 100) <= 13)) + { + return $number.'th'; + } + else + { + return $number.$ends[$number % 10]; + } + } +} diff --git a/user_guide_src/source/helpers/number_helper.rst b/user_guide_src/source/helpers/number_helper.rst index 9d5e98cfb..20acdec18 100644 --- a/user_guide_src/source/helpers/number_helper.rst +++ b/user_guide_src/source/helpers/number_helper.rst @@ -49,4 +49,19 @@ The following functions are available: echo byte_format(45678, 2); // Returns 44.61 KB .. note:: The text generated by this function is found in the following - language file: *language//number_lang.php* \ No newline at end of file + language file: *language//number_lang.php* + +.. php:function:: ordinal_format($number) + + :param int $number: Non-negative natural number to be converted + :returns: Ordinal numeral for given number or FALSE on failure + :rtype: string + + Returns the ordinal numeral (1st, 2nd, 3rd etc.) for a + non-negative natural number. If the input is not a natural number + greater than 0, the function will return boolean FALSE. Examples:: + + echo ordinal_format(1); // Returns 1st + echo ordinal_format(3); // Returns 3rd + echo ordinal_format(21); // Returns 21st + echo ordinal_format(102); // Returns 102nd -- cgit v1.2.3-24-g4f1b From 433ebc5814cda44a93664812f28e030f61732564 Mon Sep 17 00:00:00 2001 From: gxgpet Date: Thu, 20 Oct 2016 11:54:19 +0300 Subject: Accepting strings too, 0 allowed. --- system/helpers/number_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/helpers/number_helper.php b/system/helpers/number_helper.php index 8e77c91ab..d7b96c322 100644 --- a/system/helpers/number_helper.php +++ b/system/helpers/number_helper.php @@ -105,7 +105,7 @@ if ( ! function_exists('ordinal_format')) */ function ordinal_format($number) { - if ( ! is_int($number) OR $number < 1) + if ( ! is_numeric($number) OR $number < 0) { return FALSE; } -- cgit v1.2.3-24-g4f1b From 4b2b27199025d3c5a6f9b1eee19340c45ec7f2a2 Mon Sep 17 00:00:00 2001 From: gxgpet Date: Thu, 20 Oct 2016 11:56:03 +0300 Subject: - fixed docs --- user_guide_src/source/helpers/number_helper.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/user_guide_src/source/helpers/number_helper.rst b/user_guide_src/source/helpers/number_helper.rst index 20acdec18..47f616690 100644 --- a/user_guide_src/source/helpers/number_helper.rst +++ b/user_guide_src/source/helpers/number_helper.rst @@ -53,13 +53,13 @@ The following functions are available: .. php:function:: ordinal_format($number) - :param int $number: Non-negative natural number to be converted + :param int $number: natural number to be converted :returns: Ordinal numeral for given number or FALSE on failure :rtype: string - Returns the ordinal numeral (1st, 2nd, 3rd etc.) for a - non-negative natural number. If the input is not a natural number - greater than 0, the function will return boolean FALSE. Examples:: + Returns the ordinal numeral (0th, 1st, 2nd, 3rd etc.) for a + natural number. If the input is not a natural number, the + function will return boolean FALSE. Examples:: echo ordinal_format(1); // Returns 1st echo ordinal_format(3); // Returns 3rd -- cgit v1.2.3-24-g4f1b From 7534aa85c2c8a4359f6df0a6f421fb15ca981877 Mon Sep 17 00:00:00 2001 From: gxgpet Date: Thu, 20 Oct 2016 12:19:48 +0300 Subject: small refactorisations --- system/helpers/number_helper.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/system/helpers/number_helper.php b/system/helpers/number_helper.php index d7b96c322..e94a5ffbc 100644 --- a/system/helpers/number_helper.php +++ b/system/helpers/number_helper.php @@ -110,14 +110,24 @@ if ( ! function_exists('ordinal_format')) return FALSE; } - $ends = array('th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'); + $last_digit = array( + 0 => 'th', + 1 => 'st', + 2 => 'nd', + 3 => 'rd', + 4 => 'th', + 5 => 'th', + 6 => 'th', + 7 => 'th', + 8 => 'th', + 9 => 'th'); if ((($number % 100) >= 11) && (($number % 100) <= 13)) { return $number.'th'; } else { - return $number.$ends[$number % 10]; + return $number.$last_digit[$number % 10]; } } } -- cgit v1.2.3-24-g4f1b From 1ff88001bead0ff4187a5b121d9d36a25a45c313 Mon Sep 17 00:00:00 2001 From: gxgpet Date: Thu, 20 Oct 2016 12:21:05 +0300 Subject: fixed floating for ordinal_format() --- system/helpers/number_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/helpers/number_helper.php b/system/helpers/number_helper.php index e94a5ffbc..af8b70f86 100644 --- a/system/helpers/number_helper.php +++ b/system/helpers/number_helper.php @@ -105,7 +105,7 @@ if ( ! function_exists('ordinal_format')) */ function ordinal_format($number) { - if ( ! is_numeric($number) OR $number < 0) + if ( ! ctype_digit((string) $number) OR $number < 0) { return FALSE; } -- cgit v1.2.3-24-g4f1b From 7de46d4d99017b42ba172cebff8998163ee6295f Mon Sep 17 00:00:00 2001 From: gxgpet Date: Thu, 20 Oct 2016 12:31:44 +0300 Subject: fixed coding style + removal of extra paranthesis --- system/helpers/number_helper.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/system/helpers/number_helper.php b/system/helpers/number_helper.php index af8b70f86..20e262e66 100644 --- a/system/helpers/number_helper.php +++ b/system/helpers/number_helper.php @@ -120,8 +120,9 @@ if ( ! function_exists('ordinal_format')) 6 => 'th', 7 => 'th', 8 => 'th', - 9 => 'th'); - if ((($number % 100) >= 11) && (($number % 100) <= 13)) + 9 => 'th' + ); + if (($number % 100) >= 11 && ($number % 100) <= 13) { return $number.'th'; } -- cgit v1.2.3-24-g4f1b From 11954d2521475f6964bfa9c65dcd2f3e53a65af5 Mon Sep 17 00:00:00 2001 From: George Petculescu Date: Thu, 20 Oct 2016 20:53:11 +0300 Subject: moved ordinal_format() helper function from Number helper to Inflector helper --- system/helpers/inflector_helper.php | 40 +++++++++++++++++++++ system/helpers/number_helper.php | 42 +--------------------- user_guide_src/source/helpers/inflector_helper.rst | 17 ++++++++- user_guide_src/source/helpers/number_helper.rst | 17 +-------- 4 files changed, 58 insertions(+), 58 deletions(-) diff --git a/system/helpers/inflector_helper.php b/system/helpers/inflector_helper.php index 6dc3b5030..c71516564 100644 --- a/system/helpers/inflector_helper.php +++ b/system/helpers/inflector_helper.php @@ -273,3 +273,43 @@ if ( ! function_exists('is_countable')) ); } } + +// ------------------------------------------------------------------------ + +if ( ! function_exists('ordinal_format')) +{ + /** + * Returns the English ordinal numeral for a given number + * + * @param int $number + * @return string + */ + function ordinal_format($number) + { + if ( ! ctype_digit((string) $number) OR $number < 0) + { + return FALSE; + } + + $last_digit = array( + 0 => 'th', + 1 => 'st', + 2 => 'nd', + 3 => 'rd', + 4 => 'th', + 5 => 'th', + 6 => 'th', + 7 => 'th', + 8 => 'th', + 9 => 'th' + ); + if (($number % 100) >= 11 && ($number % 100) <= 13) + { + return $number.'th'; + } + else + { + return $number.$last_digit[$number % 10]; + } + } +} \ No newline at end of file diff --git a/system/helpers/number_helper.php b/system/helpers/number_helper.php index 20e262e66..219124cb5 100644 --- a/system/helpers/number_helper.php +++ b/system/helpers/number_helper.php @@ -91,44 +91,4 @@ if ( ! function_exists('byte_format')) return number_format($num, $precision).' '.$unit; } -} - -// ------------------------------------------------------------------------ - -if ( ! function_exists('ordinal_format')) -{ - /** - * Returns the English ordinal numeral for a given number - * - * @param int $number - * @return string - */ - function ordinal_format($number) - { - if ( ! ctype_digit((string) $number) OR $number < 0) - { - return FALSE; - } - - $last_digit = array( - 0 => 'th', - 1 => 'st', - 2 => 'nd', - 3 => 'rd', - 4 => 'th', - 5 => 'th', - 6 => 'th', - 7 => 'th', - 8 => 'th', - 9 => 'th' - ); - if (($number % 100) >= 11 && ($number % 100) <= 13) - { - return $number.'th'; - } - else - { - return $number.$last_digit[$number % 10]; - } - } -} +} \ No newline at end of file diff --git a/user_guide_src/source/helpers/inflector_helper.rst b/user_guide_src/source/helpers/inflector_helper.rst index df0c568c0..8a6ca7a92 100644 --- a/user_guide_src/source/helpers/inflector_helper.rst +++ b/user_guide_src/source/helpers/inflector_helper.rst @@ -93,4 +93,19 @@ The following functions are available: Checks if the given word has a plural version. Example:: - is_countable('equipment'); // Returns FALSE \ No newline at end of file + is_countable('equipment'); // Returns FALSE + +.. php:function:: ordinal_format($number) + + :param int $number: natural number to be converted + :returns: Ordinal numeral for given number or FALSE on failure + :rtype: string + + Returns the ordinal numeral (0th, 1st, 2nd, 3rd etc.) for a + natural number. If the input is not a natural number, the + function will return boolean FALSE. Examples:: + + echo ordinal_format(1); // Returns 1st + echo ordinal_format(3); // Returns 3rd + echo ordinal_format(21); // Returns 21st + echo ordinal_format(102); // Returns 102nd diff --git a/user_guide_src/source/helpers/number_helper.rst b/user_guide_src/source/helpers/number_helper.rst index 47f616690..9d5e98cfb 100644 --- a/user_guide_src/source/helpers/number_helper.rst +++ b/user_guide_src/source/helpers/number_helper.rst @@ -49,19 +49,4 @@ The following functions are available: echo byte_format(45678, 2); // Returns 44.61 KB .. note:: The text generated by this function is found in the following - language file: *language//number_lang.php* - -.. php:function:: ordinal_format($number) - - :param int $number: natural number to be converted - :returns: Ordinal numeral for given number or FALSE on failure - :rtype: string - - Returns the ordinal numeral (0th, 1st, 2nd, 3rd etc.) for a - natural number. If the input is not a natural number, the - function will return boolean FALSE. Examples:: - - echo ordinal_format(1); // Returns 1st - echo ordinal_format(3); // Returns 3rd - echo ordinal_format(21); // Returns 21st - echo ordinal_format(102); // Returns 102nd + language file: *language//number_lang.php* \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 4fe5e99f437fe79cc40cebd08277eb319d8a6095 Mon Sep 17 00:00:00 2001 From: George Petculescu Date: Thu, 20 Oct 2016 20:58:10 +0300 Subject: fixed unnecessary file termination for Number helper --- system/helpers/number_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/helpers/number_helper.php b/system/helpers/number_helper.php index 219124cb5..e7810c706 100644 --- a/system/helpers/number_helper.php +++ b/system/helpers/number_helper.php @@ -91,4 +91,4 @@ if ( ! function_exists('byte_format')) return number_format($num, $precision).' '.$unit; } -} \ No newline at end of file +} -- cgit v1.2.3-24-g4f1b From 03a1eac3b7bed8ed6df59109364a3eebe3f1cceb Mon Sep 17 00:00:00 2001 From: George Petculescu Date: Thu, 20 Oct 2016 21:03:38 +0300 Subject: ordinal_format will accept only non-negative natural numbers and return original value on failure + docs update regarding this. --- system/helpers/inflector_helper.php | 4 ++-- user_guide_src/source/helpers/inflector_helper.rst | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/system/helpers/inflector_helper.php b/system/helpers/inflector_helper.php index c71516564..04e178aec 100644 --- a/system/helpers/inflector_helper.php +++ b/system/helpers/inflector_helper.php @@ -286,9 +286,9 @@ if ( ! function_exists('ordinal_format')) */ function ordinal_format($number) { - if ( ! ctype_digit((string) $number) OR $number < 0) + if ( ! ctype_digit((string) $number) OR $number < 1) { - return FALSE; + return $number; } $last_digit = array( diff --git a/user_guide_src/source/helpers/inflector_helper.rst b/user_guide_src/source/helpers/inflector_helper.rst index 8a6ca7a92..76cce6f4d 100644 --- a/user_guide_src/source/helpers/inflector_helper.rst +++ b/user_guide_src/source/helpers/inflector_helper.rst @@ -97,15 +97,16 @@ The following functions are available: .. php:function:: ordinal_format($number) - :param int $number: natural number to be converted - :returns: Ordinal numeral for given number or FALSE on failure + :param int $number: non-negative natural number to be converted + :returns: Ordinal numeral for given number or original value on failure :rtype: string - Returns the ordinal numeral (0th, 1st, 2nd, 3rd etc.) for a - natural number. If the input is not a natural number, the - function will return boolean FALSE. Examples:: + Returns the ordinal numeral (1st, 2nd, 3rd etc.) for a + non-negative natural number. If the input is not a natural number + greater than 0, the function will return the original value. Examples:: echo ordinal_format(1); // Returns 1st echo ordinal_format(3); // Returns 3rd echo ordinal_format(21); // Returns 21st echo ordinal_format(102); // Returns 102nd + echo ordinal_format(-5); // Invalid input, will return -5 -- cgit v1.2.3-24-g4f1b From 25dc0937dd27c6d3c9f4d4483a857c218d6a9dad Mon Sep 17 00:00:00 2001 From: gxgpet Date: Fri, 21 Oct 2016 17:44:03 +0300 Subject: fixed small coding style issues --- system/helpers/inflector_helper.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/system/helpers/inflector_helper.php b/system/helpers/inflector_helper.php index 04e178aec..83bf28098 100644 --- a/system/helpers/inflector_helper.php +++ b/system/helpers/inflector_helper.php @@ -303,13 +303,12 @@ if ( ! function_exists('ordinal_format')) 8 => 'th', 9 => 'th' ); + if (($number % 100) >= 11 && ($number % 100) <= 13) { return $number.'th'; } - else - { - return $number.$last_digit[$number % 10]; - } + + return $number.$last_digit[$number % 10]; } -} \ No newline at end of file +} -- cgit v1.2.3-24-g4f1b