AnyOfSchema.php
4 years ago
ArraySchema.php
4 years ago
BooleanSchema.php
4 years ago
IntegerSchema.php
4 years ago
NullSchema.php
4 years ago
NumberSchema.php
4 years ago
ObjectSchema.php
4 years ago
OneOfSchema.php
4 years ago
StringSchema.php
4 years ago
index.php
3 years ago
AnyOfSchema.php
41 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Validator\Schema; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Validator\Schema; |
| 9 | |
| 10 | // See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#oneof-and-anyof |
| 11 | class AnyOfSchema extends Schema { |
| 12 | protected $schema = [ |
| 13 | 'anyOf' => [], |
| 14 | ]; |
| 15 | |
| 16 | /** @param Schema[] $schemas */ |
| 17 | public function __construct( |
| 18 | array $schemas |
| 19 | ) { |
| 20 | foreach ($schemas as $schema) { |
| 21 | $this->schema['anyOf'][] = $schema->toArray(); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | public function nullable(): self { |
| 26 | $null = ['type' => 'null']; |
| 27 | $anyOf = $this->schema['anyOf']; |
| 28 | $value = in_array($null, $anyOf, true) ? $anyOf : array_merge($anyOf, [$null]); |
| 29 | return $this->updateSchemaProperty('anyOf', $value); |
| 30 | } |
| 31 | |
| 32 | public function nonNullable(): self { |
| 33 | $null = ['type' => 'null']; |
| 34 | $anyOf = $this->schema['anyOf']; |
| 35 | $value = array_filter($anyOf, function ($item) use ($null) { |
| 36 | return $item !== $null; |
| 37 | }); |
| 38 | return $this->updateSchemaProperty('anyOf', $value); |
| 39 | } |
| 40 | } |
| 41 |