init
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Validator\Schema;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#oneof-and-anyof
|
||||
class AnyOfSchema extends Schema {
|
||||
protected $schema = [
|
||||
'anyOf' => [],
|
||||
];
|
||||
|
||||
/** @param Schema[] $schemas */
|
||||
public function __construct(
|
||||
array $schemas
|
||||
) {
|
||||
foreach ($schemas as $schema) {
|
||||
$this->schema['anyOf'][] = $schema->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
public function nullable(): self {
|
||||
$null = ['type' => 'null'];
|
||||
$anyOf = $this->schema['anyOf'];
|
||||
$value = in_array($null, $anyOf, true) ? $anyOf : array_merge($anyOf, [$null]);
|
||||
return $this->updateSchemaProperty('anyOf', $value);
|
||||
}
|
||||
|
||||
public function nonNullable(): self {
|
||||
$null = ['type' => 'null'];
|
||||
$anyOf = $this->schema['anyOf'];
|
||||
$value = array_filter($anyOf, function ($item) use ($null) {
|
||||
return $item !== $null;
|
||||
});
|
||||
return $this->updateSchemaProperty('anyOf', $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Validator\Schema;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#arrays
|
||||
class ArraySchema extends Schema {
|
||||
protected $schema = [
|
||||
'type' => 'array',
|
||||
];
|
||||
|
||||
public function items(Schema $schema): self {
|
||||
return $this->updateSchemaProperty('items', $schema->toArray());
|
||||
}
|
||||
|
||||
public function minItems(int $value): self {
|
||||
return $this->updateSchemaProperty('minItems', $value);
|
||||
}
|
||||
|
||||
public function maxItems(int $value): self {
|
||||
return $this->updateSchemaProperty('maxItems', $value);
|
||||
}
|
||||
|
||||
public function uniqueItems(): self {
|
||||
return $this->updateSchemaProperty('uniqueItems', true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Validator\Schema;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#primitive-types
|
||||
class BooleanSchema extends Schema {
|
||||
protected $schema = [
|
||||
'type' => 'boolean',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Validator\Schema;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#numbers
|
||||
class IntegerSchema extends Schema {
|
||||
protected $schema = [
|
||||
'type' => 'integer',
|
||||
];
|
||||
|
||||
public function minimum(int $value): self {
|
||||
return $this->updateSchemaProperty('minimum', $value)
|
||||
->unsetSchemaProperty('exclusiveMinimum');
|
||||
}
|
||||
|
||||
public function exclusiveMinimum(int $value): self {
|
||||
return $this->updateSchemaProperty('minimum', $value)
|
||||
->updateSchemaProperty('exclusiveMinimum', true);
|
||||
}
|
||||
|
||||
public function maximum(int $value): self {
|
||||
return $this->updateSchemaProperty('maximum', $value)
|
||||
->unsetSchemaProperty('exclusiveMaximum');
|
||||
}
|
||||
|
||||
public function exclusiveMaximum(int $value): self {
|
||||
return $this->updateSchemaProperty('maximum', $value)
|
||||
->updateSchemaProperty('exclusiveMaximum', true);
|
||||
}
|
||||
|
||||
public function multipleOf(int $value): self {
|
||||
return $this->updateSchemaProperty('multipleOf', $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Validator\Schema;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#primitive-types
|
||||
class NullSchema extends Schema {
|
||||
protected $schema = [
|
||||
'type' => 'null',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Validator\Schema;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#numbers
|
||||
class NumberSchema extends Schema {
|
||||
protected $schema = [
|
||||
'type' => 'number',
|
||||
];
|
||||
|
||||
public function minimum(float $value): self {
|
||||
return $this->updateSchemaProperty('minimum', $value)
|
||||
->unsetSchemaProperty('exclusiveMinimum');
|
||||
}
|
||||
|
||||
public function exclusiveMinimum(float $value): self {
|
||||
return $this->updateSchemaProperty('minimum', $value)
|
||||
->updateSchemaProperty('exclusiveMinimum', true);
|
||||
}
|
||||
|
||||
public function maximum(float $value): self {
|
||||
return $this->updateSchemaProperty('maximum', $value)
|
||||
->unsetSchemaProperty('exclusiveMaximum');
|
||||
}
|
||||
|
||||
public function exclusiveMaximum(float $value): self {
|
||||
return $this->updateSchemaProperty('maximum', $value)
|
||||
->updateSchemaProperty('exclusiveMaximum', true);
|
||||
}
|
||||
|
||||
public function multipleOf(float $value): self {
|
||||
return $this->updateSchemaProperty('multipleOf', $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Validator\Schema;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#objects
|
||||
class ObjectSchema extends Schema {
|
||||
protected $schema = [
|
||||
'type' => 'object',
|
||||
];
|
||||
|
||||
/** @param array<string, Schema> $properties */
|
||||
public function properties(array $properties): self {
|
||||
return $this->updateSchemaProperty('properties', array_map(
|
||||
function (Schema $property) {
|
||||
return $property->toArray();
|
||||
},
|
||||
$properties
|
||||
));
|
||||
}
|
||||
|
||||
public function additionalProperties(Schema $schema): self {
|
||||
return $this->updateSchemaProperty('additionalProperties', $schema->toArray());
|
||||
}
|
||||
|
||||
public function disableAdditionalProperties(): self {
|
||||
return $this->updateSchemaProperty('additionalProperties', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keys of $properties are regular expressions without leading/trailing delimiters.
|
||||
* See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#patternproperties
|
||||
*
|
||||
* @param array<string, Schema> $properties
|
||||
*/
|
||||
public function patternProperties(array $properties): self {
|
||||
$patternProperties = [];
|
||||
foreach ($properties as $key => $value) {
|
||||
$this->validatePattern($key);
|
||||
$patternProperties[$key] = $value->toArray();
|
||||
}
|
||||
return $this->updateSchemaProperty('patternProperties', $patternProperties);
|
||||
}
|
||||
|
||||
public function minProperties(int $value): self {
|
||||
return $this->updateSchemaProperty('minProperties', $value);
|
||||
}
|
||||
|
||||
public function maxProperties(int $value): self {
|
||||
return $this->updateSchemaProperty('maxProperties', $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Validator\Schema;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#oneof-and-anyof
|
||||
class OneOfSchema extends Schema {
|
||||
protected $schema = [
|
||||
'oneOf' => [],
|
||||
];
|
||||
|
||||
/** @param Schema[] $schemas */
|
||||
public function __construct(
|
||||
array $schemas
|
||||
) {
|
||||
foreach ($schemas as $schema) {
|
||||
$this->schema['oneOf'][] = $schema->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
public function nullable(): self {
|
||||
$null = ['type' => 'null'];
|
||||
$oneOf = $this->schema['oneOf'];
|
||||
$value = in_array($null, $oneOf, true) ? $oneOf : array_merge($oneOf, [$null]);
|
||||
return $this->updateSchemaProperty('oneOf', $value);
|
||||
}
|
||||
|
||||
public function nonNullable(): self {
|
||||
$null = ['type' => 'null'];
|
||||
$oneOf = $this->schema['oneOf'];
|
||||
$value = array_filter($oneOf, function ($item) use ($null) {
|
||||
return $item !== $null;
|
||||
});
|
||||
return $this->updateSchemaProperty('oneOf', $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Validator\Schema;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#strings
|
||||
class StringSchema extends Schema {
|
||||
protected $schema = [
|
||||
'type' => 'string',
|
||||
];
|
||||
|
||||
public function minLength(int $value): self {
|
||||
return $this->updateSchemaProperty('minLength', $value);
|
||||
}
|
||||
|
||||
public function maxLength(int $value): self {
|
||||
return $this->updateSchemaProperty('maxLength', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameter $pattern is a regular expression without leading/trailing delimiters.
|
||||
* See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#pattern
|
||||
*/
|
||||
public function pattern(string $pattern): self {
|
||||
$this->validatePattern($pattern);
|
||||
return $this->updateSchemaProperty('pattern', $pattern);
|
||||
}
|
||||
|
||||
public function formatDateTime(): self {
|
||||
return $this->updateSchemaProperty('format', 'date-time');
|
||||
}
|
||||
|
||||
public function formatEmail(): self {
|
||||
return $this->updateSchemaProperty('format', 'email');
|
||||
}
|
||||
|
||||
public function formatHexColor(): self {
|
||||
return $this->updateSchemaProperty('format', 'hex-color');
|
||||
}
|
||||
|
||||
public function formatIp(): self {
|
||||
return $this->updateSchemaProperty('format', 'ip');
|
||||
}
|
||||
|
||||
public function formatUri(): self {
|
||||
return $this->updateSchemaProperty('format', 'uri');
|
||||
}
|
||||
|
||||
public function formatUuid(): self {
|
||||
return $this->updateSchemaProperty('format', 'uuid');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<?php
|
||||
Reference in New Issue
Block a user