XOrderValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 1
cbo 3
dl 0
loc 72
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSchema() 0 12 3
A validate() 0 16 2
1
<?php
2
3
/**
4
 * xOrder Validator.
5
 *
6
 * @package     craftt/xorder-sdk
7
 * @author      Ryan Stratton <[email protected]>
8
 * @copyright   Copyright (c) Ryan Stratton
9
 * @license     https://github.com/craftt/xorder-php-sdk/blob/master/LICENSE.md Apache 2.0
10
 * @link        https://github.com/craftt/xorder-php-sdk
11
 */
12
13
namespace XOrder;
14
15
use XOrder\Exceptions\FileDoesNotExistException;
16
use XOrder\Exceptions\XOrderValidationException;
17
18
/**
19
 * XOrder
20
 */
21
class XOrderValidator
22
{
23
24
    /**
25
     * @var array
26
     */
27
    public $errors = [];
28
29
    /**
30
     * @var string
31
     */
32
    public $schema = '/Schema/XOrderSchema.xsd';
33
34
    /**
35
     * @var \XOrder\XOrder
36
     */
37
    public $xorder;
38
39
    /**
40
     * Constructor
41
     *
42
     * @param XOrder\XOrder $xorder
43
     */
44 12
    public function __construct(XOrder $xorder)
45
    {
46 12
        $this->xorder = $xorder;
47 12
    }
48
49
    /**
50
     * Get the schema file.
51
     *
52
     * @param  string|null $schema
53
     * @return string
54
     */
55 10
    public function getSchema($schema = null)
56
    {
57 10
        if (is_null($schema)) {
58 6
            return __DIR__ . $this->schema;
59
        }
60
61 4
        if (!file_exists($schema)) {
62 2
            throw new FileDoesNotExistException('The schema file does not exists.');
63
        }
64
65 2
        return $schema;
66
    }
67
68
    /**
69
     * Validate the xml document against the xOrder Schema.  Or
70
     * you can pass in the path to your preferred schema.
71
     *
72
     * @param  string|null $schema
73
     * @throws \XOrder\Exceptions\XOrderValidationException
74
     * @return boolean
75
     */
76 4
    public function validate($schema = null)
77
    {
78 4
        libxml_use_internal_errors(true);
79
80 4
        $dom = dom_import_simplexml($this->xorder->xml)->ownerDocument;
81 4
        $valid = $dom->schemaValidate($this->getSchema($schema));
82
83 4
        if (!$valid) {
84 2
            $errors = libxml_get_errors();
85 2
            throw new XOrderValidationException($errors);
86
        }
87
88 2
        libxml_use_internal_errors(false);
89
90 2
        return $valid;
91
    }
92
}
93