| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* This file is part of the Nette Framework (https://nette.org)
|
|
|
5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
|
|
6 |
*/
|
|
|
7 |
|
|
|
8 |
declare(strict_types=1);
|
|
|
9 |
|
|
|
10 |
namespace Nette\Schema;
|
|
|
11 |
|
|
|
12 |
use Nette;
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
final class Message
|
|
|
16 |
{
|
|
|
17 |
use Nette\SmartObject;
|
|
|
18 |
|
|
|
19 |
/** variables: {value: mixed, expected: string} */
|
|
|
20 |
public const TYPE_MISMATCH = 'schema.typeMismatch';
|
|
|
21 |
|
|
|
22 |
/** variables: {value: mixed, expected: string} */
|
|
|
23 |
public const VALUE_OUT_OF_RANGE = 'schema.valueOutOfRange';
|
|
|
24 |
|
|
|
25 |
/** variables: {value: mixed, length: int, expected: string} */
|
|
|
26 |
public const LENGTH_OUT_OF_RANGE = 'schema.lengthOutOfRange';
|
|
|
27 |
|
|
|
28 |
/** variables: {value: string, pattern: string} */
|
|
|
29 |
public const PATTERN_MISMATCH = 'schema.patternMismatch';
|
|
|
30 |
|
|
|
31 |
/** variables: {value: mixed, assertion: string} */
|
|
|
32 |
public const FAILED_ASSERTION = 'schema.failedAssertion';
|
|
|
33 |
|
|
|
34 |
/** no variables */
|
|
|
35 |
public const MISSING_ITEM = 'schema.missingItem';
|
|
|
36 |
|
|
|
37 |
/** variables: {hint: string} */
|
|
|
38 |
public const UNEXPECTED_ITEM = 'schema.unexpectedItem';
|
|
|
39 |
|
|
|
40 |
/** no variables */
|
|
|
41 |
public const DEPRECATED = 'schema.deprecated';
|
|
|
42 |
|
|
|
43 |
/** @var string */
|
|
|
44 |
public $message;
|
|
|
45 |
|
|
|
46 |
/** @var string */
|
|
|
47 |
public $code;
|
|
|
48 |
|
|
|
49 |
/** @var string[] */
|
|
|
50 |
public $path;
|
|
|
51 |
|
|
|
52 |
/** @var string[] */
|
|
|
53 |
public $variables;
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
public function __construct(string $message, string $code, array $path, array $variables = [])
|
|
|
57 |
{
|
|
|
58 |
$this->message = $message;
|
|
|
59 |
$this->code = $code;
|
|
|
60 |
$this->path = $path;
|
|
|
61 |
$this->variables = $variables;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
public function toString(): string
|
|
|
66 |
{
|
|
|
67 |
$vars = $this->variables;
|
|
|
68 |
$vars['label'] = empty($vars['isKey']) ? 'item' : 'key of item';
|
|
|
69 |
$vars['path'] = $this->path
|
|
|
70 |
? "'" . implode("\u{a0}›\u{a0}", $this->path) . "'"
|
|
|
71 |
: null;
|
|
|
72 |
$vars['value'] = Helpers::formatValue($vars['value'] ?? null);
|
|
|
73 |
|
|
|
74 |
return preg_replace_callback('~( ?)%(\w+)%~', function ($m) use ($vars) {
|
|
|
75 |
[, $space, $key] = $m;
|
|
|
76 |
return $vars[$key] === null ? '' : $space . $vars[$key];
|
|
|
77 |
}, $this->message);
|
|
|
78 |
}
|
|
|
79 |
}
|