New Library
This commit is contained in:
parent
5c6f6c97b7
commit
c479658f0b
83 changed files with 5788 additions and 0 deletions
72
server/vendor/php-opencloud/common/src/Common/JsonSchema/Schema.php
vendored
Normal file
72
server/vendor/php-opencloud/common/src/Common/JsonSchema/Schema.php
vendored
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace OpenCloud\Common\JsonSchema;
|
||||
|
||||
use JsonSchema\Validator;
|
||||
|
||||
class Schema
|
||||
{
|
||||
private $body;
|
||||
private $validator;
|
||||
|
||||
public function __construct($body, Validator $validator = null)
|
||||
{
|
||||
$this->body = (object) $body;
|
||||
$this->validator = $validator ?: new Validator();
|
||||
}
|
||||
|
||||
public function getPropertyPaths()
|
||||
{
|
||||
$paths = [];
|
||||
|
||||
foreach ($this->body->properties as $propertyName => $property) {
|
||||
$paths[] = sprintf("/%s", $propertyName);
|
||||
}
|
||||
|
||||
return $paths;
|
||||
}
|
||||
|
||||
public function normalizeObject($subject, array $aliases)
|
||||
{
|
||||
$out = new \stdClass;
|
||||
|
||||
foreach ($this->body->properties as $propertyName => $property) {
|
||||
$name = isset($aliases[$propertyName]) ? $aliases[$propertyName] : $propertyName;
|
||||
if (isset($property->readOnly) && $property->readOnly === true) {
|
||||
continue;
|
||||
} elseif (property_exists($subject, $name)) {
|
||||
$out->$propertyName = $subject->$name;
|
||||
} elseif (property_exists($subject, $propertyName)) {
|
||||
$out->$propertyName = $subject->$propertyName;
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function validate($data)
|
||||
{
|
||||
$this->validator->check($data, $this->body);
|
||||
}
|
||||
|
||||
public function isValid()
|
||||
{
|
||||
return $this->validator->isValid();
|
||||
}
|
||||
|
||||
public function getErrors()
|
||||
{
|
||||
return $this->validator->getErrors();
|
||||
}
|
||||
|
||||
public function getErrorString()
|
||||
{
|
||||
$msg = "Provided values do not validate. Errors:\n";
|
||||
|
||||
foreach ($this->getErrors() as $error) {
|
||||
$msg .= sprintf("[%s] %s\n", $error['property'], $error['message']);
|
||||
}
|
||||
|
||||
return $msg;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue