2016-07-16 00:58:25 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Amount.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-07-16 00:58:25 -05:00
|
|
|
*/
|
|
|
|
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-07-16 00:58:25 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Import\Converter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class RabobankDebetCredit
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Import\Converter
|
|
|
|
*/
|
|
|
|
class Amount extends BasicConverter implements ConverterInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
|
|
|
|
* - Jamie Zawinski
|
|
|
|
*
|
2016-09-19 17:53:51 -05:00
|
|
|
*
|
2016-07-16 00:58:25 -05:00
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function convert($value): float
|
|
|
|
{
|
|
|
|
$len = strlen($value);
|
|
|
|
$decimalPosition = $len - 3;
|
|
|
|
$decimal = null;
|
|
|
|
|
2016-09-19 17:53:51 -05:00
|
|
|
if (($len > 2 && $value{$decimalPosition} == '.') || ($len > 2 && strpos($value, '.') > $decimalPosition)) {
|
2016-07-16 00:58:25 -05:00
|
|
|
$decimal = '.';
|
2016-09-19 18:00:11 -05:00
|
|
|
}
|
|
|
|
if ($len > 2 && $value{$decimalPosition} == ',') {
|
2016-07-16 00:58:25 -05:00
|
|
|
$decimal = ',';
|
2016-10-06 22:44:21 -05:00
|
|
|
}
|
2016-07-16 00:58:25 -05:00
|
|
|
|
|
|
|
// if decimal is dot, replace all comma's and spaces with nothing. then parse as float (round to 4 pos)
|
|
|
|
if ($decimal === '.') {
|
|
|
|
$search = [',', ' '];
|
|
|
|
$value = str_replace($search, '', $value);
|
|
|
|
}
|
|
|
|
if ($decimal === ',') {
|
|
|
|
$search = ['.', ' '];
|
|
|
|
$value = str_replace($search, '', $value);
|
|
|
|
$value = str_replace(',', '.', $value);
|
|
|
|
}
|
|
|
|
if (is_null($decimal)) {
|
|
|
|
// replace all:
|
|
|
|
$search = ['.', ' ', ','];
|
|
|
|
$value = str_replace($search, '', $value);
|
|
|
|
}
|
|
|
|
|
2016-07-29 14:40:58 -05:00
|
|
|
$this->setCertainty(90);
|
|
|
|
|
2016-07-16 00:58:25 -05:00
|
|
|
|
2016-12-30 06:45:02 -06:00
|
|
|
return round(floatval($value), 12);
|
2016-07-16 00:58:25 -05:00
|
|
|
|
|
|
|
}
|
2016-08-12 08:10:03 -05:00
|
|
|
}
|