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

126 lines
4.4 KiB
PHP
Raw Normal View History

2016-07-16 07:58:25 +02:00
<?php
/**
* Amount.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-07-16 07:58:25 +02:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02: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
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-07-16 07:58:25 +02:00
*/
declare(strict_types=1);
2016-07-16 07:58:25 +02:00
namespace FireflyIII\Import\Converter;
2017-09-08 20:24:11 +02:00
use Log;
2016-07-16 07:58:25 +02:00
/**
2017-12-19 18:53:50 +01:00
* Class Amount.
2016-07-16 07:58:25 +02:00
*/
2017-06-24 06:57:24 +02:00
class Amount implements ConverterInterface
2016-07-16 07:58:25 +02:00
{
/**
* Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
2017-11-15 12:25:49 +01:00
* - Jamie Zawinski.
2016-07-16 07:58:25 +02:00
*
*
2016-07-16 07:58:25 +02:00
* @param $value
*
2017-06-24 06:57:24 +02:00
* @return string
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2016-07-16 07:58:25 +02:00
*/
2017-06-24 06:57:24 +02:00
public function convert($value): string
2016-07-16 07:58:25 +02:00
{
2017-11-15 12:25:49 +01:00
if (null === $value) {
return '0';
}
2017-09-08 20:24:11 +02:00
Log::debug(sprintf('Start with amount "%s"', $value));
2018-01-02 06:39:34 +01:00
$original = $value;
$value = strval($value);
$value = $this->stripAmount($value);
2016-07-16 07:58:25 +02:00
$len = strlen($value);
$decimalPosition = $len - 3;
$altPosition = $len - 2;
2016-07-16 07:58:25 +02:00
$decimal = null;
2017-11-15 12:25:49 +01:00
if (($len > 2 && '.' === $value[$decimalPosition]) || ($len > 2 && strpos($value, '.') > $decimalPosition)) {
2016-07-16 07:58:25 +02:00
$decimal = '.';
2017-09-08 20:24:11 +02:00
Log::debug(sprintf('Decimal character in "%s" seems to be a dot.', $value));
2016-09-20 00:00:11 +01:00
}
2017-11-15 12:25:49 +01:00
if ($len > 2 && ',' === $value[$decimalPosition]) {
2016-07-16 07:58:25 +02:00
$decimal = ',';
2017-09-08 20:24:11 +02:00
Log::debug(sprintf('Decimal character in "%s" seems to be a comma.', $value));
2016-10-07 05:44:21 +02:00
}
// decimal character is null? find out if "0.1" or ".1" or "0,1" or ",1"
2017-11-15 12:25:49 +01:00
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 07:58:25 +02:00
2017-12-19 21:54:15 +01:00
// decimal character still null? Search from the left for '.',',' or ' '.
if (is_null($decimal)) {
Log::debug('Decimal is still NULL, probably number with >2 decimals. Search for a dot.');
$res = strrpos($value, '.');
2017-12-22 18:32:43 +01:00
if (!(false === $res)) {
2017-12-19 21:54:15 +01:00
// blandly assume this is the one.
Log::debug(sprintf('Searched from the left for "." in amount "%s", assume this is the decimal sign.', $value));
$decimal = '.';
}
unset($options, $res);
2017-12-19 21:54:15 +01:00
}
2016-07-16 07:58:25 +02:00
// if decimal is dot, replace all comma's and spaces with nothing. then parse as float (round to 4 pos)
2017-11-15 12:25:49 +01:00
if ('.' === $decimal) {
2017-09-08 20:24:11 +02:00
$search = [',', ' '];
$value = str_replace($search, '', $value);
2018-01-02 06:39:34 +01:00
Log::debug(sprintf('Converted amount from "%s" to "%s".', $original, $value));
2016-07-16 07:58:25 +02:00
}
2017-11-15 12:25:49 +01:00
if (',' === $decimal) {
2017-09-08 20:24:11 +02:00
$search = ['.', ' '];
$value = str_replace($search, '', $value);
$value = str_replace(',', '.', $value);
2018-01-02 06:39:34 +01:00
Log::debug(sprintf('Converted amount from "%s" to "%s".', $original, $value));
2016-07-16 07:58:25 +02:00
}
2017-11-15 12:25:49 +01:00
if (null === $decimal) {
2016-07-16 07:58:25 +02:00
// replace all:
2017-09-08 20:24:11 +02:00
$search = ['.', ' ', ','];
$value = str_replace($search, '', $value);
2018-01-02 06:39:34 +01:00
Log::debug(sprintf('No decimal character found. Converted amount from "%s" to "%s".', $original, $value));
2016-07-16 07:58:25 +02:00
}
2017-11-22 21:12:27 +01:00
$number = strval(number_format(round(floatval($value), 12), 12, '.', ''));
2017-11-18 20:26:42 +01:00
return $number;
2016-07-16 07:58:25 +02:00
}
2018-01-02 06:39:34 +01:00
/**
* @param string $value
*
* @return string
*/
private function stripAmount(string $value): string
{
$str = preg_replace('/[^\-\(\)\.\,0-9 ]/', '', $value);
$len = strlen($str);
if ($str{0} === '(' && $str{$len - 1} === ')') {
$str = '-' . substr($str, 1, ($len - 2));
}
Log::debug(sprintf('Stripped "%s" away to "%s"', $value, $str));
return $str;
}
2016-08-12 15:10:03 +02:00
}