You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.2 KiB
66 lines
2.2 KiB
3 years ago
|
<?php
|
||
3 years ago
|
/** @license MIT
|
||
|
* Copyright 2017 , Dustin Wilson, J. King et al.
|
||
|
* See LICENSE and AUTHORS files for details */
|
||
|
|
||
3 years ago
|
declare(strict_types=1);
|
||
3 years ago
|
namespace MensBeam\HTML\DOM;
|
||
3 years ago
|
|
||
3 years ago
|
class DOMException extends \Exception {
|
||
3 years ago
|
// From PHP's DOMException; keeping error codes consistent
|
||
3 years ago
|
const HIERARCHY_REQUEST_ERROR = 3;
|
||
3 years ago
|
const WRONG_DOCUMENT = 4;
|
||
3 years ago
|
const INVALID_CHARACTER = 5;
|
||
3 years ago
|
const NO_MODIFICATION_ALLOWED = 7;
|
||
3 years ago
|
const NOT_FOUND = 8;
|
||
3 years ago
|
const SYNTAX_ERROR = 12;
|
||
3 years ago
|
|
||
3 years ago
|
const ARGUMENT_TYPE_ERROR = 100;
|
||
|
const OUTER_HTML_FAILED_NOPARENT = 101;
|
||
3 years ago
|
|
||
|
protected static $messages = [
|
||
3 years ago
|
3 => 'Hierarchy request error; supplied node is not allowed here',
|
||
3 years ago
|
4 => 'Supplied node does not belong to this document',
|
||
3 years ago
|
5 => 'Invalid character',
|
||
3 years ago
|
7 => 'Modification not allowed here',
|
||
3 years ago
|
8 => 'Not found error',
|
||
3 years ago
|
12 => 'Syntax error',
|
||
3 years ago
|
100 => 'Argument #%s (\$%s) must be of type %s, %s given',
|
||
|
101 => 'Failed to set the "outerHTML" property; the element does not have a parent node'
|
||
3 years ago
|
];
|
||
|
|
||
|
public function __construct(int $code, ...$args) {
|
||
3 years ago
|
if (!isset(self::$messages[$code])) {
|
||
3 years ago
|
throw new Exception(Exception::INVALID_CODE);
|
||
|
}
|
||
|
|
||
3 years ago
|
$message = self::$messages[$code];
|
||
3 years ago
|
$previous = null;
|
||
|
|
||
|
if ($args) {
|
||
|
// Grab a previous exception if there is one.
|
||
|
if ($args[0] instanceof \Throwable) {
|
||
|
$previous = array_shift($args);
|
||
|
} elseif (end($args) instanceof \Throwable) {
|
||
|
$previous = array_pop($args);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Count the number of replacements needed in the message.
|
||
|
preg_match_all('/(\%(?:\d+\$)?s)/', $message, $matches);
|
||
3 years ago
|
$count = count($matches[1]);
|
||
3 years ago
|
|
||
|
// If the number of replacements don't match the arguments then oops.
|
||
|
if (count($args) !== $count) {
|
||
|
throw new Exception(Exception::INCORRECT_PARAMETERS_FOR_MESSAGE, $count);
|
||
|
}
|
||
|
|
||
|
if ($count > 0) {
|
||
|
// Go through each of the arguments and run sprintf on the strings.
|
||
|
$message = call_user_func_array('sprintf', array_merge([$message], $args));
|
||
|
}
|
||
|
|
||
|
parent::__construct($message, $code, $previous);
|
||
|
}
|
||
|
}
|