Web Interfaces of PROSUME

Validator.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * This file is part of the JsonSchema package.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JsonSchema;
  9. use JsonSchema\Constraints\BaseConstraint;
  10. use JsonSchema\Constraints\Constraint;
  11. /**
  12. * A JsonSchema Constraint
  13. *
  14. * @author Robert Schönthal <seroscho@googlemail.com>
  15. * @author Bruno Prieto Reis <bruno.p.reis@gmail.com>
  16. *
  17. * @see README.md
  18. */
  19. class Validator extends BaseConstraint
  20. {
  21. const SCHEMA_MEDIA_TYPE = 'application/schema+json';
  22. const ERROR_NONE = 0x00000000;
  23. const ERROR_ALL = 0xFFFFFFFF;
  24. const ERROR_DOCUMENT_VALIDATION = 0x00000001;
  25. const ERROR_SCHEMA_VALIDATION = 0x00000002;
  26. /**
  27. * Validates the given data against the schema and returns an object containing the results
  28. * Both the php object and the schema are supposed to be a result of a json_decode call.
  29. * The validation works as defined by the schema proposal in http://json-schema.org.
  30. *
  31. * Note that the first argument is passwd by reference, so you must pass in a variable.
  32. *
  33. * {@inheritdoc}
  34. */
  35. public function validate(&$value, $schema = null, $checkMode = null)
  36. {
  37. // make sure $schema is an object
  38. if (is_array($schema)) {
  39. $schema = self::arrayToObjectRecursive($schema);
  40. }
  41. // set checkMode
  42. $initialCheckMode = $this->factory->getConfig();
  43. if ($checkMode !== null) {
  44. $this->factory->setConfig($checkMode);
  45. }
  46. // add provided schema to SchemaStorage with internal URI to allow internal $ref resolution
  47. if (is_object($schema) && property_exists($schema, 'id')) {
  48. $schemaURI = $schema->id;
  49. } else {
  50. $schemaURI = SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI;
  51. }
  52. $this->factory->getSchemaStorage()->addSchema($schemaURI, $schema);
  53. $validator = $this->factory->createInstanceFor('schema');
  54. $validator->check(
  55. $value,
  56. $this->factory->getSchemaStorage()->getSchema($schemaURI)
  57. );
  58. $this->factory->setConfig($initialCheckMode);
  59. $this->addErrors(array_unique($validator->getErrors(), SORT_REGULAR));
  60. return $validator->getErrorMask();
  61. }
  62. /**
  63. * Alias to validate(), to maintain backwards-compatibility with the previous API
  64. */
  65. public function check($value, $schema)
  66. {
  67. return $this->validate($value, $schema);
  68. }
  69. /**
  70. * Alias to validate(), to maintain backwards-compatibility with the previous API
  71. */
  72. public function coerce(&$value, $schema)
  73. {
  74. return $this->validate($value, $schema, Constraint::CHECK_MODE_COERCE_TYPES);
  75. }
  76. }