-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractCellRuleCombo.php
80 lines (62 loc) · 2.23 KB
/
AbstractCellRuleCombo.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
<?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;
use JBZoo\CsvBlueprint\Rules\AbstractRuleCombo;
abstract class AbstractCellRuleCombo extends AbstractRuleCombo
{
abstract protected function getActualCell(string $cellValue): float;
protected function getActual(array|string $value): float
{
if (\is_array($value)) {
throw new Exception('This method should not be called with an array');
}
return $this->getActualCell($value);
}
protected function getCurrentStr(string $cellValue): string
{
return (string)$this->getActualCell($cellValue);
}
protected function getExpectedStr(): string
{
return (string)$this->getExpected();
}
protected function validateComboCell(string $cellValue, ?string $mode = null): ?string
{
$mode ??= $this->mode;
if ($cellValue === '') {
return null;
}
if (!static::compare($this->getExpected(), $this->getActual($cellValue), $mode)) {
return $this->getErrorMessage($cellValue, $mode);
}
return null;
}
private function getErrorMessage(string $cellValue, string $mode): string
{
$prefix = $mode === self::NOT ? 'not ' : '';
$verb = self::VERBS[$mode];
$name = static::NAME;
$currentStr = $this->getCurrentStr($cellValue);
$expectedStr = $this->getExpectedStr();
if ($currentStr !== '') {
$currentStr = " is {$currentStr}";
}
if ($name === '') {
return "The value \"<c>{$cellValue}</c>\"{$currentStr} " .
"is {$verb} than the {$prefix}expected \"<green>{$expectedStr}</green>\"";
}
return "The {$name} of the value \"<c>{$cellValue}</c>\"{$currentStr}, " .
"which is {$verb} than the {$prefix}expected \"<green>{$expectedStr}</green>\"";
}
}