Skip to content

Commit

Permalink
Moved appendAnd argument away from constructor and back to convert() …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
martinjoiner committed Feb 6, 2016
1 parent 9362295 commit ce28b83
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 5 additions & 14 deletions src/OrdinalNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,23 @@
*/
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" );



/**
* 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;
Expand All @@ -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 ';
}

Expand Down

0 comments on commit ce28b83

Please sign in to comment.