Gumbo PHP Library API Documentation
Number
[
class tree
] [
index
] [
all elements
]
Todo List
Packages:
Buffer
Collection
Composite
Config
Converter
Curl
Date
DB
Debug
Encryption
Error
Factory
Filter
Flyweight
Http
Input
Iterator
List
Load
Lockable
Log
Map
Number
Observer
Output
Query
Record
Router
Session
Setting
Singleton
Template
Timer
Utility
Valid
Source for file Number.class.php
Documentation is available at
Number.class.php
<?php
/**
* Gumbo Library Framework
*
* LICENSE
* This library is being released under the terms of the New BSD License. A
* copy of the license is packaged with the software (LICENSE.txt). If no
* copy is found, a copy of the license template can be found at:
* http://www.opensource.org/licenses/bsd-license.php
*
*
@category
Gumbo
*
@package
Number
*
@copyright
Copyright (c) 2007, iBayou, Michael Luster
*
@license
http://www.opensource.org/licenses/bsd-license.php New BSD License
*
@author
Michael Luster <mluster79@yahoo.com>
*
@link
http://sourceforge.net/projects/phpgumbo
*
@version
1.0.0
*/
/**
* Number Class
*
* The current implementation only provides static methods, performing simple
* operations dealing with Numbers. This class will be changed in the future
* into something more interesting. For now, it performs simple tasks.
*
*
@category
Gumbo
*
@package
Number
*
@copyright
Copyright (c) 2007, iBayou, Michael Luster
*
@license
http://www.opensource.org/licenses/bsd-license.php New BSD License
*
@author
Michael Luster <mluster79@yahoo.com>
*
@link
http://sourceforge.net/projects/phpgumbo
*
@desc
Number Class
*
@version
1.0.0
*/
class
Gumbo_Number
{
/**
* Returns the suffix for a given number
*
@param
int
$num
*
@return
string
*
@throws
Gumbo_Exception
*/
public
static
function
getSuffix
(
$num
)
{
$suffix
=
null
;
try
{
// verify precondition
if
(
!
is_int
(
$num
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
num
:
int
' => {
$num
}
:
"
.
gettype
(
$num
))
;
}
// retrieve last digit
if
(
strlen
(
$num
)
>
1
)
{
$num
=
substr
(
$num
,
strlen
(
$num
)
-
1
)
;
}
$suffix
=
false
;
switch
(
$num
)
{
case
0
:
$suffix
=
"th"
;
break
;
case
1
:
$suffix
=
"st"
;
break
;
case
2
:
$suffix
=
"nd"
;
break
;
case
3
:
$suffix
=
"rd"
;
break
;
case
4
:
$suffix
=
"th"
;
break
;
case
5
:
$suffix
=
"th"
;
break
;
case
6
:
$suffix
=
"th"
;
break
;
case
7
:
$suffix
=
"th"
;
break
;
case
8
:
$suffix
=
"th"
;
break
;
case
9
:
$suffix
=
"th"
;
break
;
}
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
return
$suffix
;
}
/**
* Returns a randomly generated number x digits long
*
@param
int
$digits
number of digits
*
@return
int
*
@throws
Gumbo_Exception
*/
public
static
function
random
(
$digits
=
1
)
{
try
{
if
(
!
is_numeric
(
$digits
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
digits
:
int
' => {
$digits
}
:
"
.
gettype
(
$digits
))
;
}
if
(
$digits
<
1
)
{
throw
new
Gumbo_Exception
(
"
Argument
Must
Be
Greater
Than
One
: {
$digits
}
"
)
;
}
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
$digits
=
1
;
}
$digits
= (int)
$digits
;
$pool
=
"0123456789"
;
$len
=
strlen
(
$pool
)
-
1
;
$num
=
0
;
for
(
$x
=
0
;
$x
<
$digits
;
$x
++
)
{
$num
.=
$pool
{
mt_rand
(
0
,
$len
)
}
;
}
return
(int)
$num
;
}
/**
* Returns a randomly generated number
*
@param
int
$min
*
@param
int
$max
*
@return
int
*
@throws
Gumbo_Exception
*/
public
static
function
rand
(
$min
=
null
,
$max
=
null
)
{
try
{
if
(
!
is_null
(
$min
)
&&
!
is_numeric
(
$min
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
min
:
int
|
null
' => {
$min
}
:
"
.
gettype
(
$min
))
;
}
if
(
!
is_null
(
$max
)
&&
!
is_numeric
(
$max
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
max
:
int
|
null
' => {
$max
}
:
"
.
gettype
(
$max
))
;
}
return
mt_rand
(
$min
,
$max
)
;
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
return
0
;
}
}
?>