PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Andrey Iatsenko   PHP Validate XML Against XSD   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Validate XML Against XSD
Validate XML documents with XSD schema files
Author: By
Last change:
Date: 2 months ago
Size: 1,800 bytes
 

Contents

Class file image Download

ValidatorXSD is a facade over DOMDocument.

Packagist Version Packagist Downloads Packagist Downloads

ValidatorXSD is a DOMDocument facade that will allow you to more conveniently validate your XML file and also localize errors.

:scroll: Installation

The package can be installed via composer:

composer require yzen.dev/validator-xsd

:scroll: Features

  • Simple use
  • Casting LibXMLError errors to ErrorXSD
  • Parsing fields, rules and more from an error
  • The ability to localize validator errors

:scroll: 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}" ?????? ??????????? ?????.',
        ];
    }
}