-
Notifications
You must be signed in to change notification settings - Fork 8
/
currency.php
76 lines (62 loc) · 1.77 KB
/
currency.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
Currency plugin for the PHP Fat-Free Framework
The contents of this file are subject to the terms of the GNU General
Public License Version 3.0. You may not use this file except in
compliance with the license. Any of the license terms and conditions
can be waived if you get permission from the copyright holder.
Copyright (c) 2010-2011 Killsaw
Steven Bredenberg <[email protected]>
@package Currency
@version 1.0.0
**/
//! Plugin for currency conversion methods
class Currency extends Core {
//! Minimum framework version required to run
const F3_Minimum='1.4.0';
//@{
//! Locale-specific error/exception messages
const
TEXT_NoConversion='Currency could not be converted.',
TEXT_AmountNotNumber='Money amount is not a number.';
//@}
/**
Retrieve currency conversion from Google Finance.
@return bool|float
@param $amount float
@param $from string Currency to convert from. e.g. USD
@param $to string Currency to convert to. e.g. EUR
@public
**/
public static function convertAmount($amount, $from, $to, $quiet=false) {
$from = strtoupper($from);
$to = strtoupper($to);
if (!is_numeric($amount)) {
trigger_error(self::TEXT_AmountNotNumber);
return false;
}
// Workaround for empty ENV. Causes an issue in f3::http()
if (!isset($_ENV['OS'])) {
$_ENV['OS'] = 'Windows';
}
$data = f3::http(
"GET http://www.google.com/finance/converter",
http_build_query(
array(
'a'=>$amount,
'from'=>$from,
'to'=>$to
)
)
);
if (preg_match('/<span class=bld>(.+) (.+)<\/span>/', $data, $match)) {
if ($match[2] == $to) {
return (float)$match[1];
}
}
if (!$quiet) {
trigger_error(self::TEXT_NoConversion);
}
return false;
}
}