1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lamoda\GS1Parser\Validator; |
6
|
|
|
|
7
|
|
|
use Lamoda\GS1Parser\Exception\InvalidBarcodeException; |
8
|
|
|
use Lamoda\GS1Parser\Parser\ParserInterface; |
9
|
|
|
|
10
|
|
|
final class Validator implements ValidatorInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ParserInterface |
14
|
|
|
*/ |
15
|
|
|
private $parser; |
16
|
|
|
/** |
17
|
|
|
* @var ValidatorConfig |
18
|
|
|
*/ |
19
|
|
|
private $config; |
20
|
|
|
|
21
|
8 |
|
public function __construct(ParserInterface $parser, ValidatorConfig $config) |
22
|
|
|
{ |
23
|
8 |
|
$this->parser = $parser; |
24
|
8 |
|
$this->config = $config; |
25
|
8 |
|
} |
26
|
|
|
|
27
|
8 |
|
public function validate($value): Resolution |
28
|
|
|
{ |
29
|
8 |
|
if ($value === null && $this->config->isAllowEmpty()) { |
30
|
1 |
|
return Resolution::createValid(); |
31
|
|
|
} |
32
|
|
|
|
33
|
7 |
|
if (!is_string($value)) { |
34
|
2 |
|
return Resolution::createInvalid([ |
35
|
2 |
|
ErrorCodes::VALUE_IS_NOT_STRING => 'Value is not a string', |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
5 |
|
$trimmedValue = trim($value); |
40
|
|
|
|
41
|
5 |
|
if ($trimmedValue === '') { |
42
|
2 |
|
return $this->config->isAllowEmpty() ? |
43
|
1 |
|
Resolution::createValid() : |
44
|
1 |
|
Resolution::createInvalid([ |
45
|
2 |
|
ErrorCodes::VALUE_EMPTY => 'Value is empty', |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
try { |
50
|
3 |
|
$barcode = $this->parser->parse($trimmedValue); |
51
|
|
|
} catch (InvalidBarcodeException $exception) { |
|
|
|
|
52
|
|
|
return Resolution::createInvalid([ |
53
|
|
|
ErrorCodes::INVALID_VALUE => sprintf( |
54
|
|
|
'Value is invalid: %s', |
55
|
|
|
$exception->getCode() |
56
|
|
|
), |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
$ais = array_keys($barcode->ais()); |
61
|
3 |
|
$requiredAis = array_diff($this->config->getRequiredAIs(), $ais); |
62
|
3 |
|
$forbiddenAIs = array_intersect($this->config->getForbiddenAIs(), $ais); |
63
|
|
|
|
64
|
3 |
View Code Duplication |
if (count($requiredAis) > 0) { |
|
|
|
|
65
|
1 |
|
return Resolution::createInvalid([ |
66
|
1 |
|
ErrorCodes::MISSING_AIS => sprintf( |
67
|
1 |
|
'AIs are missing: "%s"', |
68
|
1 |
|
implode('", "', $requiredAis) |
69
|
|
|
), |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
View Code Duplication |
if (count($forbiddenAIs) > 0) { |
|
|
|
|
74
|
1 |
|
return Resolution::createInvalid([ |
75
|
1 |
|
ErrorCodes::FORBIDDEN_AIS => sprintf( |
76
|
1 |
|
'AIs are forbidden: "%s"', |
77
|
1 |
|
implode('", "', $forbiddenAIs) |
78
|
|
|
), |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
return Resolution::createValid(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.