From ce28b835093c9b0f2c1dd078f86e58ca180be191 Mon Sep 17 00:00:00 2001 From: Martin Joiner Date: Sat, 6 Feb 2016 20:51:44 +0000 Subject: [PATCH] Moved appendAnd argument away from constructor and back to convert() method --- README.md | 32 +++++++++++++++++++++++++------- src/OrdinalNumber.php | 19 +++++-------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index bb72ec5..b714886 100755 --- a/README.md +++ b/README.md @@ -3,26 +3,44 @@ Ordinal Number A PHP package for converting numbers to a human readable sentence of it's ordinal form eg. _'first'_, _'second'_ or even _'Three thousand five hundred and sixty first'_ -Supports numbers in the range 1-9999. +Latest release supports numbers in the range 1 - 9999. -## Usage +## Installation Recomended installation via Composer: ``` - composer require martinjoiner/ordinal-number + composer require martinjoiner/ordinal-number @dev ``` See library page on Packagist https://packagist.org/packages/martinjoiner/ordinal-number -Use the OrdinalNumber class in your PHP code as follows: + +## Usage example ```php - $ordinal = new OrdinalNumber( true ); - // The following line will set the value or $ordinalForm to the string 'three hundred and seventy eighth' - $ordinalForm = $ordinal->convert( 378 ); + // Tell our code to use the namespace + use MartinJoiner\OrdinalNumber\OrdinalNumber; + + // Define an instance of the OrdinalNumber class + $ordinal = new OrdinalNumber(); + + // The following line will output 'three hundred seventy eighth' + print $ordinal->convert( 378 ); + + // The following line will output 'three hundred and seventy eighth' + print $ordinal->convert( 378, true ); + + // The following line will output 'Three hundred and seventy eighth' (notice capitalised) + print $ordinal->convert( 378, true, true ); ``` +## convert() method parameters + +### Required parameters + +* num {integer} A number to be converted (in the range of 1 - 9999) + ### Optional parameters * appendAnd {boolean} - Default: _false_ - Places the word 'and' before the final 2 parts if number above 101 or higher (eg. One hundred and first). Added to support both American and European versions of English language. diff --git a/src/OrdinalNumber.php b/src/OrdinalNumber.php index ca98db4..1c44c81 100755 --- a/src/OrdinalNumber.php +++ b/src/OrdinalNumber.php @@ -7,20 +7,10 @@ */ class OrdinalNumber{ - protected $numWords = array( "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ); - - protected $appendAnd = false; - - - /** - * Constructor - * - * @param {boolean} $appendAnd Default: false - Places an 'and' before the final 2 parts if number above 101 or higher (eg. One hundred and first) + * @var {array} The word equivilent of our first 9 numbers */ - public function __construct( $appendAnd = false ){ - $this->appendAnd = $appendAnd; - } + protected $numWords = array( "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ); @@ -28,11 +18,12 @@ public function __construct( $appendAnd = false ){ * Converts a number to a sentence * * @param {integer} $num An integer to be converted + * @param {boolean} $appendAnd Default: false - Places an 'and' before the final 2 parts if number above 101 or higher (eg. One hundred and first) * @param {boolean} $titleCase - Boolean - Default: false - Capitalises the first letter * * @return {string} */ - public function convert( $num, $titleCase = false ){ + public function convert( $num, $appendAnd = false, $titleCase = false ){ $strReturn = ''; $strNum = (string)$num; @@ -54,7 +45,7 @@ public function convert( $num, $titleCase = false ){ if( $this->right($strNum,2) != "00" ){ // Does the user want the "and" appended before the words that represent the last 2 digits? - if( $num > 100 && $this->appendAnd ){ + if( $num > 100 && $appendAnd ){ $strReturn .= ' and '; }