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

235 lines
6.7 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
2017-12-17 07:44:05 -06:00
* 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
/**
2017-12-19 11:53:50 -06:00
* Class Amount.
2016-07-16 00:58:25 -05:00
*/
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.
2017-11-15 05:25:49 -06:00
* - Jamie Zawinski.
2016-07-16 00:58:25 -05:00
*
2018-07-22 05:52:07 -05:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2016-07-16 00:58:25 -05:00
* @param $value
*
2017-06-23 23:57:24 -05:00
* @return string
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
{
2017-11-15 05:25:49 -06:00
if (null === $value) {
return '0';
}
2017-09-08 13:24:11 -05:00
Log::debug(sprintf('Start with amount "%s"', $value));
2018-07-22 05:52:07 -05:00
$original = $value;
$value = $this->stripAmount((string)$value);
$decimal = null;
if ($this->decimalIsDot($value)) {
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
}
2018-07-22 05:52:07 -05:00
if ($this->decimalIsComma($value)) {
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
}
2018-07-22 05:52:07 -05:00
// decimal character is null? find out if "0.1" or ".1" or "0,1" or ",1"
2018-07-22 05:52:07 -05:00
if ($this->alternativeDecimalSign($value)) {
$decimal = $this->getAlternativeDecimalSign($value);
}
2016-07-16 00:58:25 -05:00
2017-12-19 14:54:15 -06:00
// decimal character still null? Search from the left for '.',',' or ' '.
2018-04-02 08:10:40 -05:00
if (null === $decimal) {
2018-07-22 05:52:07 -05:00
$decimal = $this->findFromLeft($value);
2017-12-19 14:54:15 -06:00
}
2018-07-22 05:52:07 -05:00
// if decimal is dot, replace all comma's and spaces with nothing
if (null !== $decimal) {
$value = $this->replaceDecimal($decimal, $value);
2018-01-01 23:39:34 -06:00
Log::debug(sprintf('Converted amount from "%s" to "%s".', $original, $value));
2016-07-16 00:58:25 -05:00
}
2018-07-22 05:52:07 -05:00
2017-11-15 05:25:49 -06:00
if (null === $decimal) {
2016-07-16 00:58:25 -05:00
// replace all:
2018-01-10 12:06:27 -06:00
$search = ['.', ' ', ','];
$value = str_replace($search, '', $value);
2018-01-01 23:39:34 -06:00
Log::debug(sprintf('No decimal character found. Converted amount from "%s" to "%s".', $original, $value));
2016-07-16 00:58:25 -05:00
}
2018-07-22 05:52:07 -05:00
if ('.' === $value{0}) {
2018-07-01 06:34:57 -05:00
$value = '0' . $value;
}
if (is_numeric($value)) {
Log::debug(sprintf('Final NUMERIC value is: "%s"', $value));
return $value;
}
2018-09-15 06:44:36 -05:00
// @codeCoverageIgnoreStart
2018-07-01 06:34:57 -05:00
Log::debug(sprintf('Final value is: "%s"', $value));
$formatted = sprintf('%01.12f', $value);
Log::debug(sprintf('Is formatted to : "%s"', $formatted));
return $formatted;
2018-09-15 06:44:36 -05:00
// @codeCoverageIgnoreEnd
2018-07-01 06:34:57 -05:00
}
2018-07-22 05:52:07 -05:00
/**
* Check if the value has a dot or comma on an alternative place,
* catching strings like ",1" or ".5".
*
* @param string $value
*
* @return bool
*/
private function alternativeDecimalSign(string $value): bool
{
$length = \strlen($value);
$altPosition = $length - 2;
return $length > 1 && ('.' === $value[$altPosition] || ',' === $value[$altPosition]);
}
/**
* Helper function to see if the decimal separator is a comma.
*
* @param string $value
*
* @return bool
*/
private function decimalIsComma(string $value): bool
{
$length = \strlen($value);
$decimalPosition = $length - 3;
return $length > 2 && ',' === $value[$decimalPosition];
}
/**
* Helper function to see if the decimal separator is a dot.
*
* @param string $value
*
* @return bool
*/
private function decimalIsDot(string $value): bool
2018-07-01 06:34:57 -05:00
{
2018-07-22 05:52:07 -05:00
$length = \strlen($value);
$decimalPosition = $length - 3;
return ($length > 2 && '.' === $value[$decimalPosition]) || ($length > 2 && strpos($value, '.') > $decimalPosition);
}
/**
* Search from the left for decimal sign.
*
* @param string $value
*
* @return string
*/
private function findFromLeft(string $value): ?string
{
$decimal = null;
Log::debug('Decimal is still NULL, probably number with >2 decimals. Search for a dot.');
$res = strrpos($value, '.');
if (!(false === $res)) {
// 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 = '.';
}
return $decimal;
}
/**
* Returns the alternative decimal point used, such as a dot or a comma,
* from strings like ",1" or "0.5".
*
* @param string $value
*
* @return string
*/
private function getAlternativeDecimalSign(string $value): string
{
$length = \strlen($value);
$altPosition = $length - 2;
return $value[$altPosition];
}
/**
2018-07-22 08:08:56 -05:00
* Replaces other characters like thousand separators with nothing to make the decimal separator the only special
* character in the string.
*
2018-07-22 05:52:07 -05:00
* @param string $decimal
* @param string $value
*
* @return string
*/
private function replaceDecimal(string $decimal, string $value): string
{
$search = [',', ' ']; // default when decimal sign is a dot.
if (',' === $decimal) {
$search = ['.', ' '];
2018-07-01 06:34:57 -05:00
}
2018-07-22 05:52:07 -05:00
$value = str_replace($search, '', $value);
2016-07-16 00:58:25 -05:00
2018-07-22 05:52:07 -05:00
/** @noinspection CascadeStringReplacementInspection */
$value = str_replace(',', '.', $value);
return $value;
2016-07-16 00:58:25 -05:00
}
2018-01-01 23:39:34 -06:00
/**
2018-07-22 05:52:07 -05:00
* Strip amount from weird characters.
*
2018-01-01 23:39:34 -06:00
* @param string $value
*
* @return string
*/
private function stripAmount(string $value): string
{
2018-06-08 22:50:31 -05:00
if (0 === strpos($value, '--')) {
$value = substr($value, 2);
}
2018-01-01 23:39:34 -06:00
$str = preg_replace('/[^\-\(\)\.\,0-9 ]/', '', $value);
2018-04-27 23:23:13 -05:00
$len = \strlen($str);
2018-03-24 04:35:42 -05:00
if ('(' === $str[0] && ')' === $str[$len - 1]) {
2018-03-29 12:01:47 -05:00
$str = '-' . substr($str, 1, $len - 2);
2018-01-01 23:39:34 -06:00
}
Log::debug(sprintf('Stripped "%s" away to "%s"', $value, $str));
return $str;
}
2016-08-12 08:10:03 -05:00
}