-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractAggregateRuleCombo.php
More file actions
96 lines (76 loc) · 2.83 KB
/
Copy pathAbstractAggregateRuleCombo.php
File metadata and controls
96 lines (76 loc) · 2.83 KB
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
<?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\Aggregate;
use JBZoo\CsvBlueprint\Rules\AbstractRule;
use JBZoo\CsvBlueprint\Rules\AbstractRuleCombo;
abstract class AbstractAggregateRuleCombo extends AbstractRuleCombo
{
public const INPUT_TYPE = AbstractRule::INPUT_TYPE_STRINGS;
abstract protected static function calcValue(array $columnValues, ?array $options = null): float|int|null;
public function getRuleCode(?string $mode = null): string
{
return 'ag:' . parent::getRuleCode($mode);
}
/**
* @phan-suppress PhanAbstractStaticMethodCallInStatic
*/
public static function analyzeColumnValues(array $columnValues): array|bool|float|int|string
{
$result = static::calcValue($columnValues);
if ($result === null) {
return false;
}
return $result;
}
protected function getActualAggregate(array $colValues): ?float
{
return static::calcValue($colValues);
}
protected function getActual(array|string $value): float
{
if (\is_string($value)) {
throw new Exception('The value should be an array of numbers/strings');
}
$result = $this->getActualAggregate($value);
return $result ?? 0;
}
protected function getExpected(): float
{
return $this->getOptionAsFloat();
}
protected function validateComboAggregate(array $colValues, string $mode): ?string
{
$prefix = $mode === self::NOT ? 'not ' : '';
$verb = static::VERBS[$mode];
$name = static::NAME;
try {
$actual = $this->getActualAggregate($colValues); // Important to use the original method here!
} catch (\Throwable $exception) {
return "<red>{$exception->getMessage()}</red>"; // TODO: Expose the error/warning message in the report?
}
if ($actual === null) {
return null; // Looks like it's impossible to calculate the aggregate value in this case. Skip.
}
try {
$expected = $this->getExpected();
} catch (\Throwable $exception) {
return "<red>{$exception->getMessage()}</red>"; // TODO: Expose the error/warning message in the report?
}
if (!static::compare($expected, $actual, $mode)) {
return "The {$name} in the column is \"<c>{$actual}</c>\", "
. "which is {$verb} than the {$prefix}expected \"<green>{$expected}</green>\"";
}
return null;
}
}