<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
m1x0n / opis-json-schema-error-presenter example snippets
use Opis\JsonSchema\Schema;
use Opis\JsonSchema\ValidationResult;
use Opis\JsonSchema\Validator;
use OpisErrorPresenter\Contracts\PresentedValidationError;
use OpisErrorPresenter\Implementation\MessageFormatterFactory;
use OpisErrorPresenter\Implementation\PresentedValidationErrorFactory;
use OpisErrorPresenter\Implementation\ValidationErrorPresenter;
"minLength": 3
},
"price": {
"type": "object",
"properties": {
"amount": {
"type": "integer",
"minimum": 0,
"maximum": 1000
},
"currency": {
"type": "string",
"enum": ["USD", "EUR", "BTC"]
}
},
"pected presenter error
print_r(array_map(static function (PresentedValidationError $error) {
return $error->toArray();
}, $presented));
// Json-serializable
echo json_encode($presented);
$presenter = new ValidationErrorPresenter(
new PresentedValidationErrorFactory(
new MessageFormatterFactory()
),
new BestMatchError()
);
declare(strict_types=1);
namespace Acme;
use OpisErrorPresenter\Contracts\Keyword;
use OpisErrorPresenter\Implementation\MessageFormatterFactory;
use OpisErrorPresenter\Implementation\PresentedValidationErrorFactory;
use OpisErrorPresenter\Implementation\Translators\DefaultTranslator;
use OpisErrorPresenter\Implementation\ValidationErrorPresenter;
class InternationalTranslator extends DefaultTranslator
{
protected $messages = [];
private const DEFAULT_MESSAGE = 'The attribute is invalid';
public function __construct()
{
parent::__construct();
$this->loadMessages();
}
public function translate(string $key, array $replacements = [], $locale = null): string
{
if ($locale && array_key_exists($locale, $this->messages)) {
$message = $this->messages[$locale][$key] ?? self::DEFAULT_MESSAGE;
return strtr($message, $replacements);
}
// Fallback on default locale
return parent::translate($key, $replacements, $locale);
}
private function loadMessages(): void
{
/*
Locales structure example:
[
'locale_1' => [
'keyword' => 'translation_1'
...
],
'locale_2' => [
'keyword' => 'translation_2'
...
],
...
]
*/
$this->messages = [
'de_DE' => [
// The rest of other keywords ...
Keyword::MIN_LENGTH => 'Die Attributlänge sollte mindestens betragen: min: Zeichen.'
// ...
],
'ru_RU' => [
// ...
Keyword::ENUM => 'Длина атрибута должна быть минимум :min: символов.'
// ....
]
];
}
}
// Then configure presenter factory
$presenter = new ValidationErrorPresenter(
new PresentedValidationErrorFactory(
new MessageFormatterFactory(),
new InternationalTranslator()
)
);
$presenter = new ValidationErrorPresenter(
new PresentedValidationErrorFactory(
new MessageFormatterFactory(),
new InternationalTranslator(),
new HttpLocaleResolver()
)
);
$presenter = new ValidationErrorPresenter(
new PresentedValidationErrorFactory(
new MessageFormatterFactory(),
new DefaultTranslator(
(new ArrayLocaleLoader())->addPath('de', '/path_to_lang/lang/de.php')
)
)
);
$presenter = new ValidationErrorPresenter(
new PresentedValidationErrorFactory(
new MessageFormatterFactory(),
new DefaultTranslator(
(new ArrayLocaleLoader())->addPath('cs', '../lang')
),
new FixedLocaleResolver('cs')
)
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.