yzen.dev / validator-xsd
ValidatorXSD is a facade over DOMDocument that will allow you to more conveniently validate your XML file.
Installs: 5 377
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.4|^8.0
- ext-dom: *
- ext-libxml: *
Requires (Dev)
- mockery/mockery: ^1.0
- phpstan/phpstan: ^0.12.37
- phpunit/phpunit: ^9.3
- squizlabs/php_codesniffer: *
README
ValidatorXSD is a DOMDocument facade that will allow you to more conveniently validate your XML file and also localize errors.
📜 Installation
The package can be installed via composer:
composer require yzen.dev/validator-xsd
📜 Features
- Simple use
- Casting LibXMLError errors to ErrorXSD
- Parsing fields, rules and more from an error
- The ability to localize validator errors
📜 Usage
Validate xml by schema:
$data = '<XML>...</XML>'; $validator = ValidatorXSD::init($data) ->loadSchema( './XSD/request.xsd') ->setLocalization(CustomLocalizationXSD::class); echo $validator->validate();
Get all error:
if (!$validator->validate()) { foreach ($validator->getErrors() as $error) { var_dump($error); } }
Pluck result and group by field:
$errors = $validator->getErrors() ->pluck(['element','message']) ->groupBy('element');
Create custom localization
class CustomLocalizationXSD implements LocalizationXSD { public function customAttributes(): array { return [ 'Country' => 'Страна', 'Province' => 'Область', ]; } public function messages(): array { return [ 'minLength' => 'Поле "${field}" меньше минимальной длины.', ]; } }