-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReportBag.php
104 lines (87 loc) · 2.74 KB
/
ReportBag.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
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
/*
* This file is part of the TransMaintain.
*
* (c) Anatoliy Melnikov <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Aeliot\Bundle\TransMaintain\Model;
use Symfony\Component\OptionsResolver\OptionsResolver;
final class ReportBag
{
/**
* @var string[]
*/
private array $columns;
private int $columnsCount;
/**
* @var ReportLineInterface[]
*/
private array $lines = [];
private string $messageEmptyReport;
private string $messagePrefix = 'Translation files linter report: ';
private string $messageReportWithErrors;
private OptionsResolver $resolver;
/**
* @param array<string,string|array<string>> $columnsConfig
*/
public function __construct(array $columnsConfig, string $messageEmptyReport, string $messageReportWithErrors)
{
if (!$columnsConfig) {
throw new \InvalidArgumentException('Columns are not configured');
}
$this->columns = array_keys($columnsConfig);
$this->messageEmptyReport = $messageEmptyReport;
$this->messageReportWithErrors = $messageReportWithErrors;
$this->columnsCount = \count($this->columns);
$this->resolver = new OptionsResolver();
$this->resolver->setDefaults(array_fill_keys($this->columns, null));
foreach ($columnsConfig as $column => $allowedTypes) {
$this->resolver->setAllowedTypes($column, $allowedTypes);
}
}
/**
* @param string|string[]|mixed ...$values
*/
public function addLine(...$values): void
{
if (\count($values) !== $this->columnsCount) {
throw new \InvalidArgumentException('Invalid values count');
}
/** @var array<int|string,mixed> $options */
$options = array_combine($this->columns, $values);
$this->lines[] = new ReportLine($this->resolver->resolve($options));
}
/**
* @return string[]
*/
public function getHeaders(): array
{
return $this->columns;
}
/**
* @return ReportLineInterface[]
*/
public function getLines(): array
{
return $this->lines;
}
public function getPriorMessage(): string
{
if ($this->isEmpty()) {
return sprintf('%s<info>%s</info>', $this->messagePrefix, $this->messageEmptyReport);
}
return sprintf('%s<fg=black;bg=yellow>%s</>', $this->messagePrefix, $this->messageReportWithErrors);
}
public function isEmpty(): bool
{
return !$this->lines;
}
public function setMessagePrefix(string $messagePrefix): void
{
$this->messagePrefix = $messagePrefix;
}
}