Repository navigation
Expand file tree
/
Copy pathValidationError.php
More file actions
89 lines (75 loc) · 1.9 KB
/
Copy pathValidationError.php
File metadata and controls
89 lines (75 loc) · 1.9 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
<?php
declare(strict_types=1);
namespace Bfabio\PublicCodeParser;
class ValidationError
{
private int $line;
private int $column;
private string $key;
private string $description;
public function __construct(int $line, int $column, string $key, string $description)
{
$this->line = $line;
$this->column = $column;
$this->key = $key;
$this->description = $description;
}
public function getType(): string
{
return 'error';
}
public function getLine(): int
{
return $this->line;
}
public function getColumn(): int
{
return $this->column;
}
public function getKey(): string
{
return $this->key;
}
public function getDescription(): string
{
return $this->description;
}
public function __toString(): string
{
$key = $this->key !== '' ? $this->key . ': ' : '';
return sprintf(
'publiccode.yml:%d:%d: error: %s%s',
$this->line,
$this->column,
$key,
$this->description,
);
}
/** @param array<string, mixed> $data */
public static function fromArray(array $data): self
{
return new self(
(int) ($data['line'] ?? 0),
(int) ($data['column'] ?? 0),
(string) ($data['key'] ?? ''),
(string) ($data['description'] ?? ''),
);
}
public static function fromString(string $message): self
{
$matches = [];
if (preg_match(
'/^publiccode\.yml:(\d+):(\d+): error: (?:(.*?): )?(.*)$/s',
$message,
$matches,
) === 1) {
return new self(
(int) $matches[1],
(int) $matches[2],
$matches[3],
$matches[4],
);
}
return new self(0, 0, '', $message);
}
}