Text UI
A simple and fast way to take user input and display output in text mode using PHP.
Loading...
Searching...
No Matches
NumberEntry Class Reference
Inheritance diagram for NumberEntry:
EntryInterface

Public Member Functions

 __construct (string $label)
 
 setParser (?object $parser)
 
 setDecimalPoint (string $decimalPoint)
 
 setThousandsSep (string $thousandsSep)
 
 setDecimals (int $decimals)
 
 setOutputType (string $type)
 
 read ()
 
- Public Member Functions inherited from EntryInterface

Protected Member Functions

 defaultParser (string $entry)
 

Protected Attributes

readonly string $label
 
object $parser = null
 
string $decimalPoint
 
string $thousandsSep
 
string $outputType = 'float'
 
int $decimals = null
 

Detailed Description

Receives numbers from the user.

Accepts decimal places, thousands separator, decimal point and custom parser.

Author
everton3x

Definition at line 15 of file NumberEntry.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( string $label)

Definition at line 24 of file NumberEntry.php.

Member Function Documentation

◆ defaultParser()

defaultParser ( string $entry)
protected

Default parser and will always be applied unless a custom parser is provided by NumberEntry::setParser().

The default parser applies the following operations on user input (in the following order):

  1. Removes any characters that are not numbers, . or ,.
  2. Removes the thousands separator. It can be set by NumberEntry::setThousandsSep(), otherwise use the one obtained by localeconv() function.
  3. Replaces the decimal point set by NumberEntry::setDecimalPoint() (or the default obtained by localeconv()) with ..
  4. If the number of decimals is set by NumberEntry::setDecimals(), apply round().
  5. Converts the output to float or int as defined in NumberEntry::setOutputType() (default is float).
Parameters
string$entry
Returns
float|int

Definition at line 55 of file NumberEntry.php.

◆ read()

read ( )

Reads the user input.

Returns
float|int

Implements EntryInterface.

Definition at line 158 of file NumberEntry.php.

◆ setDecimalPoint()

setDecimalPoint ( string $decimalPoint)

Sets the decimal point.

Parameters
string$decimalPoint
Returns
NumberEntry

Definition at line 101 of file NumberEntry.php.

◆ setDecimals()

setDecimals ( int $decimals)

Sets a decimal places.

Parameters
int$decimals
Returns
NumberEntry

Definition at line 125 of file NumberEntry.php.

◆ setOutputType()

setOutputType ( string $type)

Sets the output type.

Parameters
string$typeOnly float|double|int|integer allowed.
Returns
NumberEntry
Exceptions
InvalidArgumentException

Definition at line 139 of file NumberEntry.php.

◆ setParser()

setParser ( ?object $parser)

Sets a parser.

$parser must be a callable type that will be executed as NumberEntry::parser() and must receive as its only argument the user's string and return a float or int.

Parameters
null | object$parser
Returns
NumberEntry

Definition at line 89 of file NumberEntry.php.

◆ setThousandsSep()

setThousandsSep ( string $thousandsSep)

Sets a thousands separator.

Parameters
string$thousandsSep
Returns
NumberEntry

Definition at line 113 of file NumberEntry.php.

Field Documentation

◆ $decimalPoint

string $decimalPoint
protected

Definition at line 19 of file NumberEntry.php.

◆ $decimals

int $decimals = null
protected

Definition at line 22 of file NumberEntry.php.

◆ $label

readonly string $label
protected

Definition at line 17 of file NumberEntry.php.

◆ $outputType

string $outputType = 'float'
protected

Definition at line 21 of file NumberEntry.php.

◆ $parser

object $parser = null
protected

Definition at line 18 of file NumberEntry.php.

◆ $thousandsSep

string $thousandsSep
protected

Definition at line 20 of file NumberEntry.php.


The documentation for this class was generated from the following file: