firefly-iii/app/Import/Converter/Amount.php

98 lines
3.4 KiB
PHP
Raw Normal View History

2016-07-16 00:58:25 -05:00
<?php
/**
* Amount.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-07-16 00:58:25 -05:00
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-07-16 00:58:25 -05:00
*/
declare(strict_types=1);
2016-07-16 00:58:25 -05:00
namespace FireflyIII\Import\Converter;
2017-09-08 13:24:11 -05:00
use Log;
2016-07-16 00:58:25 -05:00
/**
* Class RabobankDebetCredit
*
* @package FireflyIII\Import\Converter
*/
2017-06-23 23:57:24 -05:00
class Amount implements ConverterInterface
2016-07-16 00:58:25 -05:00
{
/**
* Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
* - Jamie Zawinski
*
*
2016-07-16 00:58:25 -05:00
* @param $value
*
2017-06-23 23:57:24 -05:00
* @return string
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2016-07-16 00:58:25 -05:00
*/
2017-06-23 23:57:24 -05:00
public function convert($value): string
2016-07-16 00:58:25 -05:00
{
if (is_null($value)) {
return '0';
}
$value = strval($value);
2017-09-08 13:24:11 -05:00
Log::debug(sprintf('Start with amount "%s"', $value));
2016-07-16 00:58:25 -05:00
$len = strlen($value);
$decimalPosition = $len - 3;
$altPosition = $len - 2;
2016-07-16 00:58:25 -05:00
$decimal = null;
2017-07-15 09:41:07 -05:00
if (($len > 2 && $value{$decimalPosition} === '.') || ($len > 2 && strpos($value, '.') > $decimalPosition)) {
2016-07-16 00:58:25 -05:00
$decimal = '.';
2017-09-08 13:24:11 -05:00
Log::debug(sprintf('Decimal character in "%s" seems to be a dot.', $value));
2016-09-19 18:00:11 -05:00
}
2017-07-15 09:41:07 -05:00
if ($len > 2 && $value{$decimalPosition} === ',') {
2016-07-16 00:58:25 -05:00
$decimal = ',';
2017-09-08 13:24:11 -05:00
Log::debug(sprintf('Decimal character in "%s" seems to be a comma.', $value));
2016-10-06 22:44:21 -05:00
}
// decimal character is null? find out if "0.1" or ".1" or "0,1" or ",1"
if ($len > 1 && ($value{$altPosition} === '.' || $value{$altPosition} === ',')) {
$decimal = $value{$altPosition};
Log::debug(sprintf('Alternate search resulted in "%s" for decimal sign.', $decimal));
}
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 === '.') {
2017-09-08 13:24:11 -05:00
$search = [',', ' '];
$oldValue = $value;
$value = str_replace($search, '', $value);
Log::debug(sprintf('Converted amount from "%s" to "%s".', $oldValue, $value));
2016-07-16 00:58:25 -05:00
}
if ($decimal === ',') {
2017-09-08 13:24:11 -05:00
$search = ['.', ' '];
$oldValue = $value;
$value = str_replace($search, '', $value);
$value = str_replace(',', '.', $value);
Log::debug(sprintf('Converted amount from "%s" to "%s".', $oldValue, $value));
2016-07-16 00:58:25 -05:00
}
if (is_null($decimal)) {
// replace all:
2017-09-08 13:24:11 -05:00
$search = ['.', ' ', ','];
$oldValue = $value;
$value = str_replace($search, '', $value);
Log::debug(sprintf('No decimal character found. Converted amount from "%s" to "%s".', $oldValue, $value));
2016-07-16 00:58:25 -05:00
}
2017-06-23 23:57:24 -05:00
return strval(round(floatval($value), 12));
2016-07-16 00:58:25 -05:00
}
2016-08-12 08:10:03 -05:00
}