Complex classes like Schema often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Schema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Schema implements \JsonSerializable { |
||
| 14 | /** |
||
| 15 | * Trigger a notice when extraneous properties are encountered during validation. |
||
| 16 | */ |
||
| 17 | const VALIDATE_EXTRA_PROPERTY_NOTICE = 0x1; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Throw a ValidationException when extraneous properties are encountered during validation. |
||
| 21 | */ |
||
| 22 | const VALIDATE_EXTRA_PROPERTY_EXCEPTION = 0x2; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array All the known types. |
||
| 26 | * |
||
| 27 | * If this is ever given some sort of public access then remove the static. |
||
| 28 | */ |
||
| 29 | private static $types = [ |
||
| 30 | 'array' => ['a'], |
||
| 31 | 'object' => ['o'], |
||
| 32 | 'integer' => ['i', 'int'], |
||
| 33 | 'string' => ['s', 'str'], |
||
| 34 | 'number' => ['f', 'float'], |
||
| 35 | 'boolean' => ['b', 'bool'], |
||
| 36 | 'timestamp' => ['ts'], |
||
| 37 | 'datetime' => ['dt'], |
||
| 38 | 'null' => ['n'] |
||
| 39 | ]; |
||
| 40 | |||
| 41 | private $schema = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int A bitwise combination of the various **Schema::FLAG_*** constants. |
||
| 45 | */ |
||
| 46 | private $flags = 0; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array An array of callbacks that will custom validate the schema. |
||
| 50 | */ |
||
| 51 | private $validators = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string|Validation The name of the class or an instance that will be cloned. |
||
| 55 | */ |
||
| 56 | private $validationClass = Validation::class; |
||
| 57 | |||
| 58 | |||
| 59 | /// Methods /// |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Initialize an instance of a new {@link Schema} class. |
||
| 63 | * |
||
| 64 | * @param array $schema The array schema to validate against. |
||
| 65 | */ |
||
| 66 | 143 | public function __construct($schema = []) { |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Grab the schema's current description. |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | 1 | public function getDescription() { |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Set the description for the schema. |
||
| 81 | * |
||
| 82 | * @param string $description The new description. |
||
| 83 | * @throws \InvalidArgumentException Throws an exception when the provided description is not a string. |
||
| 84 | * @return Schema |
||
| 85 | */ |
||
| 86 | 2 | public function setDescription($description) { |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Return the validation flags. |
||
| 98 | * |
||
| 99 | * @return int Returns a bitwise combination of flags. |
||
| 100 | */ |
||
| 101 | 1 | public function getFlags() { |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Set the validation flags. |
||
| 107 | * |
||
| 108 | * @param int $flags One or more of the **Schema::FLAG_*** constants. |
||
| 109 | * @return Schema Returns the current instance for fluent calls. |
||
| 110 | */ |
||
| 111 | 8 | public function setFlags($flags) { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Whether or not the schema has a flag (or combination of flags). |
||
| 122 | * |
||
| 123 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
| 124 | * @return bool Returns **true** if all of the flags are set or **false** otherwise. |
||
| 125 | */ |
||
| 126 | 8 | public function hasFlag($flag) { |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Set a flag. |
||
| 132 | * |
||
| 133 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
| 134 | * @param bool $value Either true or false. |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | 1 | public function setFlag($flag, $value) { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Merge a schema with this one. |
||
| 148 | * |
||
| 149 | * @param Schema $schema A scheme instance. Its parameters will be merged into the current instance. |
||
| 150 | */ |
||
| 151 | 2 | public function merge(Schema $schema) { |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Returns the internal schema array. |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | * @see Schema::jsonSerialize() |
||
| 181 | */ |
||
| 182 | 11 | public function getSchemaArray() { |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Parse a short schema and return the associated schema. |
||
| 188 | * |
||
| 189 | * @param array $arr The schema array. |
||
| 190 | * @return Schema Returns a new schema. |
||
| 191 | */ |
||
| 192 | 138 | public static function parse(array $arr) { |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Parse a schema in short form into a full schema array. |
||
| 200 | * |
||
| 201 | * @param array $arr The array to parse into a schema. |
||
| 202 | * @return array The full schema array. |
||
| 203 | * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid. |
||
| 204 | */ |
||
| 205 | 138 | protected function parseInternal(array $arr) { |
|
| 206 | 138 | if (empty($arr)) { |
|
| 207 | // An empty schema validates to anything. |
||
| 208 | 5 | return []; |
|
| 209 | 134 | } elseif (isset($arr['type'])) { |
|
| 210 | // This is a long form schema and can be parsed as the root. |
||
| 211 | return $this->parseNode($arr); |
||
| 212 | } else { |
||
| 213 | // Check for a root schema. |
||
| 214 | 134 | $value = reset($arr); |
|
| 215 | 134 | $key = key($arr); |
|
| 216 | 134 | if (is_int($key)) { |
|
| 217 | 83 | $key = $value; |
|
| 218 | 83 | $value = null; |
|
| 219 | } |
||
| 220 | 134 | list ($name, $param) = $this->parseShortParam($key, $value); |
|
| 221 | 134 | if (empty($name)) { |
|
| 222 | 50 | return $this->parseNode($param, $value); |
|
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | // If we are here then this is n object schema. |
||
| 227 | 86 | list($properties, $required) = $this->parseProperties($arr); |
|
| 228 | |||
| 229 | $result = [ |
||
| 230 | 86 | 'type' => 'object', |
|
| 231 | 86 | 'properties' => $properties, |
|
| 232 | 86 | 'required' => $required |
|
| 233 | ]; |
||
| 234 | |||
| 235 | 86 | return array_filter($result); |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Parse a schema node. |
||
| 240 | * |
||
| 241 | * @param array $node The node to parse. |
||
| 242 | * @param mixed $value Additional information from the node. |
||
| 243 | * @return array Returns a JSON schema compatible node. |
||
| 244 | */ |
||
| 245 | 134 | private function parseNode($node, $value = null) { |
|
| 246 | 134 | if (is_array($value)) { |
|
| 247 | // The value describes a bit more about the schema. |
||
| 248 | 50 | switch ($node['type']) { |
|
| 249 | 50 | case 'array': |
|
| 250 | 7 | if (isset($value['items'])) { |
|
| 251 | // The value includes array schema information. |
||
| 252 | 1 | $node = array_replace($node, $value); |
|
| 253 | } else { |
||
| 254 | 6 | $node['items'] = $this->parseInternal($value); |
|
| 255 | } |
||
| 256 | 7 | break; |
|
| 257 | 43 | case 'object': |
|
| 258 | // The value is a schema of the object. |
||
| 259 | 9 | if (isset($value['properties'])) { |
|
| 260 | list($node['properties']) = $this->parseProperties($value['properties']); |
||
| 261 | } else { |
||
| 262 | 9 | list($node['properties'], $required) = $this->parseProperties($value); |
|
| 263 | 9 | if (!empty($required)) { |
|
| 264 | 9 | $node['required'] = $required; |
|
| 265 | } |
||
| 266 | } |
||
| 267 | 9 | break; |
|
| 268 | default: |
||
| 269 | 34 | $node = array_replace($node, $value); |
|
| 270 | 50 | break; |
|
| 271 | } |
||
| 272 | 101 | } elseif (is_string($value)) { |
|
| 273 | 78 | if ($node['type'] === 'array' && $arrType = $this->getType($value)) { |
|
| 274 | 2 | $node['items'] = ['type' => $arrType]; |
|
| 275 | } elseif (!empty($value)) { |
||
| 276 | 22 | $node['description'] = $value; |
|
| 277 | } |
||
| 278 | 26 | } elseif ($value === null) { |
|
| 279 | // Parse child elements. |
||
| 280 | 24 | if ($node['type'] === 'array' && isset($node['items'])) { |
|
| 281 | // The value includes array schema information. |
||
| 282 | $node['items'] = $this->parseInternal($node['items']); |
||
| 283 | 24 | } elseif ($node['type'] === 'object' && isset($node['properties'])) { |
|
| 284 | list($node['properties']) = $this->parseProperties($node['properties']); |
||
| 285 | |||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | |||
| 290 | 134 | return $node; |
|
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Parse the schema for an object's properties. |
||
| 295 | * |
||
| 296 | * @param array $arr An object property schema. |
||
| 297 | * @return array Returns a schema array suitable to be placed in the **properties** key of a schema. |
||
| 298 | */ |
||
| 299 | 86 | private function parseProperties(array $arr) { |
|
| 300 | 86 | $properties = []; |
|
| 301 | 86 | $requiredProperties = []; |
|
| 302 | 86 | foreach ($arr as $key => $value) { |
|
| 303 | // Fix a schema specified as just a value. |
||
| 304 | 86 | if (is_int($key)) { |
|
| 305 | 62 | if (is_string($value)) { |
|
| 306 | 62 | $key = $value; |
|
| 307 | 62 | $value = ''; |
|
| 308 | } else { |
||
| 309 | throw new \InvalidArgumentException("Schema at position $key is not a valid parameter.", 500); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | // The parameter is defined in the key. |
||
| 314 | 86 | list($name, $param, $required) = $this->parseShortParam($key, $value); |
|
| 315 | |||
| 316 | 86 | $node = $this->parseNode($param, $value); |
|
| 317 | |||
| 318 | 86 | $properties[$name] = $node; |
|
| 319 | 86 | if ($required) { |
|
| 320 | 44 | $requiredProperties[] = $name; |
|
| 321 | } |
||
| 322 | } |
||
| 323 | 86 | return array($properties, $requiredProperties); |
|
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Parse a short parameter string into a full array parameter. |
||
| 328 | * |
||
| 329 | * @param string $key The short parameter string to parse. |
||
| 330 | * @param array $value An array of other information that might help resolve ambiguity. |
||
| 331 | * @return array Returns an array in the form `[string name, array param, bool required]`. |
||
| 332 | * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format. |
||
| 333 | */ |
||
| 334 | 134 | public function parseShortParam($key, $value = []) { |
|
| 335 | // Is the parameter optional? |
||
| 336 | 134 | if (substr($key, -1) === '?') { |
|
| 337 | 60 | $required = false; |
|
| 338 | 60 | $key = substr($key, 0, -1); |
|
| 339 | } else { |
||
| 340 | 92 | $required = true; |
|
| 341 | } |
||
| 342 | |||
| 343 | // Check for a type. |
||
| 344 | 134 | $parts = explode(':', $key); |
|
| 345 | 134 | $name = $parts[0]; |
|
| 346 | 134 | $allowNull = false; |
|
| 347 | 134 | if (!empty($parts[1])) { |
|
| 348 | 133 | $types = explode('|', $parts[1]); |
|
| 349 | 133 | foreach ($types as $alias) { |
|
| 350 | 133 | $found = $this->getType($alias); |
|
| 351 | 133 | if ($found === null) { |
|
| 352 | throw new \InvalidArgumentException("Unknown type '$alias'", 500); |
||
| 353 | 133 | } elseif ($found === 'null') { |
|
| 354 | 9 | $allowNull = true; |
|
| 355 | } else { |
||
| 356 | 133 | $type = $found; |
|
| 357 | } |
||
| 358 | } |
||
| 359 | } else { |
||
| 360 | 2 | $type = null; |
|
| 361 | } |
||
| 362 | |||
| 363 | 134 | if ($value instanceof Schema) { |
|
| 364 | 2 | if ($type === 'array') { |
|
| 365 | 1 | $param = ['type' => $type, 'items' => $value]; |
|
| 366 | } else { |
||
| 367 | 1 | $param = $value; |
|
| 368 | } |
||
| 369 | 134 | } elseif (isset($value['type'])) { |
|
| 370 | $param = $value; |
||
| 371 | |||
| 372 | if (!empty($type) && $type !== $param['type']) { |
||
| 373 | throw new \InvalidArgumentException("Type mismatch between $type and {$param['type']} for field $name.", 500); |
||
| 374 | } |
||
| 375 | } else { |
||
| 376 | 134 | if (empty($type) && !empty($parts[1])) { |
|
| 377 | throw new \InvalidArgumentException("Invalid type {$parts[1]} for field $name.", 500); |
||
| 378 | } |
||
| 379 | 134 | $param = ['type' => $type]; |
|
| 380 | |||
| 381 | // Parsed required strings have a minimum length of 1. |
||
| 382 | 134 | if ($type === 'string' && !empty($name) && $required && (!isset($value['default']) || $value['default'] !== '')) { |
|
| 383 | 29 | $param['minLength'] = 1; |
|
| 384 | } |
||
| 385 | } |
||
| 386 | 134 | if ($allowNull) { |
|
| 387 | 9 | $param['allowNull'] = true; |
|
| 388 | } |
||
| 389 | |||
| 390 | 134 | return [$name, $param, $required]; |
|
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Add a custom validator to to validate the schema. |
||
| 395 | * |
||
| 396 | * @param string $fieldname The name of the field to validate, if any. |
||
| 397 | * |
||
| 398 | * If you are adding a validator to a deeply nested field then separate the path with dots. |
||
| 399 | * @param callable $callback The callback to validate with. |
||
| 400 | * @return Schema Returns `$this` for fluent calls. |
||
| 401 | */ |
||
| 402 | 2 | public function addValidator($fieldname, callable $callback) { |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Require one of a given set of fields in the schema. |
||
| 409 | * |
||
| 410 | * @param array $required The field names to require. |
||
| 411 | * @param string $fieldname The name of the field to attach to. |
||
| 412 | * @param int $count The count of required items. |
||
| 413 | * @return Schema Returns `$this` for fluent calls. |
||
| 414 | */ |
||
| 415 | 1 | public function requireOneOf(array $required, $fieldname = '', $count = 1) { |
|
| 416 | 1 | $result = $this->addValidator( |
|
| 417 | $fieldname, |
||
| 418 | function ($data, ValidationField $field) use ($required, $count) { |
||
| 419 | 1 | $hasCount = 0; |
|
|
1 ignored issue
–
show
|
|||
| 420 | 1 | $flattened = []; |
|
| 421 | |||
| 422 | 1 | foreach ($required as $name) { |
|
| 423 | 1 | $flattened = array_merge($flattened, (array)$name); |
|
| 424 | |||
| 425 | 1 | if (is_array($name)) { |
|
| 426 | // This is an array of required names. They all must match. |
||
| 427 | 1 | $hasCountInner = 0; |
|
| 428 | 1 | foreach ($name as $nameInner) { |
|
| 429 | 1 | if (isset($data[$nameInner]) && $data[$nameInner]) { |
|
| 430 | 1 | $hasCountInner++; |
|
| 431 | } else { |
||
| 432 | 1 | break; |
|
| 433 | } |
||
| 434 | } |
||
| 435 | 1 | if ($hasCountInner >= count($name)) { |
|
| 436 | 1 | $hasCount++; |
|
| 437 | } |
||
| 438 | 1 | } elseif (isset($data[$name]) && $data[$name]) { |
|
| 439 | 1 | $hasCount++; |
|
| 440 | } |
||
| 441 | |||
| 442 | 1 | if ($hasCount >= $count) { |
|
| 443 | 1 | return true; |
|
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 447 | 1 | if ($count === 1) { |
|
| 448 | 1 | $message = 'One of {required} are required.'; |
|
| 449 | } else { |
||
| 450 | $message = '{count} of {required} are required.'; |
||
| 451 | } |
||
| 452 | |||
| 453 | 1 | $field->addError('missingField', [ |
|
| 454 | 1 | 'messageCode' => $message, |
|
| 455 | 1 | 'required' => $required, |
|
| 456 | 1 | 'count' => $count |
|
| 457 | ]); |
||
| 458 | 1 | return false; |
|
| 459 | 1 | } |
|
| 460 | ); |
||
| 461 | |||
| 462 | 1 | return $result; |
|
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Validate data against the schema. |
||
| 467 | * |
||
| 468 | * @param mixed $data The data to validate. |
||
| 469 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 470 | * @return mixed Returns a cleaned version of the data. |
||
| 471 | * @throws ValidationException Throws an exception when the data does not validate against the schema. |
||
| 472 | */ |
||
| 473 | 117 | public function validate($data, $sparse = false) { |
|
| 474 | 117 | $field = new ValidationField($this->createValidation(), $this->schema, ''); |
|
| 475 | |||
| 476 | 117 | $clean = $this->validateField($data, $field, $sparse); |
|
| 477 | |||
| 478 | 115 | if (Invalid::isInvalid($clean) && $field->isValid()) { |
|
| 479 | // This really shouldn't happen, but we want to protect against seeing the invalid object. |
||
| 480 | $field->addError('invalid', ['messageCode' => '{field} is invalid.', 'status' => 422]); |
||
| 481 | } |
||
| 482 | |||
| 483 | 115 | if (!$field->getValidation()->isValid()) { |
|
| 484 | 63 | throw new ValidationException($field->getValidation()); |
|
| 485 | } |
||
| 486 | |||
| 487 | 71 | return $clean; |
|
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Validate data against the schema and return the result. |
||
| 492 | * |
||
| 493 | * @param mixed $data The data to validate. |
||
| 494 | * @param bool $sparse Whether or not to do a sparse validation. |
||
| 495 | * @return bool Returns true if the data is valid. False otherwise. |
||
| 496 | */ |
||
| 497 | 33 | public function isValid($data, $sparse = false) { |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Validate a field. |
||
| 508 | * |
||
| 509 | * @param mixed $value The value to validate. |
||
| 510 | * @param ValidationField $field A validation object to add errors to. |
||
| 511 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 512 | * @return mixed|Invalid Returns a clean version of the value with all extra fields stripped out or invalid if the value |
||
| 513 | * is completely invalid. |
||
| 514 | */ |
||
| 515 | 117 | protected function validateField($value, ValidationField $field, $sparse = false) { |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Validate an array. |
||
| 576 | * |
||
| 577 | * @param mixed $value The value to validate. |
||
| 578 | * @param ValidationField $field The validation results to add. |
||
| 579 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 580 | * @return array|Invalid Returns an array or invalid if validation fails. |
||
| 581 | */ |
||
| 582 | 13 | protected function validateArray($value, ValidationField $field, $sparse = false) { |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Validate a boolean value. |
||
| 616 | * |
||
| 617 | * @param mixed $value The value to validate. |
||
| 618 | * @param ValidationField $field The validation results to add. |
||
| 619 | * @return bool|Invalid Returns the cleaned value or invalid if validation fails. |
||
| 620 | */ |
||
| 621 | 21 | protected function validateBoolean($value, ValidationField $field) { |
|
| 629 | |||
| 630 | /** |
||
| 631 | * Validate a date time. |
||
| 632 | * |
||
| 633 | * @param mixed $value The value to validate. |
||
| 634 | * @param ValidationField $field The validation results to add. |
||
| 635 | * @return \DateTimeInterface|Invalid Returns the cleaned value or **null** if it isn't valid. |
||
| 636 | */ |
||
| 637 | 13 | protected function validateDatetime($value, ValidationField $field) { |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Validate a float. |
||
| 665 | * |
||
| 666 | * @param mixed $value The value to validate. |
||
| 667 | * @param ValidationField $field The validation results to add. |
||
| 668 | * @return float|Invalid Returns a number or **null** if validation fails. |
||
| 669 | */ |
||
| 670 | 9 | protected function validateNumber($value, ValidationField $field) { |
|
| 678 | |||
| 679 | /** |
||
| 680 | * Validate and integer. |
||
| 681 | * |
||
| 682 | * @param mixed $value The value to validate. |
||
| 683 | * @param ValidationField $field The validation results to add. |
||
| 684 | * @return int|Invalid Returns the cleaned value or **null** if validation fails. |
||
| 685 | */ |
||
| 686 | 23 | protected function validateInteger($value, ValidationField $field) { |
|
| 695 | |||
| 696 | /** |
||
| 697 | * Validate an object. |
||
| 698 | * |
||
| 699 | * @param mixed $value The value to validate. |
||
| 700 | * @param ValidationField $field The validation results to add. |
||
| 701 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 702 | * @return object|Invalid Returns a clean object or **null** if validation fails. |
||
| 703 | */ |
||
| 704 | 77 | protected function validateObject($value, ValidationField $field, $sparse = false) { |
|
| 716 | |||
| 717 | /** |
||
| 718 | * Validate data against the schema and return the result. |
||
| 719 | * |
||
| 720 | * @param array|\ArrayAccess $data The data to validate. |
||
| 721 | * @param ValidationField $field This argument will be filled with the validation result. |
||
| 722 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 723 | * @return array|Invalid Returns a clean array with only the appropriate properties and the data coerced to proper types. |
||
| 724 | * or invalid if there are no valid properties. |
||
| 725 | */ |
||
| 726 | 76 | protected function validateProperties($data, ValidationField $field, $sparse = false) { |
|
| 783 | |||
| 784 | /** |
||
| 785 | * Validate a string. |
||
| 786 | * |
||
| 787 | * @param mixed $value The value to validate. |
||
| 788 | * @param ValidationField $field The validation results to add. |
||
| 789 | * @return string|Invalid Returns the valid string or **null** if validation fails. |
||
| 790 | */ |
||
| 791 | 52 | protected function validateString($value, ValidationField $field) { |
|
| 880 | |||
| 881 | /** |
||
| 882 | * Validate a unix timestamp. |
||
| 883 | * |
||
| 884 | * @param mixed $value The value to validate. |
||
| 885 | * @param ValidationField $field The field being validated. |
||
| 886 | * @return int|Invalid Returns a valid timestamp or invalid if the value doesn't validate. |
||
| 887 | */ |
||
| 888 | 8 | protected function validateTimestamp($value, ValidationField $field) { |
|
| 899 | |||
| 900 | /** |
||
| 901 | * Validate a null value. |
||
| 902 | * |
||
| 903 | * @param mixed $value The value to validate. |
||
| 904 | * @param ValidationField $field The error collector for the field. |
||
| 905 | * @return null|Invalid Returns **null** or invalid. |
||
| 906 | */ |
||
| 907 | protected function validateNull($value, ValidationField $field) { |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Validate a value against an enum. |
||
| 917 | * |
||
| 918 | * @param mixed $value The value to test. |
||
| 919 | * @param ValidationField $field The validation object for adding errors. |
||
| 920 | * @return mixed|Invalid Returns the value if it is one of the enumerated values or invalid otherwise. |
||
| 921 | */ |
||
| 922 | 115 | protected function validateEnum($value, ValidationField $field) { |
|
| 941 | |||
| 942 | /** |
||
| 943 | * Call all of the validators attached to a field. |
||
| 944 | * |
||
| 945 | * @param mixed $value The field value being validated. |
||
| 946 | * @param ValidationField $field The validation object to add errors. |
||
| 947 | */ |
||
| 948 | 115 | protected function callValidators($value, ValidationField $field) { |
|
| 968 | |||
| 969 | /** |
||
| 970 | * Specify data which should be serialized to JSON. |
||
| 971 | * |
||
| 972 | * This method specifically returns data compatible with the JSON schema format. |
||
| 973 | * |
||
| 974 | * @return mixed Returns data which can be serialized by **json_encode()**, which is a value of any type other than a resource. |
||
| 975 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
| 976 | * @link http://json-schema.org/ |
||
| 977 | */ |
||
| 978 | public function jsonSerialize() { |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Look up a type based on its alias. |
||
| 1016 | * |
||
| 1017 | * @param string $alias The type alias or type name to lookup. |
||
| 1018 | * @return mixed |
||
| 1019 | */ |
||
| 1020 | 133 | protected function getType($alias) { |
|
| 1031 | |||
| 1032 | /** |
||
| 1033 | * Get the class that's used to contain validation information. |
||
| 1034 | * |
||
| 1035 | * @return Validation|string Returns the validation class. |
||
| 1036 | */ |
||
| 1037 | 117 | public function getValidationClass() { |
|
| 1040 | |||
| 1041 | /** |
||
| 1042 | * Set the class that's used to contain validation information. |
||
| 1043 | * |
||
| 1044 | * @param Validation|string $class Either the name of a class or a class that will be cloned. |
||
| 1045 | * @return $this |
||
| 1046 | */ |
||
| 1047 | 1 | public function setValidationClass($class) { |
|
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Create a new validation instance. |
||
| 1058 | * |
||
| 1059 | * @return Validation Returns a validation object. |
||
| 1060 | */ |
||
| 1061 | 117 | protected function createValidation() { |
|
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Check whether or not a value is an array or accessible like an array. |
||
| 1074 | * |
||
| 1075 | * @param mixed $value The value to check. |
||
| 1076 | * @return bool Returns **true** if the value can be used like an array or **false** otherwise. |
||
| 1077 | */ |
||
| 1078 | 77 | private function isArray($value) { |
|
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Cast a value to an array. |
||
| 1084 | * |
||
| 1085 | * @param \Traversable $value The value to convert. |
||
| 1086 | * @return array Returns an array. |
||
| 1087 | */ |
||
| 1088 | private function toArray(\Traversable $value) { |
||
| 1094 | } |
||
| 1095 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.