You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.4 KiB

4 years ago
4 years ago
  1. <?php
  2. /*
  3. * Date.php
  4. * Copyright (c) 2021 james@firefly-iii.org
  5. *
  6. * This file is part of the Firefly III Data Importer
  7. * (https://github.com/firefly-iii/data-importer).
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. declare(strict_types=1);
  23. namespace App\Services\CSV\Converter;
  24. use Carbon\Carbon;
  25. use Carbon\Language;
  26. use Exception;
  27. use InvalidArgumentException;
  28. /**
  29. * Class Date
  30. */
  31. class Date implements ConverterInterface
  32. {
  33. private string $dateFormat;
  34. private string $dateFormatPattern;
  35. private string $dateLocale;
  36. /**
  37. * Date constructor.
  38. */
  39. public function __construct()
  40. {
  41. $this->dateFormat = 'Y-m-d';
  42. $this->dateLocale = 'en';
  43. $this->dateFormatPattern = '/(?:('.join("|", array_keys(Language::all())).')\:)?(.+)/';
  44. }
  45. /**
  46. * Convert a value.
  47. *
  48. * @param $value
  49. *
  50. * @return string
  51. *
  52. */
  53. public function convert($value)
  54. {
  55. $string = app('steam')->cleanStringAndNewlines($value);
  56. $carbon = null;
  57. if ('!' !== $this->dateFormat[0]) {
  58. $this->dateFormat = sprintf('!%s', $this->dateFormat);
  59. }
  60. if ('' === $string) {
  61. app('log')->warning('Empty date string, so date is set to today.');
  62. $carbon = today();
  63. $carbon->startOfDay();
  64. }
  65. if ('' !== $string) {
  66. app('log')->debug(sprintf('Date converter is going to work on "%s" using format "%s"', $string, $this->dateFormat));
  67. try {
  68. $carbon = Carbon::createFromLocaleFormat($this->dateFormat, $this->dateLocale, $string);
  69. } catch (InvalidArgumentException|Exception $e) {
  70. app('log')->error(sprintf('%s converting the date: %s', get_class($e), $e->getMessage()));
  71. return Carbon::today()->startOfDay()->format('Y-m-d H:i:s');
  72. }
  73. }
  74. return $carbon->format('Y-m-d H:i:s');
  75. }
  76. /**
  77. * Add extra configuration parameters.
  78. *
  79. * @param string $configuration
  80. */
  81. public function setConfiguration(string $configuration): void
  82. {
  83. [$this->dateLocale, $this->dateFormat] = $this->splitLocaleFormat($configuration);
  84. }
  85. /**
  86. * @param string $format
  87. *
  88. * @return array
  89. */
  90. public function splitLocaleFormat(string $format): array
  91. {
  92. $currentDateLocale = 'en';
  93. $currentDateFormat = 'Y-m-d';
  94. $dateFormatConfiguration = [];
  95. preg_match($this->dateFormatPattern, $format, $dateFormatConfiguration);
  96. if (3 === count($dateFormatConfiguration)) {
  97. $currentDateLocale = $dateFormatConfiguration[1] ?: $currentDateLocale;
  98. $currentDateFormat = $dateFormatConfiguration[2];
  99. }
  100. return [$currentDateLocale, $currentDateFormat];
  101. }
  102. }