1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
<?php
/*
* (c) Markus Lanthaler <mail@markus-lanthaler.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ML\JsonLD\Exception;
use ML\JsonLD\JsonLD;
/**
* Exception class thrown when an error occurs during parsing.
*
* @author Markus Lanthaler <mail@markus-lanthaler.com>
*/
class JsonLdException extends \RuntimeException
{
/**
* An unspecified error code (none was standardized yet)
*/
const UNSPECIFIED = 'unknown';
/**
* The document could not be loaded or parsed as JSON.
*/
const LOADING_DOCUMENT_FAILED = "loading document failed";
/**
* A list of lists was detected. List of lists are not supported in
* this version of JSON-LD due to the algorithmic complexity.
*/
const LIST_OF_LISTS = "list of lists";
/**
* An @index member was encountered whose value was not a string.
*/
const INVALID_INDEX_VALUE = "invalid @index value";
/**
* Multiple conflicting indexes have been found for the same node.
*/
const CONFLICTING_INDEXES = "conflicting indexes";
/**
* An @id member was encountered whose value was not a string.
*/
const INVALID_ID_VALUE = "invalid @id value";
/**
* In invalid local context was detected.
*/
const INVALID_LOCAL_CONTEXT = "invalid local context";
/**
* Multiple HTTP Link Headers [RFC5988] using th
* http://www.w3.org/ns/json-ld#context link relation have been detected.
*/
const MULTIPLE_CONTEXT_LINK_HEADERS = "multiple context link headers";
/**
* There was a problem encountered loading a remote context.
*/
const LOADING_REMOTE_CONTEXT_FAILED = "loading remote context failed";
/**
* No valid context document has been found for a referenced,
* remote context.
*/
const INVALID_REMOTE_CONTEXT = "invalid remote context";
/**
* A cycle in remote context inclusions has been detected.
*/
const RECURSIVE_CONTEXT_INCLUSION = "recursive context inclusion";
/**
* An invalid base IRI has been detected, i.e., it is neither an
* absolute IRI nor null.
*/
const INVALID_BASE_IRI = "invalid base IRI";
/**
* An invalid vocabulary mapping has been detected, i.e., it is
* neither an absolute IRI nor null.
*/
const INVALID_VOCAB_MAPPING = "invalid vocab mapping";
/**
* The value of the default language is not a string or null and
* thus invalid.
*/
const INVALID_DEFAULT_LANGUAGE = "invalid default language";
/**
* A keyword redefinition has been detected.
*/
const KEYWORD_REDEFINITION = "keyword redefinition";
/**
* An invalid term definition has been detected.
*/
const INVALID_TERM_DEFINITION = "invalid term definition";
/**
* An invalid reverse property definition has been detected.
*/
const INVALID_REVERSE_PROPERTY = "invalid reverse property";
/**
* IRI mapping A local context contains a term that has an invalid
* or missing IRI mapping.
*/
const INVALID_IRI_MAPPING = "invalid IRI mapping";
/**
* IRI mapping A cycle in IRI mappings has been detected.
*/
const CYCLIC_IRI_MAPPING = "cyclic IRI mapping";
/**
* An invalid keyword alias definition has been encountered.
*/
const INVALID_KEYWORD_ALIAS = "invalid keyword alias";
/**
* An @type member in a term definition was encountered whose value
* could not be expanded to an absolute IRI.
*/
const INVALID_TYPE_MAPPING = "invalid type mapping";
/**
* An @language member in a term definition was encountered whose
* value was neither a string nor null and thus invalid.
*/
const INVALID_LANGUAGE_MAPPING = "invalid language mapping";
/**
* Two properties which expand to the same keyword have been detected.
* This might occur if a keyword and an alias thereof are used at the
* same time.
*/
const COLLIDING_KEYWORDS = "colliding keywords";
/**
* An @container member was encountered whose value was not one of
* the following strings: @list, @set, or @index.
*/
const INVALID_CONTAINER_MAPPING = "invalid container mapping";
/**
* An invalid value for an @type member has been detected, i.e., the
* value was neither a string nor an array of strings.
*/
const INVALID_TYPE_VALUE = "invalid type value";
/**
* A value object with disallowed members has been detected.
*/
const INVALID_VALUE_OBJECT = "invalid value object";
/**
* An invalid value for the @value member of a value object has been
* detected, i.e., it is neither a scalar nor null.
*/
const INVALID_VALUE_OBJECT_VALUE = "invalid value object value";
/**
* A language-tagged string with an invalid language value was detected.
*/
const INVALID_LANGUAGE_TAGGED_STRING = "invalid language-tagged string";
/**
* A number, true, or false with an associated language tag was detected.
*/
const INVALID_LANGUAGE_TAGGED_VALUE = "invalid language-tagged value";
/**
* A typed value with an invalid type was detected.
*/
const INVALID_TYPED_VALUE = "invalid typed value";
/**
* A set object or list object with disallowed members has been detected.
*/
const INVALID_SET_OR_LIST_OBJECT = "invalid set or list object";
/**
* An invalid value in a language map has been detected. It has to be
* a string or an array of strings.
*/
const INVALID_LANGUAGE_MAP_VALUE = "invalid language map value";
/**
* The compacted document contains a list of lists as multiple lists
* have been compacted to the same term.
*/
const COMPACTION_TO_LIST_OF_LISTS = "compaction to list of lists";
/**
* An invalid reverse property map has been detected. No keywords apart
* from @context are allowed in reverse property maps.
*/
const INVALID_REVERSE_PROPERTY_MAP = "invalid reverse property map";
/**
* An invalid value for an @reverse member has been detected, i.e., the
* value was not a JSON object.
*/
const INVALID_REVERSE_VALUE = "invalid @reverse value";
/**
* An invalid value for a reverse property has been detected. The value
* of an inverse property must be a node object.
*/
const INVALID_REVERSE_PROPERTY_VALUE = "invalid reverse property value";
/**
* The JSON-LD snippet that triggered the error
*
* @var null|string
*/
private $snippet;
/**
* The document that triggered the error
*
* @var null|string
*/
private $document;
/**
* The raw error message (containing place-holders)
*
* @var string
*/
private $rawMessage;
/**
* Constructor.
*
* @param string $code The error code
* @param null|string $message The error message
* @param null|mixed $snippet The code snippet
* @param null|string $document The document that triggered the error
* @param null|\Exception $previous The previous exception
*/
public function __construct($code, $message = null, $snippet = null, $document = null, \Exception $previous = null)
{
$this->code = $code;
$this->document = $document;
$this->snippet = ($snippet) ? JsonLD::toString($snippet) : $snippet;
$this->rawMessage = $message;
$this->updateMessage();
parent::__construct($this->message, 0, $previous);
}
/**
* Gets the snippet of code near the error.
*
* @return null|string The snippet of code
*/
public function getSnippet()
{
return $this->snippet;
}
/**
* Gets the document that triggered the error
*
* @return null|string The document that triggered the error
*/
public function getParsedFile()
{
return $this->document;
}
/**
* Updates the exception message by including the file name if available.
*/
private function updateMessage()
{
$this->message = $this->rawMessage;
$dot = false;
if ('.' === substr($this->message, -1)) {
$this->message = substr($this->message, 0, -1);
$dot = true;
}
if (null !== $this->document) {
$this->message .= sprintf(' in %s', $this->document);
}
if ($this->snippet) {
$this->message .= sprintf(' (near %s)', $this->snippet);
}
if ($dot) {
$this->message .= '.';
}
}
}
|