-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNth.php
65 lines (52 loc) · 1.84 KB
/
Nth.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
<?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;
final class Nth extends AbstractAggregateRule
{
public const INPUT_TYPE = AbstractRule::INPUT_TYPE_STRINGS;
private const ARGS = 2;
private const NTH = 0;
private const VAL = 1;
public function getHelpMeta(): array
{
return [
[],
[self::DEFAULT => ['[ 2, Expected ]', 'Nth value in the column. Will be compared as strings.']],
];
}
public function validateRule(array $columnValues): ?string
{
if (\count($columnValues) === 0) {
return null;
}
$params = $this->getOptionAsArray();
if (\count($params) !== self::ARGS) {
return 'The rule expects exactly two arguments: ' .
'the first is the line number (without header), the second is the expected value';
}
$realLine = (int)$params[self::NTH];
$arrayInd = $realLine - 1;
$expValue = (string)$params[self::VAL];
$actual = $columnValues[$arrayInd] ?? null;
if ($actual === null) {
return "The column does not have a line {$realLine}, so the value cannot be checked.";
}
if ($actual !== $expValue) {
return "The value on line {$realLine} in the column is \"<c>{$actual}</c>\", " .
"which is not equal than the expected \"<green>{$expValue}</green>\"";
}
return null;
}
}