PageRenderTime 27ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/session24/Home Practice/Mobile Management System/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php

https://gitlab.com/imamul68e/137619_PHP31
PHP | 286 lines | 188 code | 30 blank | 68 comment | 56 complexity | 676b31ab36e941f4d1a977bef8ecf835 MD5 | raw file
  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\Constraints;
  9. use JsonSchema\Exception\InvalidArgumentException;
  10. use JsonSchema\Uri\UriResolver;
  11. /**
  12. * The UndefinedConstraint Constraints
  13. *
  14. * @author Robert Schönthal <seroscho@googlemail.com>
  15. * @author Bruno Prieto Reis <bruno.p.reis@gmail.com>
  16. */
  17. class UndefinedConstraint extends Constraint
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function check($value, $schema = null, $path = null, $i = null)
  23. {
  24. if (is_null($schema) || !is_object($schema)) {
  25. return;
  26. }
  27. $i = is_null($i) ? "" : $i;
  28. $path = $this->incrementPath($path, $i);
  29. // check special properties
  30. $this->validateCommonProperties($value, $schema, $path);
  31. // check allOf, anyOf, and oneOf properties
  32. $this->validateOfProperties($value, $schema, $path);
  33. // check known types
  34. $this->validateTypes($value, $schema, $path, $i);
  35. }
  36. /**
  37. * Validates the value against the types
  38. *
  39. * @param mixed $value
  40. * @param mixed $schema
  41. * @param string $path
  42. * @param string $i
  43. */
  44. public function validateTypes($value, $schema = null, $path = null, $i = null)
  45. {
  46. // check array
  47. if (is_array($value)) {
  48. $this->checkArray($value, $schema, $path, $i);
  49. }
  50. // check object
  51. if (is_object($value)) {
  52. $this->checkObject(
  53. $value,
  54. isset($schema->properties) ? $schema->properties : $schema,
  55. $path,
  56. isset($schema->additionalProperties) ? $schema->additionalProperties : null,
  57. isset($schema->patternProperties) ? $schema->patternProperties : null
  58. );
  59. }
  60. // check string
  61. if (is_string($value)) {
  62. $this->checkString($value, $schema, $path, $i);
  63. }
  64. // check numeric
  65. if (is_numeric($value)) {
  66. $this->checkNumber($value, $schema, $path, $i);
  67. }
  68. // check enum
  69. if (isset($schema->enum)) {
  70. $this->checkEnum($value, $schema, $path, $i);
  71. }
  72. }
  73. /**
  74. * Validates common properties
  75. *
  76. * @param mixed $value
  77. * @param mixed $schema
  78. * @param string $path
  79. * @param string $i
  80. */
  81. protected function validateCommonProperties($value, $schema = null, $path = null, $i = "")
  82. {
  83. // if it extends another schema, it must pass that schema as well
  84. if (isset($schema->extends)) {
  85. if (is_string($schema->extends)) {
  86. $schema->extends = $this->validateUri($schema, $schema->extends);
  87. }
  88. if (is_array($schema->extends)) {
  89. foreach ($schema->extends as $extends) {
  90. $this->checkUndefined($value, $extends, $path, $i);
  91. }
  92. } else {
  93. $this->checkUndefined($value, $schema->extends, $path, $i);
  94. }
  95. }
  96. // Verify required values
  97. if (is_object($value)) {
  98. if (!($value instanceof UndefinedConstraint) && isset($schema->required) && is_array($schema->required) ) {
  99. // Draft 4 - Required is an array of strings - e.g. "required": ["foo", ...]
  100. foreach ($schema->required as $required) {
  101. if (!property_exists($value, $required)) {
  102. $this->addError((!$path) ? $required : "$path.$required", "The property " . $required . " is required", 'required');
  103. }
  104. }
  105. } else if (isset($schema->required) && !is_array($schema->required)) {
  106. // Draft 3 - Required attribute - e.g. "foo": {"type": "string", "required": true}
  107. if ( $schema->required && $value instanceof UndefinedConstraint) {
  108. $this->addError($path, "Is missing and it is required", 'required');
  109. }
  110. }
  111. }
  112. // Verify type
  113. if (!($value instanceof UndefinedConstraint)) {
  114. $this->checkType($value, $schema, $path);
  115. }
  116. // Verify disallowed items
  117. if (isset($schema->disallow)) {
  118. $initErrors = $this->getErrors();
  119. $typeSchema = new \stdClass();
  120. $typeSchema->type = $schema->disallow;
  121. $this->checkType($value, $typeSchema, $path);
  122. // if no new errors were raised it must be a disallowed value
  123. if (count($this->getErrors()) == count($initErrors)) {
  124. $this->addError($path, "Disallowed value was matched", 'disallow');
  125. } else {
  126. $this->errors = $initErrors;
  127. }
  128. }
  129. if (isset($schema->not)) {
  130. $initErrors = $this->getErrors();
  131. $this->checkUndefined($value, $schema->not, $path, $i);
  132. // if no new errors were raised then the instance validated against the "not" schema
  133. if (count($this->getErrors()) == count($initErrors)) {
  134. $this->addError($path, "Matched a schema which it should not", 'not');
  135. } else {
  136. $this->errors = $initErrors;
  137. }
  138. }
  139. // Verify that dependencies are met
  140. if (is_object($value) && isset($schema->dependencies)) {
  141. $this->validateDependencies($value, $schema->dependencies, $path);
  142. }
  143. }
  144. /**
  145. * Validate allOf, anyOf, and oneOf properties
  146. *
  147. * @param mixed $value
  148. * @param mixed $schema
  149. * @param string $path
  150. * @param string $i
  151. */
  152. protected function validateOfProperties($value, $schema, $path, $i = "")
  153. {
  154. // Verify type
  155. if ($value instanceof UndefinedConstraint) {
  156. return;
  157. }
  158. if (isset($schema->allOf)) {
  159. $isValid = true;
  160. foreach ($schema->allOf as $allOf) {
  161. $initErrors = $this->getErrors();
  162. $this->checkUndefined($value, $allOf, $path, $i);
  163. $isValid = $isValid && (count($this->getErrors()) == count($initErrors));
  164. }
  165. if (!$isValid) {
  166. $this->addError($path, "Failed to match all schemas", 'allOf');
  167. }
  168. }
  169. if (isset($schema->anyOf)) {
  170. $isValid = false;
  171. $startErrors = $this->getErrors();
  172. foreach ($schema->anyOf as $anyOf) {
  173. $initErrors = $this->getErrors();
  174. $this->checkUndefined($value, $anyOf, $path, $i);
  175. if ($isValid = (count($this->getErrors()) == count($initErrors))) {
  176. break;
  177. }
  178. }
  179. if (!$isValid) {
  180. $this->addError($path, "Failed to match at least one schema", 'anyOf');
  181. } else {
  182. $this->errors = $startErrors;
  183. }
  184. }
  185. if (isset($schema->oneOf)) {
  186. $allErrors = array();
  187. $matchedSchemas = 0;
  188. $startErrors = $this->getErrors();
  189. foreach ($schema->oneOf as $oneOf) {
  190. $this->errors = array();
  191. $this->checkUndefined($value, $oneOf, $path, $i);
  192. if (count($this->getErrors()) == 0) {
  193. $matchedSchemas++;
  194. }
  195. $allErrors = array_merge($allErrors, array_values($this->getErrors()));
  196. }
  197. if ($matchedSchemas !== 1) {
  198. $this->addErrors(
  199. array_merge(
  200. $allErrors,
  201. array(array(
  202. 'property' => $path,
  203. 'message' => "Failed to match exactly one schema",
  204. 'constraint' => 'oneOf',
  205. ),),
  206. $startErrors
  207. )
  208. );
  209. } else {
  210. $this->errors = $startErrors;
  211. }
  212. }
  213. }
  214. /**
  215. * Validate dependencies
  216. *
  217. * @param mixed $value
  218. * @param mixed $dependencies
  219. * @param string $path
  220. * @param string $i
  221. */
  222. protected function validateDependencies($value, $dependencies, $path, $i = "")
  223. {
  224. foreach ($dependencies as $key => $dependency) {
  225. if (property_exists($value, $key)) {
  226. if (is_string($dependency)) {
  227. // Draft 3 string is allowed - e.g. "dependencies": {"bar": "foo"}
  228. if (!property_exists($value, $dependency)) {
  229. $this->addError($path, "$key depends on $dependency and $dependency is missing", 'dependencies');
  230. }
  231. } else if (is_array($dependency)) {
  232. // Draft 4 must be an array - e.g. "dependencies": {"bar": ["foo"]}
  233. foreach ($dependency as $d) {
  234. if (!property_exists($value, $d)) {
  235. $this->addError($path, "$key depends on $d and $d is missing", 'dependencies');
  236. }
  237. }
  238. } else if (is_object($dependency)) {
  239. // Schema - e.g. "dependencies": {"bar": {"properties": {"foo": {...}}}}
  240. $this->checkUndefined($value, $dependency, $path, $i);
  241. }
  242. }
  243. }
  244. }
  245. protected function validateUri($schema, $schemaUri = null)
  246. {
  247. $resolver = new UriResolver();
  248. $retriever = $this->getUriRetriever();
  249. $jsonSchema = null;
  250. if ($resolver->isValid($schemaUri)) {
  251. $schemaId = property_exists($schema, 'id') ? $schema->id : null;
  252. $jsonSchema = $retriever->retrieve($schemaId, $schemaUri);
  253. }
  254. return $jsonSchema;
  255. }
  256. }