Change coding style in email editor package to WordPress

This commit contains automated changes made with phpcbf command from CodeSniffer package.
[MAILPOET-6240]
This commit is contained in:
Jan Lysý
2024-09-24 09:53:09 +02:00
committed by Jan Lysý
parent b6103b4581
commit 554adccce3
60 changed files with 2983 additions and 2853 deletions

View File

@@ -6,32 +6,35 @@ use MailPoet\EmailEditor\Validator\Schema;
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#oneof-and-anyof
class Any_Of_Schema extends Schema {
protected $schema = [
'anyOf' => [],
];
protected $schema = array(
'anyOf' => array(),
);
/** @param Schema[] $schemas */
public function __construct(
array $schemas
) {
foreach ($schemas as $schema) {
$this->schema['anyOf'][] = $schema->toArray();
}
}
/** @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 nullable(): self {
$null = array( 'type' => 'null' );
$anyOf = $this->schema['anyOf'];
$value = in_array( $null, $anyOf, true ) ? $anyOf : array_merge( $anyOf, array( $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);
}
public function nonNullable(): self {
$null = array( 'type' => 'null' );
$anyOf = $this->schema['anyOf'];
$value = array_filter(
$anyOf,
function ( $item ) use ( $null ) {
return $item !== $null;
}
);
return $this->updateSchemaProperty( 'anyOf', $value );
}
}

View File

@@ -6,23 +6,23 @@ use MailPoet\EmailEditor\Validator\Schema;
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#arrays
class Array_Schema extends Schema {
protected $schema = [
'type' => 'array',
];
protected $schema = array(
'type' => 'array',
);
public function items(Schema $schema): self {
return $this->updateSchemaProperty('items', $schema->toArray());
}
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 minItems( int $value ): self {
return $this->updateSchemaProperty( 'minItems', $value );
}
public function maxItems(int $value): self {
return $this->updateSchemaProperty('maxItems', $value);
}
public function maxItems( int $value ): self {
return $this->updateSchemaProperty( 'maxItems', $value );
}
public function uniqueItems(): self {
return $this->updateSchemaProperty('uniqueItems', true);
}
public function uniqueItems(): self {
return $this->updateSchemaProperty( 'uniqueItems', true );
}
}

View File

@@ -6,7 +6,7 @@ use MailPoet\EmailEditor\Validator\Schema;
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#primitive-types
class Boolean_Schema extends Schema {
protected $schema = [
'type' => 'boolean',
];
protected $schema = array(
'type' => 'boolean',
);
}

View File

@@ -6,31 +6,31 @@ use MailPoet\EmailEditor\Validator\Schema;
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#numbers
class Integer_Schema extends Schema {
protected $schema = [
'type' => 'integer',
];
protected $schema = array(
'type' => 'integer',
);
public function minimum(int $value): self {
return $this->updateSchemaProperty('minimum', $value)
->unsetSchemaProperty('exclusiveMinimum');
}
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 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 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 exclusiveMaximum( int $value ): self {
return $this->updateSchemaProperty( 'maximum', $value )
->updateSchemaProperty( 'exclusiveMaximum', true );
}
public function multipleOf(int $value): self {
return $this->updateSchemaProperty('multipleOf', $value);
}
public function multipleOf( int $value ): self {
return $this->updateSchemaProperty( 'multipleOf', $value );
}
}

View File

@@ -6,7 +6,7 @@ use MailPoet\EmailEditor\Validator\Schema;
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#primitive-types
class Null_Schema extends Schema {
protected $schema = [
'type' => 'null',
];
protected $schema = array(
'type' => 'null',
);
}

View File

@@ -6,31 +6,31 @@ use MailPoet\EmailEditor\Validator\Schema;
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#numbers
class Number_Schema extends Schema {
protected $schema = [
'type' => 'number',
];
protected $schema = array(
'type' => 'number',
);
public function minimum(float $value): self {
return $this->updateSchemaProperty('minimum', $value)
->unsetSchemaProperty('exclusiveMinimum');
}
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 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 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 exclusiveMaximum( float $value ): self {
return $this->updateSchemaProperty( 'maximum', $value )
->updateSchemaProperty( 'exclusiveMaximum', true );
}
public function multipleOf(float $value): self {
return $this->updateSchemaProperty('multipleOf', $value);
}
public function multipleOf( float $value ): self {
return $this->updateSchemaProperty( 'multipleOf', $value );
}
}

View File

@@ -6,48 +6,51 @@ use MailPoet\EmailEditor\Validator\Schema;
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#objects
class Object_Schema extends Schema {
protected $schema = [
'type' => 'object',
];
protected $schema = array(
'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
));
}
/** @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 additionalProperties( Schema $schema ): self {
return $this->updateSchemaProperty( 'additionalProperties', $schema->toArray() );
}
public function disableAdditionalProperties(): self {
return $this->updateSchemaProperty('additionalProperties', false);
}
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);
}
/**
* 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 = array();
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 minProperties( int $value ): self {
return $this->updateSchemaProperty( 'minProperties', $value );
}
public function maxProperties(int $value): self {
return $this->updateSchemaProperty('maxProperties', $value);
}
public function maxProperties( int $value ): self {
return $this->updateSchemaProperty( 'maxProperties', $value );
}
}

View File

@@ -6,32 +6,35 @@ use MailPoet\EmailEditor\Validator\Schema;
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#oneof-and-anyof
class One_Of_Schema extends Schema {
protected $schema = [
'oneOf' => [],
];
protected $schema = array(
'oneOf' => array(),
);
/** @param Schema[] $schemas */
public function __construct(
array $schemas
) {
foreach ($schemas as $schema) {
$this->schema['oneOf'][] = $schema->toArray();
}
}
/** @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 nullable(): self {
$null = array( 'type' => 'null' );
$oneOf = $this->schema['oneOf'];
$value = in_array( $null, $oneOf, true ) ? $oneOf : array_merge( $oneOf, array( $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);
}
public function nonNullable(): self {
$null = array( 'type' => 'null' );
$oneOf = $this->schema['oneOf'];
$value = array_filter(
$oneOf,
function ( $item ) use ( $null ) {
return $item !== $null;
}
);
return $this->updateSchemaProperty( 'oneOf', $value );
}
}

View File

@@ -6,48 +6,48 @@ use MailPoet\EmailEditor\Validator\Schema;
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#strings
class String_Schema extends Schema {
protected $schema = [
'type' => 'string',
];
protected $schema = array(
'type' => 'string',
);
public function minLength(int $value): self {
return $this->updateSchemaProperty('minLength', $value);
}
public function minLength( int $value ): self {
return $this->updateSchemaProperty( 'minLength', $value );
}
public function maxLength(int $value): self {
return $this->updateSchemaProperty('maxLength', $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);
}
/**
* 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 formatDateTime(): self {
return $this->updateSchemaProperty( 'format', 'date-time' );
}
public function formatEmail(): self {
return $this->updateSchemaProperty('format', 'email');
}
public function formatEmail(): self {
return $this->updateSchemaProperty( 'format', 'email' );
}
public function formatHexColor(): self {
return $this->updateSchemaProperty('format', 'hex-color');
}
public function formatHexColor(): self {
return $this->updateSchemaProperty( 'format', 'hex-color' );
}
public function formatIp(): self {
return $this->updateSchemaProperty('format', 'ip');
}
public function formatIp(): self {
return $this->updateSchemaProperty( 'format', 'ip' );
}
public function formatUri(): self {
return $this->updateSchemaProperty('format', 'uri');
}
public function formatUri(): self {
return $this->updateSchemaProperty( 'format', 'uri' );
}
public function formatUuid(): self {
return $this->updateSchemaProperty('format', 'uuid');
}
public function formatUuid(): self {
return $this->updateSchemaProperty( 'format', 'uuid' );
}
}

View File

@@ -14,44 +14,44 @@ use MailPoet\EmailEditor\Validator\Schema\String_Schema;
// See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/
class Builder {
public static function string(): String_Schema {
return new String_Schema();
}
public static function string(): String_Schema {
return new String_Schema();
}
public static function number(): Number_Schema {
return new Number_Schema();
}
public static function number(): Number_Schema {
return new Number_Schema();
}
public static function integer(): Integer_Schema {
return new Integer_Schema();
}
public static function integer(): Integer_Schema {
return new Integer_Schema();
}
public static function boolean(): Boolean_Schema {
return new Boolean_Schema();
}
public static function boolean(): Boolean_Schema {
return new Boolean_Schema();
}
public static function null(): Null_Schema {
return new Null_Schema();
}
public static function null(): Null_Schema {
return new Null_Schema();
}
public static function array(Schema $items = null): Array_Schema {
$array = new Array_Schema();
return $items ? $array->items($items) : $array;
}
public static function array( Schema $items = null ): Array_Schema {
$array = new Array_Schema();
return $items ? $array->items( $items ) : $array;
}
/** @param array<string, Schema>|null $properties */
public static function object(array $properties = null): Object_Schema {
$object = new Object_Schema();
return $properties === null ? $object : $object->properties($properties);
}
/** @param array<string, Schema>|null $properties */
public static function object( array $properties = null ): Object_Schema {
$object = new Object_Schema();
return $properties === null ? $object : $object->properties( $properties );
}
/** @param Schema[] $schemas */
public static function oneOf(array $schemas): One_Of_Schema {
return new One_Of_Schema($schemas);
}
/** @param Schema[] $schemas */
public static function oneOf( array $schemas ): One_Of_Schema {
return new One_Of_Schema( $schemas );
}
/** @param Schema[] $schemas */
public static function anyOf(array $schemas): Any_Of_Schema {
return new Any_Of_Schema($schemas);
}
/** @param Schema[] $schemas */
public static function anyOf( array $schemas ): Any_Of_Schema {
return new Any_Of_Schema( $schemas );
}
}

View File

@@ -2,96 +2,95 @@
namespace MailPoet\EmailEditor\Validator;
use function json_encode;
use function rest_get_allowed_schema_keywords;
abstract class Schema {
protected $schema = [];
protected $schema = array();
/** @return static */
public function nullable() {
$type = $this->schema['type'] ?? ['null'];
return $this->updateSchemaProperty('type', is_array($type) ? $type : [$type, 'null']);
}
/** @return static */
public function nullable() {
$type = $this->schema['type'] ?? array( 'null' );
return $this->updateSchemaProperty( 'type', is_array( $type ) ? $type : array( $type, 'null' ) );
}
/** @return static */
public function nonNullable() {
$type = $this->schema['type'] ?? null;
return $type === null
? $this->unsetSchemaProperty('type')
: $this->updateSchemaProperty('type', is_array($type) ? $type[0] : $type);
}
/** @return static */
public function nonNullable() {
$type = $this->schema['type'] ?? null;
return $type === null
? $this->unsetSchemaProperty( 'type' )
: $this->updateSchemaProperty( 'type', is_array( $type ) ? $type[0] : $type );
}
/** @return static */
public function required() {
return $this->updateSchemaProperty('required', true);
}
/** @return static */
public function required() {
return $this->updateSchemaProperty( 'required', true );
}
/** @return static */
public function optional() {
return $this->unsetSchemaProperty('required');
}
/** @return static */
public function optional() {
return $this->unsetSchemaProperty( 'required' );
}
/** @return static */
public function title(string $title) {
return $this->updateSchemaProperty('title', $title);
}
/** @return static */
public function title( string $title ) {
return $this->updateSchemaProperty( 'title', $title );
}
/** @return static */
public function description(string $description) {
return $this->updateSchemaProperty('description', $description);
}
/** @return static */
public function description( string $description ) {
return $this->updateSchemaProperty( 'description', $description );
}
/** @return static */
public function default($default) {
return $this->updateSchemaProperty('default', $default);
}
/** @return static */
public function default( $default ) {
return $this->updateSchemaProperty( 'default', $default );
}
/** @return static */
public function field(string $name, $value) {
if (in_array($name, $this->getReservedKeywords(), true)) {
throw new \Exception("Field name '$name' is reserved");
}
return $this->updateSchemaProperty($name, $value);
}
/** @return static */
public function field( string $name, $value ) {
if ( in_array( $name, $this->getReservedKeywords(), true ) ) {
throw new \Exception( "Field name '$name' is reserved" );
}
return $this->updateSchemaProperty( $name, $value );
}
public function toArray(): array {
return $this->schema;
}
public function toArray(): array {
return $this->schema;
}
public function toString(): string {
$json = json_encode($this->schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION);
$error = json_last_error();
if ($error || $json === false) {
throw new \Exception(json_last_error_msg(), (string)$error);
}
return $json;
}
public function toString(): string {
$json = json_encode( $this->schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION );
$error = json_last_error();
if ( $error || $json === false ) {
throw new \Exception( json_last_error_msg(), (string) $error );
}
return $json;
}
/** @return static */
protected function updateSchemaProperty(string $name, $value) {
$clone = clone $this;
$clone->schema[$name] = $value;
return $clone;
}
/** @return static */
protected function updateSchemaProperty( string $name, $value ) {
$clone = clone $this;
$clone->schema[ $name ] = $value;
return $clone;
}
/** @return static */
protected function unsetSchemaProperty(string $name) {
$clone = clone $this;
unset($clone->schema[$name]);
return $clone;
}
/** @return static */
protected function unsetSchemaProperty( string $name ) {
$clone = clone $this;
unset( $clone->schema[ $name ] );
return $clone;
}
protected function getReservedKeywords(): array {
return rest_get_allowed_schema_keywords();
}
protected function getReservedKeywords(): array {
return rest_get_allowed_schema_keywords();
}
protected function validatePattern(string $pattern): void {
$escaped = str_replace('#', '\\#', $pattern);
$regex = "#$escaped#u";
if (@preg_match($regex, '') === false) {
throw new \Exception("Invalid regular expression '$regex'");
}
}
protected function validatePattern( string $pattern ): void {
$escaped = str_replace( '#', '\\#', $pattern );
$regex = "#$escaped#u";
if ( @preg_match( $regex, '' ) === false ) {
throw new \Exception( "Invalid regular expression '$regex'" );
}
}
}

View File

@@ -6,17 +6,17 @@ use MailPoet\UnexpectedValueException;
use WP_Error;
class Validation_Exception extends UnexpectedValueException {
/** @var WP_Error */
protected $wpError;
/** @var WP_Error */
protected $wpError;
public static function createFromWpError(WP_Error $wpError): self {
$exception = self::create()
->withMessage($wpError->get_error_message());
$exception->wpError = $wpError;
return $exception;
}
public static function createFromWpError( WP_Error $wpError ): self {
$exception = self::create()
->withMessage( $wpError->get_error_message() );
$exception->wpError = $wpError;
return $exception;
}
public function getWpError(): WP_Error {
return $this->wpError;
}
public function getWpError(): WP_Error {
return $this->wpError;
}
}

View File

@@ -8,202 +8,210 @@ use stdClass;
use WP_Error;
class Validator {
/** @var WPFunctions */
private $wp;
/** @var WPFunctions */
private $wp;
public function __construct(
WPFunctions $wp
) {
$this->wp = $wp;
}
public function __construct(
WPFunctions $wp
) {
$this->wp = $wp;
}
/**
* Strict validation & sanitization implementation.
* It only coerces int to float (e.g. 5 to 5.0).
*
* @param mixed $value
* @return mixed
*/
public function validate(Schema $schema, $value, string $paramName = 'value') {
return $this->validateSchemaArray($schema->toArray(), $value, $paramName);
}
/**
* Strict validation & sanitization implementation.
* It only coerces int to float (e.g. 5 to 5.0).
*
* @param mixed $value
* @return mixed
*/
public function validate( Schema $schema, $value, string $paramName = 'value' ) {
return $this->validateSchemaArray( $schema->toArray(), $value, $paramName );
}
/**
* Strict validation & sanitization implementation.
* It only coerces int to float (e.g. 5 to 5.0).
*
* @param array $schema. The array must follow the format, which is returned from Schema::toArray().
* @param mixed $value
* @return mixed
*/
public function validateSchemaArray(array $schema, $value, string $paramName = 'value') {
$result = $this->validateAndSanitizeValueFromSchema($value, $schema, $paramName);
if ($result instanceof WP_Error) {
throw Validation_Exception::createFromWpError($result);
}
return $result;
}
/**
* Strict validation & sanitization implementation.
* It only coerces int to float (e.g. 5 to 5.0).
*
* @param array $schema. The array must follow the format, which is returned from Schema::toArray().
* @param mixed $value
* @return mixed
*/
public function validateSchemaArray( array $schema, $value, string $paramName = 'value' ) {
$result = $this->validateAndSanitizeValueFromSchema( $value, $schema, $paramName );
if ( $result instanceof WP_Error ) {
throw Validation_Exception::createFromWpError( $result );
}
return $result;
}
/**
* Mirrors rest_validate_value_from_schema() and rest_sanitize_value_from_schema().
*
* @param mixed $value
* @param array $schema
* @param string $paramName
* @return mixed|WP_Error
*/
private function validateAndSanitizeValueFromSchema($value, array $schema, string $paramName) {
// nullable
$fullType = $schema['type'] ?? null;
if (is_array($fullType) && in_array('null', $fullType, true) && $value === null) {
return null;
}
/**
* Mirrors rest_validate_value_from_schema() and rest_sanitize_value_from_schema().
*
* @param mixed $value
* @param array $schema
* @param string $paramName
* @return mixed|WP_Error
*/
private function validateAndSanitizeValueFromSchema( $value, array $schema, string $paramName ) {
// nullable
$fullType = $schema['type'] ?? null;
if ( is_array( $fullType ) && in_array( 'null', $fullType, true ) && $value === null ) {
return null;
}
// anyOf, oneOf
if (isset($schema['anyOf'])) {
return $this->validateAndSanitizeAnyOf($value, $schema, $paramName);
} elseif (isset($schema['oneOf'])) {
return $this->validateAndSanitizeOneOf($value, $schema, $paramName);
}
// anyOf, oneOf
if ( isset( $schema['anyOf'] ) ) {
return $this->validateAndSanitizeAnyOf( $value, $schema, $paramName );
} elseif ( isset( $schema['oneOf'] ) ) {
return $this->validateAndSanitizeOneOf( $value, $schema, $paramName );
}
// make types strict
$type = is_array($fullType) ? $fullType[0] : $fullType;
switch ($type) {
case 'number':
if (!is_float($value) && !is_int($value)) {
return $this->getTypeError($paramName, $fullType);
}
break;
case 'integer':
if (!is_int($value)) {
return $this->getTypeError($paramName, $fullType);
}
break;
case 'boolean':
if (!is_bool($value)) {
return $this->getTypeError($paramName, $fullType);
}
break;
case 'array':
if (!is_array($value)) {
return $this->getTypeError($paramName, $fullType);
}
// make types strict
$type = is_array( $fullType ) ? $fullType[0] : $fullType;
switch ( $type ) {
case 'number':
if ( ! is_float( $value ) && ! is_int( $value ) ) {
return $this->getTypeError( $paramName, $fullType );
}
break;
case 'integer':
if ( ! is_int( $value ) ) {
return $this->getTypeError( $paramName, $fullType );
}
break;
case 'boolean':
if ( ! is_bool( $value ) ) {
return $this->getTypeError( $paramName, $fullType );
}
break;
case 'array':
if ( ! is_array( $value ) ) {
return $this->getTypeError( $paramName, $fullType );
}
if (isset($schema['items'])) {
foreach ($value as $i => $v) {
$result = $this->validateAndSanitizeValueFromSchema($v, $schema['items'], $paramName . '[' . $i . ']');
if ($this->wp->isWpError($result)) {
return $result;
}
}
}
break;
case 'object':
if (!is_array($value) && !$value instanceof stdClass && !$value instanceof JsonSerializable) {
return $this->getTypeError($paramName, $fullType);
}
if ( isset( $schema['items'] ) ) {
foreach ( $value as $i => $v ) {
$result = $this->validateAndSanitizeValueFromSchema( $v, $schema['items'], $paramName . '[' . $i . ']' );
if ( $this->wp->isWpError( $result ) ) {
return $result;
}
}
}
break;
case 'object':
if ( ! is_array( $value ) && ! $value instanceof stdClass && ! $value instanceof JsonSerializable ) {
return $this->getTypeError( $paramName, $fullType );
}
// ensure string keys
$value = (array)($value instanceof JsonSerializable ? $value->jsonSerialize() : $value);
if (count(array_filter(array_keys($value), 'is_string')) !== count($value)) {
return $this->getTypeError($paramName, $fullType);
}
// ensure string keys
$value = (array) ( $value instanceof JsonSerializable ? $value->jsonSerialize() : $value );
if ( count( array_filter( array_keys( $value ), 'is_string' ) ) !== count( $value ) ) {
return $this->getTypeError( $paramName, $fullType );
}
// validate object properties
foreach ($value as $k => $v) {
if (isset($schema['properties'][$k])) {
$result = $this->validateAndSanitizeValueFromSchema($v, $schema['properties'][$k], $paramName . '[' . $k . ']');
if ($this->wp->isWpError($result)) {
return $result;
}
continue;
}
// validate object properties
foreach ( $value as $k => $v ) {
if ( isset( $schema['properties'][ $k ] ) ) {
$result = $this->validateAndSanitizeValueFromSchema( $v, $schema['properties'][ $k ], $paramName . '[' . $k . ']' );
if ( $this->wp->isWpError( $result ) ) {
return $result;
}
continue;
}
$patternPropertySchema = $this->wp->restFindMatchingPatternPropertySchema($k, $schema);
if ($patternPropertySchema) {
$result = $this->validateAndSanitizeValueFromSchema($v, $patternPropertySchema, $paramName . '[' . $k . ']');
if ($this->wp->isWpError($result)) {
return $result;
}
continue;
}
$patternPropertySchema = $this->wp->restFindMatchingPatternPropertySchema( $k, $schema );
if ( $patternPropertySchema ) {
$result = $this->validateAndSanitizeValueFromSchema( $v, $patternPropertySchema, $paramName . '[' . $k . ']' );
if ( $this->wp->isWpError( $result ) ) {
return $result;
}
continue;
}
if (isset($schema['additionalProperties']) && is_array($schema['additionalProperties'])) {
$result = $this->validateAndSanitizeValueFromSchema($v, $schema['additionalProperties'], $paramName . '[' . $k . ']');
if ($this->wp->isWpError($result)) {
return $result;
}
}
}
break;
}
if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
$result = $this->validateAndSanitizeValueFromSchema( $v, $schema['additionalProperties'], $paramName . '[' . $k . ']' );
if ( $this->wp->isWpError( $result ) ) {
return $result;
}
}
}
break;
}
$result = $this->wp->restValidateValueFromSchema($value, $schema, $paramName);
if ($this->wp->isWpError($result)) {
return $result;
}
return $this->wp->restSanitizeValueFromSchema($value, $schema, $paramName);
}
$result = $this->wp->restValidateValueFromSchema( $value, $schema, $paramName );
if ( $this->wp->isWpError( $result ) ) {
return $result;
}
return $this->wp->restSanitizeValueFromSchema( $value, $schema, $paramName );
}
/**
* Mirrors rest_find_any_matching_schema().
*
* @param mixed $value
* @return mixed|WP_Error
*/
private function validateAndSanitizeAnyOf($value, array $anyOfSchema, string $paramName) {
$errors = [];
foreach ($anyOfSchema['anyOf'] as $index => $schema) {
$result = $this->validateAndSanitizeValueFromSchema($value, $schema, $paramName);
if (!$this->wp->isWpError($result)) {
return $result;
}
$errors[] = ['error_object' => $result, 'schema' => $schema, 'index' => $index];
}
return $this->wp->restGetCombiningOperationError($value, $paramName, $errors);
}
/**
* Mirrors rest_find_any_matching_schema().
*
* @param mixed $value
* @return mixed|WP_Error
*/
private function validateAndSanitizeAnyOf( $value, array $anyOfSchema, string $paramName ) {
$errors = array();
foreach ( $anyOfSchema['anyOf'] as $index => $schema ) {
$result = $this->validateAndSanitizeValueFromSchema( $value, $schema, $paramName );
if ( ! $this->wp->isWpError( $result ) ) {
return $result;
}
$errors[] = array(
'error_object' => $result,
'schema' => $schema,
'index' => $index,
);
}
return $this->wp->restGetCombiningOperationError( $value, $paramName, $errors );
}
/**
* Mirrors rest_find_one_matching_schema().
*
* @param mixed $value
* @return mixed|WP_Error
*/
private function validateAndSanitizeOneOf($value, array $oneOfSchema, string $paramName) {
$matchingSchemas = [];
$errors = [];
$data = null;
foreach ($oneOfSchema['oneOf'] as $index => $schema) {
$result = $this->validateAndSanitizeValueFromSchema($value, $schema, $paramName);
if ($this->wp->isWpError($result)) {
$errors[] = ['error_object' => $result, 'schema' => $schema, 'index' => $index];
} else {
$data = $result;
$matchingSchemas[$index] = $schema;
}
}
/**
* Mirrors rest_find_one_matching_schema().
*
* @param mixed $value
* @return mixed|WP_Error
*/
private function validateAndSanitizeOneOf( $value, array $oneOfSchema, string $paramName ) {
$matchingSchemas = array();
$errors = array();
$data = null;
foreach ( $oneOfSchema['oneOf'] as $index => $schema ) {
$result = $this->validateAndSanitizeValueFromSchema( $value, $schema, $paramName );
if ( $this->wp->isWpError( $result ) ) {
$errors[] = array(
'error_object' => $result,
'schema' => $schema,
'index' => $index,
);
} else {
$data = $result;
$matchingSchemas[ $index ] = $schema;
}
}
if (!$matchingSchemas) {
return $this->wp->restGetCombiningOperationError($value, $paramName, $errors);
}
if ( ! $matchingSchemas ) {
return $this->wp->restGetCombiningOperationError( $value, $paramName, $errors );
}
if (count($matchingSchemas) > 1) {
// reuse WP method to generate detailed error
$invalidSchema = ['type' => []];
$oneOf = array_replace(array_fill(0, count($oneOfSchema['oneOf']), $invalidSchema), $matchingSchemas);
return $this->wp->restFindOneMatchingSchema($value, ['oneOf' => $oneOf], $paramName);
}
return $data;
}
if ( count( $matchingSchemas ) > 1 ) {
// reuse WP method to generate detailed error
$invalidSchema = array( 'type' => array() );
$oneOf = array_replace( array_fill( 0, count( $oneOfSchema['oneOf'] ), $invalidSchema ), $matchingSchemas );
return $this->wp->restFindOneMatchingSchema( $value, array( 'oneOf' => $oneOf ), $paramName );
}
return $data;
}
/** @param string|string[] $type */
private function getTypeError(string $param, $type): WP_Error {
$type = is_array($type) ? $type : [$type];
return new WP_Error(
'rest_invalid_type',
// translators: %1$s is the current parameter and %2$s a comma-separated list of the allowed types.
sprintf(__('%1$s is not of type %2$s.', 'mailpoet'), $param, implode(',', $type)),
['param' => $param]
);
}
/** @param string|string[] $type */
private function getTypeError( string $param, $type ): WP_Error {
$type = is_array( $type ) ? $type : array( $type );
return new WP_Error(
'rest_invalid_type',
// translators: %1$s is the current parameter and %2$s a comma-separated list of the allowed types.
sprintf( __( '%1$s is not of type %2$s.', 'mailpoet' ), $param, implode( ',', $type ) ),
array( 'param' => $param )
);
}
}