|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OfxParser; |
|
4
|
|
|
|
|
5
|
|
|
use SimpleXMLElement; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Utilities and helpers for conversion and parsing. |
|
9
|
|
|
*/ |
|
10
|
|
|
class Utils |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Set this to define your own DateTime creation function. |
|
14
|
|
|
* function('YYYY-MM-DD HH:ii:ss') : \DateTime compatible object |
|
15
|
|
|
* @static callable |
|
16
|
|
|
*/ |
|
17
|
|
|
public static $fnDateTimeFactory; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Create a DateTime object from a valid OFX date format |
|
21
|
|
|
* |
|
22
|
|
|
* Supports: |
|
23
|
|
|
* YYYYMMDDHHMMSS.XXX[gmt offset:tz name] |
|
24
|
|
|
* YYYYMMDDHHMMSS.XXX |
|
25
|
|
|
* YYYYMMDDHHMMSS |
|
26
|
|
|
* YYYYMMDD |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $dateString |
|
29
|
|
|
* @param boolean $ignoreErrors |
|
30
|
|
|
* @return \DateTime $dateString |
|
31
|
|
|
* @throws \Exception |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public static function createDateTimeFromStr($dateString, $ignoreErrors = false) |
|
34
|
|
|
{ |
|
35
|
1 |
|
if (!isset($dateString) || trim($dateString) === '') { |
|
36
|
1 |
|
return null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$regex = '/' |
|
40
|
|
|
. "(\d{4})(\d{2})(\d{2})?" // YYYYMMDD 1,2,3 |
|
41
|
|
|
. "(?:(\d{2})(\d{2})(\d{2}))?" // HHMMSS - optional 4,5,6 |
|
42
|
|
|
. "(?:\.(\d{3}))?" // .XXX - optional 7 |
|
43
|
|
|
. "(?:\[(-?\d+)\:(\w{3}\]))?" // [-n:TZ] - optional 8,9 |
|
44
|
1 |
|
. '/'; |
|
45
|
|
|
|
|
46
|
1 |
|
if (preg_match($regex, $dateString, $matches)) { |
|
47
|
1 |
|
$year = (int)$matches[1]; |
|
48
|
1 |
|
$month = (int)$matches[2]; |
|
49
|
1 |
|
$day = (int)$matches[3]; |
|
50
|
1 |
|
$hour = isset($matches[4]) ? $matches[4] : 0; |
|
51
|
1 |
|
$min = isset($matches[5]) ? $matches[5] : 0; |
|
52
|
1 |
|
$sec = isset($matches[6]) ? $matches[6] : 0; |
|
53
|
|
|
|
|
54
|
1 |
|
$format = $year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $min . ':' . $sec; |
|
55
|
|
|
|
|
56
|
|
|
try { |
|
57
|
1 |
|
$dt = null; |
|
58
|
1 |
|
if (is_callable(static::$fnDateTimeFactory)) { |
|
59
|
1 |
|
$dt = call_user_func(static::$fnDateTimeFactory, $format); |
|
60
|
|
|
} else { |
|
61
|
1 |
|
$dt = new \DateTime($format); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return $dt; |
|
65
|
|
|
} catch (\Exception $e) { |
|
66
|
|
|
if ($ignoreErrors) { |
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
throw $e; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
throw new \RuntimeException('Failed to initialize DateTime for string: ' . $dateString); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Create a formatted number in Float according to different locale options |
|
79
|
|
|
* |
|
80
|
|
|
* Supports: |
|
81
|
|
|
* 000,00 and -000,00 |
|
82
|
|
|
* 0.000,00 and -0.000,00 |
|
83
|
|
|
* 0,000.00 and -0,000.00 |
|
84
|
|
|
* 000.00 and 000.00 |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $amountString |
|
87
|
|
|
* @return float |
|
88
|
|
|
*/ |
|
89
|
17 |
|
public static function createAmountFromStr($amountString) |
|
90
|
|
|
{ |
|
91
|
|
|
// Decimal mark style (UK/US): 000.00 or 0,000.00 |
|
92
|
17 |
|
if (preg_match('/^(-|\+)?([\d,]+)(\.?)([\d]{2})$/', $amountString) === 1) { |
|
93
|
10 |
|
return (float)preg_replace( |
|
94
|
10 |
|
['/([,]+)/', '/\.?([\d]{2})$/'], |
|
95
|
10 |
|
['', '.$1'], |
|
96
|
10 |
|
$amountString |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// European style: 000,00 or 0.000,00 |
|
101
|
7 |
|
if (preg_match('/^(-|\+)?([\d\.]+,?[\d]{2})$/', $amountString) === 1) { |
|
102
|
3 |
|
return (float)preg_replace( |
|
103
|
3 |
|
['/([\.]+)/', '/,?([\d]{2})$/'], |
|
104
|
3 |
|
['', '.$1'], |
|
105
|
3 |
|
$amountString |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
4 |
|
return (float)$amountString; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|