Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F98020883
PhortuneCurrency.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Jan 8, 18:30
Size
3 KB
Mime Type
text/x-php
Expires
Fri, Jan 10, 18:30 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
23482989
Attached To
rPH Phabricator
PhortuneCurrency.php
View Options
<?php
final
class
PhortuneCurrency
extends
Phobject
{
private
$value
;
private
$currency
;
private
function
__construct
()
{
// Intentionally private.
}
public
static
function
getDefaultCurrency
()
{
return
'USD'
;
}
public
static
function
newEmptyCurrency
()
{
return
self
::
newFromString
(
'0.00 USD'
);
}
public
static
function
newFromUserInput
(
PhabricatorUser
$user
,
$string
)
{
// Eventually, this might select a default currency based on user settings.
return
self
::
newFromString
(
$string
,
self
::
getDefaultCurrency
());
}
public
static
function
newFromString
(
$string
,
$default
=
null
)
{
$matches
=
null
;
$ok
=
preg_match
(
'/^([-$]*(?:
\d
+)?(?:[.]
\d
{0,2})?)(?:
\s
+([A-Z]+))?$/'
,
trim
(
$string
),
$matches
);
if
(!
$ok
)
{
self
::
throwFormatException
(
$string
);
}
$value
=
$matches
[
1
];
if
(
substr_count
(
$value
,
'-'
)
>
1
)
{
self
::
throwFormatException
(
$string
);
}
if
(
substr_count
(
$value
,
'$'
)
>
1
)
{
self
::
throwFormatException
(
$string
);
}
$value
=
str_replace
(
'$'
,
''
,
$value
);
$value
=
(
float
)
$value
;
$value
=
(
int
)
round
(
100
*
$value
);
$currency
=
idx
(
$matches
,
2
,
$default
);
if
(
$currency
)
{
switch
(
$currency
)
{
case
'USD'
:
break
;
default
:
throw
new
Exception
(
"Unsupported currency '{$currency}'!"
);
}
}
return
self
::
newFromValueAndCurrency
(
$value
,
$currency
);
}
public
static
function
newFromValueAndCurrency
(
$value
,
$currency
)
{
$obj
=
new
PhortuneCurrency
();
$obj
->
value
=
$value
;
$obj
->
currency
=
$currency
;
return
$obj
;
}
public
static
function
newFromList
(
array
$list
)
{
assert_instances_of
(
$list
,
'PhortuneCurrency'
);
$total
=
0
;
$currency
=
null
;
foreach
(
$list
as
$item
)
{
if
(
$currency
===
null
)
{
$currency
=
$item
->
getCurrency
();
}
else
if
(
$currency
===
$item
->
getCurrency
())
{
// Adding a value denominated in the same currency, which is
// fine.
}
else
{
throw
new
Exception
(
pht
(
'Trying to sum a list of unlike currencies.'
));
}
// TODO: This should check for integer overflows, etc.
$total
+=
$item
->
getValue
();
}
return
PhortuneCurrency
::
newFromValueAndCurrency
(
$total
,
self
::
getDefaultCurrency
());
}
public
function
formatForDisplay
()
{
$bare
=
$this
->
formatBareValue
();
return
'$'
.
$bare
.
' '
.
$this
->
currency
;
}
public
function
serializeForStorage
()
{
return
$this
->
formatBareValue
().
' '
.
$this
->
currency
;
}
public
function
formatBareValue
()
{
switch
(
$this
->
currency
)
{
case
'USD'
:
return
sprintf
(
'%.02f'
,
$this
->
value
/
100
);
default
:
throw
new
Exception
(
pht
(
'Unsupported currency ("%s")!'
,
$this
->
currency
));
}
}
public
function
getValue
()
{
return
$this
->
value
;
}
public
function
getCurrency
()
{
return
$this
->
currency
;
}
private
static
function
throwFormatException
(
$string
)
{
throw
new
Exception
(
"Invalid currency format ('{$string}')."
);
}
}
Event Timeline
Log In to Comment