-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComboNthNum.php
95 lines (78 loc) · 2.73 KB
/
ComboNthNum.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
<?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 function JBZoo\Utils\float;
final class ComboNthNum extends AbstractAggregateRuleCombo
{
public const INPUT_TYPE = AbstractRule::INPUT_TYPE_FLOATS;
protected const NAME = 'N-th value';
private const ARGS = 2;
private const NTH = 0;
private const VAL = 1;
public function getHelpMeta(): array
{
return [
[
'N-th value in the column.',
'The rule expects exactly two arguments: ' .
'the first is the line number (without header), the second is the expected value.',
'Example: `[ 42, 5.0 ]` On the line 42 (disregarding the header), we expect the 5.0. ' .
'The comparison is always as float.',
],
[
self::MIN => ['[ 42, 1.0 ]', 'x >= 1.0'],
self::GREATER => ['[ 42, 2.0 ]', 'x > 2.0'],
self::NOT => ['[ 42, 5.0 ]', 'x != 5.0'],
self::EQ => ['[ 42, 7.0 ]', 'x == 7.0'],
self::LESS => ['[ 42, 8.0 ]', 'x < 8.0'],
self::MAX => ['[ 42, 9.0 ]', 'x <= 9.0'],
],
];
}
protected function getExpected(): float
{
return float($this->getParams()[self::VAL]);
}
protected function getActualAggregate(array $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
}
$realLine = (int)$this->getParams()[self::NTH];
$arrayInd = $realLine - 1;
$actual = $colValues[$arrayInd] ?? null;
if ($actual === null) {
throw new Exception(
"The column does not have a line {$realLine}, so the value cannot be checked.",
);
}
return float($actual);
}
protected static function calcValue(array $columnValues, ?array $options = null): null|float|int
{
return null;
}
private function getParams(): array
{
$params = $this->getOptionAsArray();
if (\count($params) !== self::ARGS) {
throw new Exception(
'The rule expects exactly two arguments: ' .
'the first is the line number (without header), the second is the expected value',
);
}
return $params;
}
}