| 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 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Schema; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
final class Message |
| 12 |
{ |
| 13 |
use \Packetery\Nette\SmartObject; |
| 14 |
/** variables: {value: mixed, expected: string} */ |
| 15 |
public const TYPE_MISMATCH = 'schema.typeMismatch'; |
| 16 |
/** variables: {value: mixed, expected: string} */ |
| 17 |
public const VALUE_OUT_OF_RANGE = 'schema.valueOutOfRange'; |
| 18 |
/** variables: {value: mixed, length: int, expected: string} */ |
| 19 |
public const LENGTH_OUT_OF_RANGE = 'schema.lengthOutOfRange'; |
| 20 |
/** variables: {value: string, pattern: string} */ |
| 21 |
public const PATTERN_MISMATCH = 'schema.patternMismatch'; |
| 22 |
/** variables: {value: mixed, assertion: string} */ |
| 23 |
public const FAILED_ASSERTION = 'schema.failedAssertion'; |
| 24 |
/** no variables */ |
| 25 |
public const MISSING_ITEM = 'schema.missingItem'; |
| 26 |
/** variables: {hint: string} */ |
| 27 |
public const UNEXPECTED_ITEM = 'schema.unexpectedItem'; |
| 28 |
/** no variables */ |
| 29 |
public const DEPRECATED = 'schema.deprecated'; |
| 30 |
/** @var string */ |
| 31 |
public $message; |
| 32 |
/** @var string */ |
| 33 |
public $code; |
| 34 |
/** @var string[] */ |
| 35 |
public $path; |
| 36 |
/** @var string[] */ |
| 37 |
public $variables; |
| 38 |
public function __construct(string $message, string $code, array $path, array $variables = []) |
| 39 |
{ |
| 40 |
$this->message = $message; |
| 41 |
$this->code = $code; |
| 42 |
$this->path = $path; |
| 43 |
$this->variables = $variables; |
| 44 |
} |
| 45 |
public function toString() : string |
| 46 |
{ |
| 47 |
$vars = $this->variables; |
| 48 |
$vars['label'] = empty($vars['isKey']) ? 'item' : 'key of item'; |
| 49 |
$vars['path'] = $this->path ? "'" . \implode(" › ", $this->path) . "'" : null; |
| 50 |
$vars['value'] = Helpers::formatValue($vars['value'] ?? null); |
| 51 |
return \preg_replace_callback('~( ?)%(\\w+)%~', function ($m) use($vars) { |
| 52 |
[, $space, $key] = $m; |
| 53 |
return $vars[$key] === null ? '' : $space . $vars[$key]; |
| 54 |
}, $this->message); |
| 55 |
} |
| 56 |
} |
| 57 |
|