-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComboDate.php
133 lines (110 loc) · 4.15 KB
/
ComboDate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/
declare(strict_types=1);
namespace JBZoo\CsvBlueprint\Rules\Cell;
final class ComboDate extends AbstractCellRuleCombo
{
protected const NAME = 'date';
private const OUTPUT_DATE_FORMAT = 'Y-m-d H:i:s P';
private const SUGGEST_DATE_FORMAT = 'Y-m-d';
private const INVALID_TIMESTAMP = -1;
public function getHelpMeta(): array
{
return [
[
'Dates. Under the hood, the strings are converted to timestamp and compared.',
'This gives you the ability to use relative dates and any formatting you want.',
'By default, it works in UTC. But you can specify your own timezone as part of the date string.',
'Format: https://www.php.net/manual/en/datetime.format.php',
'Parsing: https://www.php.net/manual/en/function.strtotime.php',
'Timezones: https://www.php.net/manual/en/timezones.php',
],
[
self::MIN => ['-100 years', 'Example of relative past date'],
self::GREATER => ['-99 days', 'Example of relative formats'],
self::EQ => ['01 Jan 2000', 'You can use any string that can be parsed by the strtotime function'],
self::NOT => ['2006-01-02 15:04:05 -0700 Europe/Rome'],
self::LESS => ['now', 'Example of current date and time'],
self::MAX => ['+1 day', 'Example of relative future date'],
],
];
}
public static function analyzeColumnValues(array $columnValues): array|bool|float|int|string
{
$min = null;
$max = null;
foreach ($columnValues as $cellValue) {
if (!IsDate::testValue($cellValue)) {
return false;
}
$timestamp = self::convertToTimestamp($cellValue);
if ($timestamp === self::INVALID_TIMESTAMP) {
return false;
}
if ($min === null || $timestamp < $min) {
$min = $timestamp;
}
if ($max === null || $timestamp > $max) {
$max = $timestamp;
}
}
return $max === $min
? ['' => \date(self::SUGGEST_DATE_FORMAT, (int)$max)]
: [
'min' => \date(self::SUGGEST_DATE_FORMAT, (int)$min),
'max' => \date(self::SUGGEST_DATE_FORMAT, (int)$max),
];
}
protected function getActualCell(string $cellValue): float
{
return self::convertToTimestamp($cellValue);
}
protected function getExpected(): float
{
$expectedValue = $this->getOptionAsString();
try {
$result = (new \DateTimeImmutable($expectedValue))->getTimestamp();
} catch (\Exception) {
return self::INVALID_TIMESTAMP;
}
return $result;
}
protected function getExpectedStr(): string
{
$expectedValue = $this->getOptionAsString();
try {
$formated = (new \DateTimeImmutable($expectedValue))->format(self::OUTPUT_DATE_FORMAT);
} catch (\Exception) {
return "Can't parse date: {$expectedValue}";
}
return "{$formated} ({$expectedValue})";
}
protected function getCurrentStr(string $cellValue): string
{
try {
$formated = (new \DateTimeImmutable($cellValue))->format(self::OUTPUT_DATE_FORMAT);
} catch (\Exception) {
$formated = "Can't parse date: {$cellValue}";
}
return "parsed as \"{$formated}\"";
}
private static function convertToTimestamp(string $date): int
{
try {
$result = (new \DateTimeImmutable($date))->getTimestamp();
} catch (\Exception) {
return self::INVALID_TIMESTAMP;
}
return $result;
}
}