-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RiskPremium.php
334 lines (313 loc) · 12.6 KB
/
RiskPremium.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* Risk premium is a easy PHP API class for follow the main values of Risk premium
* on Europe.
*
* For more info:
* Risk premium: http://en.wikipedia.org/wiki/Risk_premium
* Von Neumann–Morgenstern utility theorem: http://en.wikipedia.org/wiki/Von_Neumann%E2%80%93Morgenstern_utility_theorem
*
* @license AGPLv3 http://www.gnu.org/licenses/agpl-3.0.html
*
* RiskPremium is free software; you can redistribute it and/or modify
* it under the terms of the Affero GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* FreeStation is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fail2Ban; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @author Ángel Guzmán Maeso <[email protected]>
* @version 1.0
* @category Core
* @package Api
* @link http://github.com/shakaran/riskpremium
* @uses twitteroauth https://github.com/abraham/twitteroauth
*/
class RiskPremium
{
/** @constant CONTENT_URL Content data extracted from some url page */
const CONTENT_URL = 'http://www.infobolsa.es/primas-riesgo.htm';
/** @constant NO_VALUE value for represent no data */
const NO_VALUE = -1;
/** @var string $content stores the html data */
private static $content = NULL;
/** @var array $valid_keys params for get different statistics from data */
private static $valid_keys = array('value', 'difference', 'percentage', 'max_year', 'min_year', 'all');
/** @var array $risk_data values for each country. Store expression for keys and method name */
private static $risk_data = array(
// Ireland
'ir' => array(
'method' => 'getIreland',
'expression' => array(
'value' => ' id="LV335.RBPLIR .D420">(.*)</td>', // Value
'difference' => ' id="LV335.RBPLIR .D240">(.*)</td>', // Difference
'percentage' => ' id="LV335.RBPLIR .D250">(.*)</td>', // Percentage
'max_year' => ' id="LV335.RBPLIR .D1245">(.*)</td>', // Max year
'min_year' => ' id="LV335.RBPLIR .D1255">(.*)</td>', // Min year
),
),
// Portugal
'pt' => array(
'method' => 'getPortugal',
'expression' => array(
'value' => ' id="LV335.RBPLPT .D420">(.*)</td>', // Value
'difference' => ' id="LV335.RBPLPT .D240">(.*)</td>', // Difference
'percentage' => ' id="LV335.RBPLPT .D250">(.*)</td>', // Percentage
'max_year' => ' id="LV335.RBPLPT .D1245">(.*)</td>', // Max year
'min_year' => ' id="LV335.RBPLPT .D1255">(.*)</td>', // Min year
),
),
// Finland
'fi' => array(
'method' => 'getFinland',
'expression' => array(
'value' => ' id="LV335.RBPLFI .D420">(.*)</td>', // Value
'difference' => ' id="LV335.RBPLFI .D240">(.*)</td>', // Difference
'percentage' => ' id="LV335.RBPLFI .D250">(.*)</td>', // Percentage
'max_year' => ' id="LV335.RBPLFI .D1245">(.*)</td>', // Max year
'min_year' => ' id="LV335.RBPLFI .D1255">(.*)</td>', // Min year
),
),
// Belgium
'be' => array(
'method' => 'getBelgium',
'expression' => array(
'value' => ' id="LV335.RBPLBE .D420">(.*)</td>', // Value
'difference' => ' id="LV335.RBPLBE .D240">(.*)</td>', // Difference
'percentage' => ' id="LV335.RBPLBE .D250">(.*)</td>', // Percentage
'max_year' => ' id="LV335.RBPLBE .D1245">(.*)</td>', // Max year
'min_year' => ' id="LV335.RBPLBE .D1255">(.*)</td>', // Min year
),
),
// Austria
'at' => array(
'method' => 'getAustria',
'expression' => array(
'value' => ' id="LV335.RBPLAT .D420">(.*)</td>', // Value
'difference' => ' id="LV335.RBPLAT .D240">(.*)</td>', // Difference
'percentage' => ' id="LV335.RBPLAT .D250">(.*)</td>', // Percentage
'max_year' => ' id="LV335.RBPLAT .D1245">(.*)</td>', // Max year
'min_year' => ' id="LV335.RBPLAT .D1255">(.*)</td>', // Min year
),
),
// Holland
'nl' => array(
'method' => 'getHolland',
'expression' => array(
'value' => ' id="LV335.RBPLNL .D420">(.*)</td>', // Value
'difference' => ' id="LV335.RBPLNL .D240">(.*)</td>', // Difference
'percentage' => ' id="LV335.RBPLNL .D250">(.*)</td>', // Percentage
'max_year' => ' id="LV335.RBPLNL .D1245">(.*)</td>', // Max year
'min_year' => ' id="LV335.RBPLNL .D1255">(.*)</td>', // Min year
),
),
// Spain
'es' => array(
'method' => 'getSpain',
'expression' => array(
'value' => ' id="LV335.RBPLES .D420">(.*)</td>', // Value
'difference' => ' id="LV335.RBPLES .D240">(.*)</td>', // Difference
'percentage' => ' id="LV335.RBPLES .D250">(.*)</td>', // Percentage
'max_year' => ' id="LV335.RBPLES .D1245">(.*)</td>', // Max year
'min_year' => ' id="LV335.RBPLES .D1255">(.*)</td>', // Min year
),
),
// France
'fr' => array(
'method' => 'getFrance',
'expression' => array(
'value' => ' id="LV335.RBPLFR .D420">(.*)</td>', // Value
'difference' => ' id="LV335.RBPLFR .D240">(.*)</td>', // Difference
'percentage' => ' id="LV335.RBPLFR .D250">(.*)</td>', // Percentage
'max_year' => ' id="LV335.RBPLFR .D1245">(.*)</td>', // Max year
'min_year' => ' id="LV335.RBPLFR .D1255">(.*)</td>', // Min year
),
),
// Italy
'it' => array(
'method' => 'getItaly',
'expression' => array(
'value' => ' id="LV335.RBPLIT .D420">(.*)</td>', // Value
'difference' => ' id="LV335.RBPLIT .D240">(.*)</td>', // Difference
'percentage' => ' id="LV335.RBPLIT .D250">(.*)</td>', // Percentage
'max_year' => ' id="LV335.RBPLIT .D1245">(.*)</td>', // Max year
'min_year' => ' id="LV335.RBPLIT .D1255">(.*)</td>', // Min year
),
),
// Greece
'gr' => array(
'method' => 'getGreece',
'expression' => array(
'value' => ' id="LV335.RBPLGR .D420">(.*)</td>', // Value
'difference' => ' id="LV335.RBPLGR .D240">(.*)</td>', // Difference
'percentage' => ' id="LV335.RBPLGR .D250">(.*)</td>', // Percentage
'max_year' => ' id="LV335.RBPLGR .D1245">(.*)</td>', // Max year
'min_year' => ' id="LV335.RBPLGR .D1255">(.*)</td>', // Min year
),
),
);
/**
* Build the main object and store html content.
*
* @author Ángel Guzmán Maeso <[email protected]>
* @return void
*/
public function __construct()
{
self::$content = file_get_contents(self::CONTENT_URL);
if(empty(self::$content))
{
self::$content = self::NO_VALUE;
}
}
/**
* Validate a key for prepared on data.
*
* Valid keys are $valid_keys.
*
* @see $valid_keys
* @author Ángel Guzmán Maeso <[email protected]>
* @access private
* @return string The validated key
*/
private static function validate($key)
{
return in_array($key, self::$valid_keys) ? $key : 'value';
}
/**
* Parse a expression searching useful data on content.
*
* Valid keys are $valid_keys.
*
* @see $risk_data
* @author Ángel Guzmán Maeso <[email protected]>
* @access private
* @return mixed The risk prime data parsed on spanish number format or 0 if no data.
*/
private static function parse($expression)
{
$data = NULL;
if(self::$content !== self::NO_VALUE && preg_match('@' . $expression . '@i', self::$content, $matches))
{
$data = $matches[1]; // Value as string on spanish money format
}
else
{
$data = 0; // Fallback mode as zero value
}
return $data;
}
/**
* Meta dinamic method called for each country.
*
* Obtain the info needed for a country and key.
*
* If no key is given, the defaul key is value.
*
* Valid keys are $valid_keys.
*
* @see $risk_data
* @see $valid_keys
* @author Ángel Guzmán Maeso <[email protected]>
* @access private
* @return mixed The risk prime data parsed on spanish number format or 0 if no data.
*/
private static function getData($country = 'es', $key = 'value')
{
if(is_array($key))
{
$key = isset($key[0]) ? $key[0] : 'value';
}
$key = self::validate($key);
// Check cached results
if(!isset(self::$risk_data[$country][$key]))
{
// Process all valid keys
if($key === 'all')
{
self::$risk_data[$country]['all'] = array();
foreach(self::$risk_data[$country]['expression'][$key] as $expression)
{
self::$risk_data[$country]['all'][$key] = self::parse($expression);
}
}
else // Process a valid key
{
$expression = self::$risk_data[$country]['expression'][$key];
self::$risk_data[$country][$key] = self::parse($expression);
}
}
return self::$risk_data[$country][$key];
}
/**
* It is called when a public method is accepted for a country.
*
* Parse the values dinamic called or show a message error if the
* method doesn't exist.
*
* If no key is given, the defaul key is value.
*
* Valid keys are $valid_keys.
*
* @see $risk_data
* @see $valid_keys
* @author Ángel Guzmán Maeso <[email protected]>
* @access private
* @return mixed The risk prime data parsed on spanish number format or 0 if no data.
*/
public function __call($method_name = 'getSpain', $key = 'value')
{
// Validate the method name
$country = NULL;
foreach(self::$risk_data as $country_name => $metadata)
{
if(self::$risk_data[$country_name]['method'] === $method_name)
{
$country = $country_name;
}
}
if(empty($country))
{
echo 'Error: risk premium for method ' . $method_name . ' not implemented';
}
else
{
return call_user_func('RiskPremium::getData', $country, $key);
}
}
}
/* Examples */
// Init the class
$risk_premium = new RiskPremium();
// Get data without keys for each country or specify value for normal behaviour
$risk_premium_ireland = $risk_premium->getIreland();
$risk_premium_portugal = $risk_premium->getPortugal();
$risk_premium_finland = $risk_premium->getFinland();
$risk_premium_belgium = $risk_premium->getBelgium();
$risk_premium_austria = $risk_premium->getAustria();
$risk_premium_holland = $risk_premium->getHolland();
$risk_premium_spain = $risk_premium->getSpain('value');
$risk_premium_france = $risk_premium->getFrance();
$risk_premium_italy = $risk_premium->getItaly();
$risk_premium_greece = $risk_premium->getGreece();
// Show the data. See on Spain other interested data with other keys
echo 'Ireland: ' . $risk_premium_ireland . '<br />' .
'Portugal: ' . $risk_premium_portugal . '<br />' .
'Finland: ' . $risk_premium_finland . '<br />' .
'Belgium: ' . $risk_premium_belgium . '<br />' .
'Austria: ' . $risk_premium_austria . '<br />' .
'Holland: ' . $risk_premium_holland . '<br />' .
'Spain: ' . $risk_premium_spain . ' Diff: ' . $risk_premium->getSpain('difference') .
' % ' . $risk_premium->getSpain('percentage') .
' Max year: ' . $risk_premium->getSpain('max_year') .
' Min year: ' . $risk_premium->getSpain('min_year') .'<br />' .
'France: ' . $risk_premium_france . '<br />' .
'Italy: ' . $risk_premium_italy . '<br />' .
'Greece: ' . $risk_premium_greece . '<br />';