Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Util\Xml;
11
 
12
use function sprintf;
13
use function trim;
14
 
15
/**
16
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
17
 *
18
 * @psalm-immutable
19
 */
20
final class ValidationResult
21
{
22
    /**
23
     * @psalm-var array<int,list<string>>
24
     */
25
    private $validationErrors = [];
26
 
27
    /**
28
     * @psalm-param array<int,\LibXMLError> $errors
29
     */
30
    public static function fromArray(array $errors): self
31
    {
32
        $validationErrors = [];
33
 
34
        foreach ($errors as $error) {
35
            if (!isset($validationErrors[$error->line])) {
36
                $validationErrors[$error->line] = [];
37
            }
38
 
39
            $validationErrors[$error->line][] = trim($error->message);
40
        }
41
 
42
        return new self($validationErrors);
43
    }
44
 
45
    private function __construct(array $validationErrors)
46
    {
47
        $this->validationErrors = $validationErrors;
48
    }
49
 
50
    public function hasValidationErrors(): bool
51
    {
52
        return !empty($this->validationErrors);
53
    }
54
 
55
    public function asString(): string
56
    {
57
        $buffer = '';
58
 
59
        foreach ($this->validationErrors as $line => $validationErrorsOnLine) {
60
            $buffer .= sprintf(PHP_EOL . '  Line %d:' . PHP_EOL, $line);
61
 
62
            foreach ($validationErrorsOnLine as $validationError) {
63
                $buffer .= sprintf('  - %s' . PHP_EOL, $validationError);
64
            }
65
        }
66
 
67
        return $buffer;
68
    }
69
}