bytic /
utility
| 1 | <?php |
||
| 2 | |||
| 3 | /** @noinspection PhpComposerExtensionStubsInspection */ |
||
| 4 | |||
| 5 | namespace Nip\Utility; |
||
| 6 | |||
| 7 | use DOMDocument; |
||
| 8 | use Exception; |
||
| 9 | use Nip\Utility\Xml\FromArrayBuilder; |
||
| 10 | use SimpleXMLElement; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Class Xml |
||
| 14 | * @package Nip\Utility |
||
| 15 | * |
||
| 16 | * @inspiration https://github.com/cakephp/utility/blob/master/Xml.php |
||
| 17 | */ |
||
| 18 | class Xml |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @param $xml |
||
| 22 | * |
||
| 23 | * @return SimpleXMLElement |
||
| 24 | */ |
||
| 25 | public static function toObject($xml): SimpleXMLElement |
||
| 26 | { |
||
| 27 | return simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | 1 | * @param array $input |
|
| 32 | * @param array $options |
||
| 33 | 1 | * |
|
| 34 | * @return DOMDocument|SimpleXMLElement |
||
| 35 | */ |
||
| 36 | public static function fromArray($input, array $options = []) |
||
| 37 | { |
||
| 38 | return FromArrayBuilder::build($input, $options); |
||
| 39 | } |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * @param $xml |
||
| 44 | * @param $schema |
||
| 45 | * |
||
| 46 | * @throws Exception |
||
| 47 | */ |
||
| 48 | public static function validate($xml, $schema) |
||
| 49 | { |
||
| 50 | libxml_use_internal_errors(true); |
||
| 51 | $xmlDocument = new DOMDocument(); |
||
| 52 | $xmlDocument->loadXML($xml); |
||
| 53 | |||
| 54 | $schema = static::prepareSchema($schema); |
||
| 55 | |||
| 56 | if (!$xmlDocument->schemaValidateSource($schema)) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 57 | $errors = libxml_get_errors(); |
||
| 58 | |||
| 59 | foreach ($errors as $error) { |
||
| 60 | throw new Exception(static::displayError($error)); |
||
| 61 | } |
||
| 62 | libxml_clear_errors(); |
||
| 63 | throw new Exception('INVALID XML'); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param $error |
||
| 69 | * |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | public static function displayError($error): string |
||
| 73 | { |
||
| 74 | $return = "\n"; |
||
| 75 | switch ($error->level) { |
||
| 76 | case LIBXML_ERR_WARNING: |
||
| 77 | $return .= "Warning $error->code: "; |
||
| 78 | break; |
||
| 79 | case LIBXML_ERR_ERROR: |
||
| 80 | $return .= "Error $error->code: "; |
||
| 81 | break; |
||
| 82 | case LIBXML_ERR_FATAL: |
||
| 83 | $return .= "Fatal Error $error->code: "; |
||
| 84 | break; |
||
| 85 | } |
||
| 86 | $return .= trim($error->message); |
||
| 87 | $return .= "\n"; |
||
| 88 | |||
| 89 | return $return; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param $schema |
||
| 94 | * |
||
| 95 | * @return false|string |
||
| 96 | */ |
||
| 97 | protected static function prepareSchema($schema) |
||
| 98 | { |
||
| 99 | if (!Url::isValid($schema)) { |
||
| 100 | return $schema; |
||
| 101 | } |
||
| 102 | |||
| 103 | return file_get_contents($schema); |
||
| 104 | } |
||
| 105 | } |
||
| 106 |