MailPoet\EmailEditor\Validator\Schema

One_Of_Schema{}WC 1.0

Represents a schema that allows a value to match one of the given schemas. See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#oneof-and-anyof

No Hooks.

Usage

$One_Of_Schema = new One_Of_Schema();
// use class methods

Methods

  1. public __construct(
  2. public non_nullable()
  3. public nullable()

One_Of_Schema{} code WC 9.8.1

class One_Of_Schema extends Schema {
	/**
	 * Schema definition.
	 *
	 * @var array
	 */
	protected $schema = array(
		'oneOf' => array(),
	);

	/**
	 * One_Of_Schema constructor.
	 *
	 * @param Schema[] $schemas List of schemas.
	 */
	public function __construct(
		array $schemas
	) {
		foreach ( $schemas as $schema ) {
			$this->schema['oneOf'][] = $schema->to_array();
		}
	}

	/**
	 * Sets the schema as nullable.
	 */
	public function nullable(): self {
		$null   = array( 'type' => 'null' );
		$one_of = $this->schema['oneOf'];
		$value  = in_array( $null, $one_of, true ) ? $one_of : array_merge( $one_of, array( $null ) );
		return $this->update_schema_property( 'oneOf', $value );
	}

	/**
	 * Sets the schema as non-nullable.
	 */
	public function non_nullable(): self {
		$null   = array( 'type' => 'null' );
		$one_of = $this->schema['one_of'];
		$value  = array_filter(
			$one_of,
			function ( $item ) use ( $null ) {
				return $item !== $null;
			}
		);
		return $this->update_schema_property( 'one_of', $value );
	}
}